Maven

Maven POM

POM的全称是Project Obejet Model, 整个项目的核心配置和信息都包含在这份xml文件中,一个最基本的pom文件要求至少有下列三要素:groupId, artifactId, version, 因此一个最小的POM配置文件如下所示:

    <project>
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.mycompany.app</groupId>
      <artifactId>my-app</artifactId>
      <version>1</version>
    </project>
  1. 超级POM
    • 作用:所有的POM全部继承自该POM, 意味着所有POM的默认配置也位于这里
      举例的配置如下:
      build directory: target
      source directory: src/main/java
      test source directory: src/test/java
      output directory: classes

    • 位置:位于 maven 安装目录的lib文件夹下的一个jar包中,使用Java反编译工具(如Java-Decompiler)即可查看,以本机为例,超级POM的位置如下:

      C:\Program Files (x86)\apache-maven-3.6.1\lib\maven-model-builder-3.6.1.jar
      
    • 内容:

      <?xml version="1.0" encoding="UTF-8"?>
      
      <!--
      Licensed to the Apache Software Foundation (ASF) under one
      or more contributor license agreements.  See the NOTICE file
      distributed with this work for additional information
      regarding copyright ownership.  The ASF licenses this file
      to you under the Apache License, Version 2.0 (the
      "License"); you may not use this file except in compliance
      with the License.  You may obtain a copy of the License at
      
          http://www.apache.org/licenses/LICENSE-2.0
      
      Unless required by applicable law or agreed to in writing,
      software distributed under the License is distributed on an
      "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
      KIND, either express or implied.  See the License for the
      specific language governing permissions and limitations
      under the License.
      -->
      
      <!-- START SNIPPET: superpom -->
      <project>
      <modelVersion>4.0.0</modelVersion>
      
      <repositories>
          <repository>
          <id>central</id>
          <name>Central Repository</name>
          <url>https://repo.maven.apache.org/maven2</url>
          <layout>default</layout>
          <snapshots>
              <enabled>false</enabled>
          </snapshots>
          </repository>
      </repositories>
      
      <pluginRepositories>
          <pluginRepository>
          <id>central</id>
          <name>Central Repository</name>
          <url>https://repo.maven.apache.org/maven2</url>
          <layout>default</layout>
          <snapshots>
              <enabled>false</enabled>
          </snapshots>
          <releases>
              <updatePolicy>never</updatePolicy>
          </releases>
          </pluginRepository>
      </pluginRepositories>
      
      <build>
          <directory>${project.basedir}/target</directory>
          <outputDirectory>${project.build.directory}/classes</outputDirectory>
          <finalName>${project.artifactId}-${project.version}</finalName>
          <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
          <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
          <scriptSourceDirectory>${project.basedir}/src/main/scripts</scriptSourceDirectory>
          <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
          <resources>
          <resource>
              <directory>${project.basedir}/src/main/resources</directory>
          </resource>
          </resources>
          <testResources>
          <testResource>
              <directory>${project.basedir}/src/test/resources</directory>
          </testResource>
          </testResources>
          <pluginManagement>
          <!-- NOTE: These plugins will be removed from future versions of the super POM -->
          <!-- They are kept for the moment as they are very unlikely to conflict with lifecycle mappings (MNG-4453) -->
          <plugins>
              <plugin>
              <artifactId>maven-antrun-plugin</artifactId>
              <version>1.3</version>
              </plugin>
              <plugin>
              <artifactId>maven-assembly-plugin</artifactId>
              <version>2.2-beta-5</version>
              </plugin>
              <plugin>
              <artifactId>maven-dependency-plugin</artifactId>
              <version>2.8</version>
              </plugin>
              <plugin>
              <artifactId>maven-release-plugin</artifactId>
              <version>2.5.3</version>
              </plugin>
          </plugins>
          </pluginManagement>
      </build>
      
      <reporting>
          <outputDirectory>${project.build.directory}/site</outputDirectory>
      </reporting>
      
      <profiles>
          <!-- NOTE: The release profile will be removed from future versions of the super POM -->
          <profile>
          <id>release-profile</id>
      
          <activation>
              <property>
              <name>performRelease</name>
              <value>true</value>
              </property>
          </activation>
      
          <build>
              <plugins>
              <plugin>
                  <inherited>true</inherited>
                  <artifactId>maven-source-plugin</artifactId>
                  <executions>
                  <execution>
                      <id>attach-sources</id>
                      <goals>
                      <goal>jar-no-fork</goal>
                      </goals>
                  </execution>
                  </executions>
              </plugin>
              <plugin>
                  <inherited>true</inherited>
                  <artifactId>maven-javadoc-plugin</artifactId>
                  <executions>
                  <execution>
                      <id>attach-javadocs</id>
                      <goals>
                      <goal>jar</goal>
                      </goals>
                  </execution>
                  </executions>
              </plugin>
              <plugin>
                  <inherited>true</inherited>
                  <artifactId>maven-deploy-plugin</artifactId>
                  <configuration>
                  <updateReleaseInfo>true</updateReleaseInfo>
                  </configuration>
              </plugin>
              </plugins>
          </build>
          </profile>
      </profiles>
      
      </project>
      <!-- END SNIPPET: superpom -->
      

