`
popwang
  • 浏览: 58761 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

maven-Profiles

阅读更多
Profiles maven 的一个很关键的术语: profile 是用来定义一些在 build lifecycle 中使用的 environmental variations profile 可以设置成在不同的环境下激活不同的 profile (例如:不同的 OS 激活不同的 profile ,不同的 JVM 激活不同的 profile ,不同的 dabase 激活不同的 profile 等等)。
 
定义 Profiles
 
你可以把 profiles 定义在 4 个地方:
  • %M2_HOME%/conf/settings.xml ,这是针对该部电脑的所有 user profiles ,是 global profiles ,它会影响所有的 maven project build
 
  • <your -home-directory>/.m2/settings.xml ,这是针对 per user profiles ,是 user 级的 profiles ,它会影响当前 user 的所有 maven project build
 
  • 定义在 pom.xml 文件里面,这是仅针对该 project profiles ,是 project 级的 profiles
 
  • profiles.xml ,它和 pom.xml 在同一个目录下,也是 project 级的 profiles ,使用 profiles.xml 的目的是希望把 profiles 的设置从 pom.xml 里抽离出来设置。
 
定义在这 4 个地方的 profiles 中,涉及范围越窄的 profiles 会覆盖范围越宽的 profiles 即:定义在 pom.xml profiles 会覆盖 profiles.xml 的, profiles.xml 的会覆盖 <your -home-directory>/.m2/settings.xml 的, <your -home-directory>/.m2/settings.xml 的会覆盖 %M2_HOME%/conf/settings.xml 的。
 
不过请注意:设置在 pom.xml 里的 profiles 是最最推荐的,因为 pom.xml 会被 deploy repository 里,所以 pom.xml 里的 profiles 才会 available for subsequent builds originating from the repository or as transitive dependencies 。而 settings.xml profiles.xml 里定义的 profiles 不会被 deploy repository ,则有诸多限制,因此,只有下面几个 profiles 能够在 settings.xml profiles.xml 里定义:
  • repositories
  • pluginRepositories
  • properties
 
其他类型的 profiles 必须在 pom.xml 里定义(上面 3 profiles 也可以在 pom.xml 里定义)。
 
Pom.xml 能够定义的 profiles 包括:
  • <repositories>
  • <pluginRepositories>
  • <dependencies>
  • <plugins>
  • <properties> (not actually available in the main POM, but used behind the scenes)
  • <modules>
  • <reporting>
  • <dependencyManagement>
  • <distributionManagement>
  • a subset of the <build> element, which consists of:
    • <defaultGoal>
    • <resources>
    • <testResources>
    • <finalName>
 
激活 Profiles
 
激活 profiles 有下列几种方式:
  • Explicitly
  • Through Maven settings
  • Based on environment variables
  • OS settings
  • Present or missing files
 
1) 通过mvn命令的-P 参数来显示激活 profiles该参数值是 profile id list (之间用逗号连接) 。如:
mvn groupId:artifactId:goal -P profileId-1,profileId-2
 
2)                        通过在 settings.xml 里设置 <activeProfiles> element 来激活(当然 <profiles> 也必须在 settings.xml 里定义)
<settings>
 ...
          <profiles>
 <profile>
    <id>profile1</id>
    ...
 </profile>
          </profiles>
 
 <activeProfiles>
    <activeProfile>profile-1</activeProfile>
 </activeProfiles>
 ...
</settings>
 
            列在<activeProfiles>里的profiles list会在每一个project执行时被激活
 
3) Profiles还可以基于detect到的build environment 的state来自动激活,而不需要象上面2种方式显式激活。这只需要在profile定义时使用<activation> element。如:
<profiles>
 <profile>
    <activation>
      <jdk>1.4</jdk>
    </activation>
    ...
 </profile>
</profiles>
上面的代码表示:如果 JDK version start with 1.4 eg. "1.4.0_08", "1.4.2_07", "1.4" ),该 profile 会被激活
 
<profiles>
 <profile>
    <activation>
      <property>
        <name>debug</name>
      </property>
    </activation>
    ...
 </profile>
</profiles>
上面的代码表示:如果存在system propertie “debug”,该profile会被激活。为了激活它,输入的命令类似于:
mvn groupId:artifactId:goal –Ddebug
 
<profiles>
 <profile>
    <activation>
      <property>
        <name>environment</name>
        <value>test</value>
      </property>
    </activation>
    ...
 </profile>
</profiles>
上面的代码表示:如果存在system propertie “environment”的值为test,该profile会被激活。为了激活它,输入的命令类似于:
mvn groupId:artifactId:goal -Denvironment=test

4) Profiles 还可以基于 OS setting 来自动激活
<profiles>
 <profile>
    <activation>
      <os>
      <name>Windows XP</name>
      <family>Windows</family>
      <arch>x86</arch>
      <version>5.1.2600</version>
    </os>
        </activation>
 ...
 </profile>
</profiles>
            上面的代码表示:如果OS为windows xp,该profile会被激活
 
5) 根据某个 file 不存在而激活 profile 。例如下面定义的profile是在target/generated-sources/axistools/wsdl2java/org/apache/maven 不存在时激活
<profiles>
 <profile>
    <activation>
      <file>
        <missing>target/generated-sources/axistools/wsdl2java/org/apache/maven</missing>
      </file>
    </activation>
    ...
 </profile>
</profiles>
 
 
使用 Profiles 时要注意的 2 个问题
 
第一、 external properties
 
不是定义在pom.xml里的properties都称为external properties。举例说明最明了:
pom.xml
<project>
 ...
 <build>
    <plugins>
      <plugin>
        <groupId>org.myco.plugins</groupId>
        <artifactId>spiffy-integrationTest-plugin</artifactId>
        <version>1.0</version>
        <configuration>
          <appserverHome>${appserver.home} </appserverHome>
        </configuration>
      </plugin>
      ...
    </plugins>
 </build>
 ...
</project>
 
~/.m2/settings.xml
<settings>
 ...
 <profiles>
    <profile>
      <id>appserverConfig</id>
      <properties>
        <appserver.home>/path/to/appserver</appserver.home>
      </properties>
    </profile>
 </profiles>
 
 <activeProfiles>
    <activeProfile>appserverConfig</activeProfile>
 </activeProfiles>
 ...
</settings>
 
当你执行该 pom 时,运行正常。但如果 another user 执行时,则运行失败,因为无法解析 ${appserver.home} (这是由于该 properties 是定义在 user 级别的 settings.xml )。
 
解决方法 就是把该profile放到pom.xml里定义,但这样做的缺点是所有使用该 profile的pom.xml每个都要定义一次该profile。
 
最好的解决方法 是:Since Maven provides good support for project inheritance, it's possible to stick this sort of configuration in the pluginManagement section of a team-level POM or similar, and simply inherit the paths
 
 
第二、 pom.xml 里定义的 profiles 不符合激活条件
依然是举个例子:
pom.xml
<project>
 ...
 <profiles>
    <profile>
      <id>appserverConfig-dev </id>
      <activation>
        <property>
          <name>env</name>
          <value> dev </value>
        </property>
      </activation>
      <properties>
        <appserver.home>/path/to/dev/appserver</appserver.home>
      </properties>
    </profile>
 
    <profile>
      <id>appserverConfig-dev-2 </id>
      <activation>
        <property>
          <name>env</name>
          <value> dev-2 </value>
        </property>
      </activation>
      <properties>
        <appserver.home>/path/to/dev/appserver2</appserver.home>
      </properties>
    </profile>
 </profiles>
 
 <build>
    <plugins>
      <plugin>
        <groupId>org.myco.plugins</groupId>
        <artifactId>spiffy-integrationTest-plugin</artifactId>
        <version>1.0</version>
        <configuration>
          <appserverHome>${appserver.home} </appserverHome>
        </configuration>
      </plugin>
      ...
    </plugins>
 </build>
 ...
</project>
 
上面定义的pom.xml定义了两个profile:不同的”env”参数值会激活不同的profile。当执行命令:
mvn -Denv=dev-2 integration-test
就会激活profile “appserverConfig-dev-2
 
当执行命令:
mvn -Denv=dev integration-test
就会激活profile “appserverConfig-dev
 
而当执行命令:
mvn -Denv=production integration-test
则运行失败,因为没有激活任何一个profile,因此无法解析${appserver.home}。
 
 
查看 build time 过程中使用了哪些 Profiles
执行help pluginactive-profiles goal ,使用命令:
            mvn help:active-profiles
 
例子:
对于上面的例子,如果输入命令:
            mvn help:active-profiles -Denv=dev
则输出的是:
The following profiles are active:
 
 - appserverConfig-dev (source: pom)
 
如果有一个profile定义在settings.xml里并使用<activeProfile>激活,那么输入命令:
            mvn help:active-profiles
则输出的是:
The following profiles are active:
 
 - appserverConfig (source: settings.xml)
 
 
如果输入命令:
            mvn help:active-profiles -P appserverConfig-dev
那么输出的是:
The following profiles are active:
 
 - appserverConfig-dev (source: pom)
 - appserverConfig (source: settings.xml)
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics