Maven2之Profiles

Maven2之Profiles

以下内容摘录自http://maven.apache.org/pom.html#Profiles

 

 

Profiles

A new feature of the POM 4.0 is the ability of a project to change settings depending on the environment where it is being built. A profile element contains both an optional activation (a profile trigger) and the set of changes to be made to the POM if that profile has been activated. For example, a project built for a test environment may point to a different database than that of the final deployment. Or dependencies may be pulled from different repositories based upon the JDK version used. The elements of profiles are as follows:

 

POM4.0的新特性之一就是project的设置能够基于编译时的环境而改变。 一个Profile元素包括一个可选的activation(profile的触发者)和一系列用于改变这个POM的设置(当这个profile被触发时)。 例如, 一个在测试环境中编译的project可能会使用不同于最终部署环境的数据库, 或者根据编译时使用的JDK版本的不同而从不同的库中抽取依赖项。 下面列出了profiles的元素:

<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                      http://maven.apache.org/xsd/maven-4.0.0.xsd">
  ...
  <profiles>
    <profile>
      <id>test</id>
      <activation>...</activation>
      <build>...</build>
      <modules>...</modules>
      <repositories>...</repositories>
      <pluginRepositories>...</pluginRepositories>
      <dependencies>...</dependencies>
      <reporting>...</reporting>
      <dependencyManagement>...</dependencyManagement>
      <distributionManagement>...</distributionManagement>
    </profile>
  </profiles>
</project>

 

Activation

Activations are the key of a profile. The power of a profile comes from its ability to modify the basic POM only under certain circumstances. Those circumstances are specified via an activation element.

 

Activations 是profile的关键。 因为profile的魅力在于它能够根据特定的环境来修改基本的POM, 而这些环境正是通过activation元素来指定的。

 

<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                      http://maven.apache.org/xsd/maven-4.0.0.xsd">
  ...
  <profiles>
    <profile>
      <id>test</id>
      <activation>
        <activeByDefault>false</activeByDefault>
        <jdk>1.5</jdk>
        <os>
          <name>Windows XP</name>
          <family>Windows</family>
          <arch>x86</arch>
          <version>5.1.2600</version>
        </os>
        <property>
          <name>mavenVersion</name>
          <value>2.0.3</value>
        </property>
        <file>
          <exists>${basedir}/file2.properties</exists>
          <missing>${basedir}/file1.properties</missing>
        </file>
      </activation>
      ...
    </profile>
  </profiles>
</project>

 

 

Activation occurs when one or more of the specified criteria have been met. When the first positive result is encountered, processing stops and the profile is marked as active.

 

当满足一个或多个指定的条件时, Activation将会生效。 实际上, 当第一个条件满足的时候Activation就不会再继续处理其他条件了, 这时Activation已经生效了,而profile也被激发。

 

  • jdk: activation has a built in, Java-centric check in the jdk element. This will activate if the test is run under a jdk version number that matches the prefix given. In the above example, 1.5.0_06 will match. Ranges are also supported as of Maven 2.1. See the maven-enforcer-plugin for more details about supported ranges.
  • os: The os element can define some operating system specific properties shown above. See the maven-enforcer-plugin for more details about OS values.
  • property: The profile will activate if Maven detects a property (a value which can be dereferenced within the POM by ${name}) of the corresponding name=value pair.
  • file: Finally, a given filename may activate the profile by the existence of a file, or if it is missing.

The activation element is not the only way that a profile may be activated. The settings.xml file's activeProfile element may contain the profile's id. They may also be activated explicitly through the command line via a comma separated list after the -P flag (e.g. -P test).

To see which profile will activate in a certain build, use the maven-help-plugin.

 

我们不仅可以通过activation元素来设置profile的符合条件, 也能够通过其他方法来激发profile。 setting.xml文件中的activeProfile元素可以包含profile的id, 因此profile也能够明确地通过命令行来激发,即通过在-p后的使用逗号分隔的列表(例如 -P test)。

可以通过使用命令maven-help-plugin来查看在某次特定的build中哪个profile会被激发。

 

mvn help:active-profiles

你可能感兴趣的:(apache,jdk,maven,XP,OS)