POM的继承

子 module 可以继承的元素如下

dependencies
developers and contributors
plugin lists (including reports)
plugin executions with matching ids
plugin configuration
resources

假设现有的项目结构如下:

.
 |-- my-module
 |   `-- pom.xml
 `-- pom.xml

两份 pom 的内容分别如下:

<!--pom.xml-->
<project>
  <modelVersion>4.0.0</modelVersion>
 
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1</version>
</project>
<!--my-module/pom.xml-->
<project>
  <modelVersion>4.0.0</modelVersion>
 
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-module</artifactId>
  <version>1</version>
</project>

现在如果想让 my-module 继承 my-app 的元素,需要在 my-module/pom.xml 中添加 parent 节点,更改后的 my-module/pom.xml 配置如下

    <project>
      <modelVersion>4.0.0</modelVersion>
      <parent>
        <groupId>com.mycompany.app</groupId>
        <artifactId>my-app</artifactId>
        <version>1</version>
        <!--如果project中的pom是子module上层目录,该节点可不做配置-->
        <relativePath>../pom.xml</relativePath>
      </parent>
      <!--注意这里没有配置groupId,version节点,此时groupId,version的值会继承自父POM-->
      <artifactId>my-module</artifactId>
    </project>

模块化聚合

Maven 中的 POM 除了继承,还可以使用聚合来联系各个独立的 POM。我们知道继承是在子模块下定义 parent 节点,这样每个子模块是知道自己的父 POM 是谁,聚合则和继承相反,聚合在父 POM 中定义它所有的子模块,这样做的好处是,父 POM 知道他所有的子模块,这样对父 POM 执行一些命令时,其所有的子模块也会得到执行。
假设现有的项目结构如下:

.
 |-- my-module
 |   `-- pom.xml
 `-- pom.xml

两份 pom 的内容分别如下:

<!--pom.xml-->
<project>
  <modelVersion>4.0.0</modelVersion>
 
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1</version>
</project>
<!--my-module/pom.xml-->
<project>
  <modelVersion>4.0.0</modelVersion>
 
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-module</artifactId>
  <version>1</version>
</project>

现在想让 my-app 和 my-moudle 以聚合的方式连接,只需修改 my-app 的 POM 文件如下:

    <!--project pom 配置-->
    <project>
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.mycompany.app</groupId>
      <artifactId>my-app</artifactId>
      <version>1</version>
      <!--packing方式必须为pom-->
      <packaging>pom</packaging>
      <!--声明它的子module-->
      <modules>
        <!--module节点的值为子模块的artifactId, 注意该值需要根据路径进行调整(如../my-module)-->
        <module>my-module</module>
      </modules>
    </project>
  • 聚合和继承的区别
    继承:使得子模块继承父 POM 的一些属性和配置,可以继承的元素如下:
dependencies
developers and contributors
plugin lists (including reports)
plugin executions with matching ids
plugin configuration
resources

聚合:对父 POM 执行命令时,相同的命令会同时在所有的子模块中执行一遍,方便多模块的打包和测试。注意聚合并不会继承父 POM 中的任何属性和配置。
继承+聚合:子模块继承父 POM 中的属性和配置,同时在父 POM 中执行命令时,在子模块也会得到执行。(常用+推荐)

Maven Properties

为了减少重复性的工作,POM 支持变量,从而达到修改一处,处处生效的效果。在每一个 POM 中,Maven 已经构建了一些内置的变量供我们使用,如

  • ${project.groupId}
  • ${project.artifactId}
  • ${project.version}
  • ${project.basedir}
  • ${project.baseuri}
  • ${maven.build.timestamp}

我们自己也可以声明变量,声明方式如下:

<project>
  <modelVersion>4.0.0</modelVersion>
 
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1</version>

<properties>
    <dbcp-version>1.4</dbcp-version>
    <jdk.version>1.8</jdk.version>
    <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
    <mysql-version>5.1.38</mysql-version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring.version>3.2.6.RELEASE</spring.version>
  </properties>
