Profile



Profile是可以在指定环境通过指定指令或者自动触发,用来替换POM中的一些插件配置或者其他配合。

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.sonatype.mavenbook</groupId>
  <artifactId>simple</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>simple</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <profiles>#
    <profile>
      <id>production</id>#
      <build>#
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
              <debug>false</debug>#
              <optimize>true</optimize>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
</project>



如上情况,
通过命令
mvn clean install -Pproduction -X
来把id=production的profile激活。该方式是通过命令激活,也可以通过环境识别

<project>
  ...
  <profiles>
    <profile>
      <id>dev</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.5</value>
        </property>
        <file>
          <exists>file2.properties</exists>#
          <missing>file1.properties</missing>
        </file>
      </activation>
      ...
    </profile>
  </profiles>
</project>


如上,配置了自动激活条件activation
JDK是1.5,X86,系统WINDOWS XP等等条件才能自动触发。


属性判断激活
<project>
  ...
  <profiles>
    <profile>
      <id>development</id>
      <activation>
        <property>
          <name>!environment.type</name>
        </property>
      </activation>
    </profile>
  </profiles>
</project>



改定义environment.type不存在触发。
外部Profile

外部Profile

如果你开始大量使用Maven profile,你会希望将profile从POM中分离,使用一个单独 的文件如profiles.xml。你可以混合使用定义在pom.xml中和外部profiles.xml文件中 的profile。


Settings Profile
比如为每个Maven构建添加一个需要访 问的内部仓库。你可以使用一个settings profile做这件事情。

<settings>
  <profiles>
    <profile>
      <id>dev</id>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <executions>
           <execution>
              <goals>
                 <goal>sign</goal>
              </goals>
           </execution>
        </executions>
        <configuration>
           <keystore>/home/tobrien/java/keystore</keystore>
           <alias>tobrien</alias>
           <storepass>s3cr3tp@ssw0rd</storepass>
           <signedjar>
         /usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/content-zh/target/signed/book.jar
           </signedjar>
           <verify>true</verify>
        </configuration>
      </plugin>
    </profile>
  </profiles>
</settings>



建议,profile的使用,应该建立在优先考虑移植性,如果你的profile配置后,会造成环境迁移更加麻烦,那么该profile就显得多余了

猜你喜欢

转载自liyixing1.iteye.com/blog/2197053