第十四章 灵活的构建

1.   通过 <properties> 元素用户可以自定义一个或多个属性,然后在 POM 的其他地方使用 ${ 属性名称 } 的方式引用属性,这种做法的最大意义在于消除重复。

 

2.   Maven 共有六类属性:

  1) 内置属性: ${basedir} 表示项目根目录,即 pom.xml 文件的目录。 ${version} 表示项目版本。

  2) POM 属性:用户可以使用该类属性引用 POM 文件中对应元素的值:

    a)  ${project.build.sourceDirectory} :项目的主源码目录,默认为 src/main/java

    b)  ${project.build.testSourceDirectory} :项目的测试源码目录,默认为 src/test/java

    c)  ${project.build.directory} :项目构建输出目录,默认为 target/

    d)  ${project.outputDirectory} :项目主代码编译输出目录,默认为 target/classes

    e)  ${project.testOutputDirector} :项目测试代码编译输出目录,默认为 target/test-classes/

    f)   ${project.groupId} :项目的 groupId

    g)  ${project.artifactId} :项目的 artifactId

    h)  ${project.version} :项目的 version ,与 ${version} 等价

    i)   ${project.build.finalName} :项目打包输出文件的名称,默认为 ${project.artifactId}-${project.version}

它们的默认值是在超级 POM 中定义的。

 

  3) 自定义属性:用户可以在 POM <properties> 元素下自定义 Maven 属性。

  4) Settings 属性:与 POM 属性同理,用户以 settings. 开头的属性引用 settings.xml 文件中 XML 元素的值。如 ${settings.localRepository} 指向本地仓库的位置

  5) Java 系统属性:所有 Java 系统属性都可以使用 Maven 属性引用。如 ${user.home} 指向用户目录。可以用 mvn help:system 查看所有 Java 系统属性 ( 包含 -D 参数设置的属性 )

  6) 环境变量属性:所有环境变量都可以使用 env. 开头的 Maven 属性引用。如 {env.JAVA_HOME} 。可以用 mvn help:system 查看所有环境变量属性。

 

3.   POM <build> 元素下可以使用 <resources> <testResources> 定义多个资源目录和测试资源目录。在 <resource> <testResource> 下定义 <filtering> 元素来告诉 Maven 是否要解析资源文件中的 Maven 属性,如:

<resources>

  <resource>

    <directory>${project.basedir}/src/main/resources</directory>

    <filtering>true</filtering>

  </resource>

</resources> 
 

4.   mvn -P 参数表示在命令行激活一个 profile 。如:

mvn clean install –Pdev

表示激活 ID dev profile 。我们可以在 id dev profile 中定义 dev 环境下的数据库连接等配置的 Maven 属性,从而用上述命令构建一个 dev 环境下的构件。

 

5.   Maven profile 能够在构建的时候修改 POM 的一个子集,或者添加额外的配置元素。用户可以使用很多方式激活 profile ,以实现构建在不同环境下的移植。

 

6.   Maven 激活 profile 的方式有 :

  1)   命令行激活:通过 mvn 命令行的 –P 参数可以激活一个或多个 profile ,多个 id 之间用逗号分隔。如: mvn clean install –Pdev-x,dev-y

  2)   settings 文件显示激活: settings.xml 文件的 activeProfiles 元素可以配置一个或多个 profile 对于所有项目都是处于激活状态,如:

<settings>

  …

  <activeProfiles>

    <activeProfile>dev-x</activeProfile>

    <activeProfile>dev-y</activeProfile>

  </activeProfiles>

  …

</settings> 
 

  3)   系统属性激活:用户可以配置当某系统属性存在的时候激活 profile (不配置 <value> 属性),进而可以设置当某系统属性存在并等某值的时候激活。如 :

<profiles>

  <profile>

     <activation>

      <property>

        <name>test</name>

        <value>x</value>

      </property>

    </activation>

  </profile>

</profiles>

 

  4) 作系统环境激活:

 

<profiles>

  <profile>

    <activation>

      <os>

        <name>Windows XP</name>

        <family>Windows</family>

        <arch>x86</arch>

         <version>5.1.2600</version>

      </os>

    </activation>

  </profile>

</profiles> 

 这里 family 的值包括 Windows UNIX Mac 等。 name arch version 可以通过查看环境中的系统属性 os.name os.arch os.version 获得。

 

 

  5 文件存在与否激活:

<profiles>

  <profile>

    <activation>

      <file>

        <missing>x.properties</missing>

        <exists>y.properties</exists>

      </file>

    </activation>

  </profile>

</profiles> 
 

  6) 默认激活:

<profiles>

  <profile>

    <activation>

      <activeByDefault>true</activeByDefault>

    </activation>

  </profile>

</profiles>
 

但如果 POM 中有任何一个 profile 通过以上其他任意一种方式被激活了,所有的默认激活配置都会失效。

 

激活的多个 profile 中的冲突属性如何解决?

 

7.   可以用 mvn help:active-profiles 查看当前项目所有被激活的 profile 。可以用 mvn help:all-profiles 查看当前项目目的所有的 profile

 

8.   profile 可以在如下位置声明:

  1)p om.xml :只对当前项目有效

  2) ~/.m2/settings.xml :对该用户所有 Maven 项目有效

  3) M2_HOME/conf/settings.xml :对本机上所有 Maven 项目有效

  4) profiles.xml :可以在项目根目录下使用一个额外的 profiles.xml 文件,只对项目有效。该文件默认不会被安装和部署。 Maven 3 已经不支持这种形式。

POM 中定义的 profile 可以修改以下项目属性:

<profile>

  <repositories/>

  <pluginRepositories/>

  <distributionManagement/>

  <dependencies/>

  <dependencyManagement/>

  <modules/>

  <properties/>

  <reporting/>

  <build>

    <plugins/>

    <defaultGoal/>

    <resources/>

    <testResources/>

    <finalName/>

  </build>

</profile>
 

但在其他地方定义的 profile 由于无法保证它们能随着特定的 pom.xml 一起被分发,因此只允许修改以下属性:

 

<profile>

  <repositories/>

  <pluginRepositories/>

  <properties/>

</profile> 

9.   Web 资源文件默认位于 src/main/webapp/ 目录,打包后位于 WAR 包的根目录。开启一般资源文件的过滤(替换 Maven 属性)也不会影响到 web 资源文件。可以在 POM 中配置 maven-war-plugin 配置 web 资源目录及是否过滤:

<plugin>

  <groupId>org.apache.maven.plugins</groupId>

  <artifactId>maven-war-plugin</artifactId>

  <version>2.1-beta-1</version>

  <configuration>

    <webResources>

      <resource>

        <filtering>true</filtering>

        <directory>src/main/webapp</directory>

        <includes>

          <include>**/*.css</include>

          <include>**/*.js</include>

        </includes>

      </resource>

    </webResources>

  </configuration>

</plugin> 
 

10.   可以用标注将 Unit Test Integration Test 进行分组,从而可以在 profile 中配置 maven-surefire-Plugin 定义执行不同的 Test

<configuration>
  <groups>group-name</groups>
</configuration>
 

 

   

猜你喜欢

转载自seanzhou.iteye.com/blog/1413700