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 -->
      
  2. POM的继承
    通过在子module中指定parent,可以使得子module从父POM中继承一系列属性和配置,示例配置如下:

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

    <!--子module pom 配置-->
    <project>
      <parent>
        <groupId>com.mycompany.app</groupId>
        <artifactId>my-app</artifactId>
        <version>1</version>
        <!--如果project中的pom是子module上层目录,该节点可不做配置-->
        <relativePath>../parent/pom.xml</relativePath>
      </parent>
      <modelVersion>4.0.0</modelVersion>
      <artifactId>my-module</artifactId>
    </project>
  1. 模块化聚合
    Maven允许工程中出现多个模块,多个模块的POM通过聚合的方式连接,其中Project中的POM中的属性和配置,各个子模块将继承。对Project中的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>
     
      <modules>
        <module>my-module</module>
      </modules>
    </project>

    <!--子module pom 配置不变-->
    <project>
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.mycompany.app</groupId>
      <artifactId>my-module</artifactId>
      <version>1</version>
    </project>
  1. 聚合和继承的区别
    Maven中的继承和聚合都会使得子模块中的Module继承Porject中的属性和配置,唯一不同的是对聚合Project中的POM执行命令时,相同的命令会在子模块Module中也执行一遍(这也是聚合存在的意义,方便打包和测试);继承则不会。

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中

推荐阅读更多精彩内容