【每日学习】【3】【maven】Profile

51 阅读1分钟

定义profile

可以根据对应的profile 做不同的事情,提供了多种方式激活profile。

maven提供了4中定义方式。

  • 在项目中的pom文件中

  • USER_HOME的settings.xml中(%USER_HOME%/.m2/settings.xml)

  • 配置在全局maven配置中(${maven.home}/conf/settings.xml)

  • 单独的profile描述文件中(profiles.xml)

显示激活

mvn groupId:artifactId:goal -P profile-1,profile-2,?profile-3

或者在settings文件中定义

<settings>
  ...
  <activeProfiles>
  <activeProfile>profile-1</activeProfile>
  </activeProfiles>
  ...
  </settings>

隐式激活

JDK

<profiles>
  <profile>
    <activation>
      <jdk>1.4</jdk><!-- 当jdk为1.4时 激活 -->
    </activation>
  ...
  </profile>
</profiles>

OS

<profiles>
  <profile>
    <activation>
      <os> <!-- 当os为Windows XP x86 5.1.2600 时激活-->
        <name>Windows XP</name>
        <family>Windows</family>
        <arch>x86</arch>
        <version>5.1.2600</version>
      </os>
    </activation>
  ...
  </profile>
</profiles>

Property

<profiles>
  <profile>
    <activation>
      <property> <!-- 当环境变量中带有debug 属性时激活 mvn groupId:artifactId:goal -Ddebug=false 存在value时需要与值匹配才可激活 -->
        <name>debug</name>
      </property>
    </activation>
  ...
  </profile>
</profiles>

Files

<profiles>
    <profile>
        <activation>
            <file><!-- missing为不存在时激活, exists为存在时激活 -->
            <missing>target/generated-sources/axistools/wsdl2java/org/apache/maven</missing>
            </file>
        </activation>
    ...
    </profile>
</profiles>

profile可修改如下内容

  • <repositories>

  • <pluginRepositories>

  • <dependencies>

  • <plugins>

  • <properties> (not actually available in the main POM, but used behind the scenes)

  • <modules>

  • <reports>

  • <reporting>

  • <dependencyManagement>

  • <distributionManagement>

  • a subset of the <build> element, which consists of:

    • <defaultGoal>
    • <resources>
    • <testResources>
    • <directory>
    • <finalName>
    • <filters>
    • <pluginManagement>
    • <plugins>

profile 顺序

可激活的profile有多个时,后定义的profile会被激活。