</project>

声明变量之后,在 POM 文件的任何地方都可以引用该变量了。引用的方式如下:

    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>${dbcp-version}</version>
    </dependency>

变量也存在继承关系,子模块也会覆盖父 POM 中定义的变量。这和面向对象的思想是一样的。

Maven Profile

  1. profile的作用
    我们在开发的时候经常需要不同的运行环境,不同运行环境的配置可能不相同,如数据源,日志文件等,Maven中的profile给予我们开发人员基于不同环境灵活的切换不同配置的能力

  2. 在project中定义你的profile
    我们在如下的pom中定义了两个不同的profile,分别是 testproduction,根据不同的环境,Maven编译的时候会自动把这些变量编译到properties文件中

    <profiles>
        <profile>
            <id>test</id>
            <activation>
                <!--默认使用该profile-->
                <activationByDefault>true</activationByDefault>
            </activation>
            <properties>
                <mvn.log.path>/export/Logs/com.fanyank.app</mvn.log.path>
                <mvn.log.level>INFO</mvn.log.level>
                <mvn.jdbc.mysql.driver></mvn.jdbc.mysql.driver>
                <mvn.jdbc.mysql.url>jdbc:mysql://111.111.111.111:3358/app</mvn.jdbc.mysql.url>
                <mvn.jdbc.mysql.username>fanyank</mvn.jdbc.mysql.username>
                <mvn.jdbc.mysql.password>fanyank</mvn.jdbc.mysql.password>
            </properties>
        </profile>
    
        <profile>
            <id>production</id>
            <properties>
                <mvn.log.path>/export/Logs/com.fanyank.app</mvn.log.path>
                <mvn.log.level>ERROR</mvn.log.level>
                <mvn.jdbc.mysql.driver></mvn.jdbc.mysql.driver>
                <mvn.jdbc.mysql.url>jdbc:mysql://222.222.222.222:3358/app</mvn.jdbc.mysql.url>
                <mvn.jdbc.mysql.username>fanyank</mvn.jdbc.mysql.username>
                <mvn.jdbc.mysql.password>fanyank</mvn.jdbc.mysql.password>
            </properties>
        </profile>
    </profiles>
    
  3. 如何使profile生效呢?

    • 命令指定
      使用 -P 指定生效的profile为production
    mvn groupId:artifactId:goal -P production
    
    • pom中指定
        <profile>
            <id>prod</id>
            <properties>
                <profiles.active>prod</profiles.active>
            </properties>
            <!--activation用来指定激活方式,可以根据jdk环境,环境变量,文件的存在或缺失-->
            <activation>
                <!--配置默认激活-->
                <activeByDefault>true</activeByDefault>
                
                <!--通过jdk版本-->
                <!--当jdk环境版本为1.5时,此profile被激活-->
                <jdk>1.5</jdk>
                <!--当jdk环境版本1.5或以上时,此profile被激活-->
                <jdk>[1.5,)</jdk>
    
                <!--根据当前操作系统-->
                <os>
                    <name>Windows XP</name>
                    <family>Windows</family>
                    <arch>x86</arch>
                    <version>5.1.2600</version>
                </os>
    
                <!--通过系统环境变量,name-value自定义-->
                <property>
                    <name>env</name>
                    <value>test</value>
                </property>
    
                <!--通过文件的存在或缺失-->
                <file>
                    <missing>target/generated-sources/axistools/wsdl2java/
                        com/companyname/group</missing>
                    <exists/>
                </file>
            </activation>
        </profile>
    
    • settings.xml
      针对全局声明的profile(${maven.home}/conf/settings.xml),使用 <activeProfiles> 标签指定生效的profile
    <settings>
      ...
      <activeProfiles>
        <activeProfile>profile-1</activeProfile>
      </activeProfiles>
      ...
    </settings>
    
  4. 我可以使用profile定制化哪些内容?

    • external files
      external files包括全局setting.xml, 你的profile只能用来修改如下三个参数 <repositories>, <pluginRepositories>, <properties>
      其中 <repositories>用来存放你们公司的私服仓库地址,<pluginRepositories>用来存放私服plugin地址,<properties>很少使用
    • profile in POMs
      定制任意数量的键值对,并在编译时期注入到properties中
    <dependencies>
    <plugins>
    <properties>
    <modules>
    <reporting>
    <dependencyManagement>
    <distributionManagement>
    a subset of the <build> element, which consists of:
    <defaultGoal>
    <resources>
    <testResources>
    <finalName>
    
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容