maven的profiles节点

来源:互联网 发布:马歇尔 知乎 编辑:程序博客网 时间:2024/05/20 04:12

  profile起的作用是根据不同的环境条件更改pom文件

profiles可以包含不同的配置,在build时修改pom.xml,例如开发环境,测试环境,生产环境

明确的命令行,maven settings,Environment specific,os settings,present or missing files均可触发某些profile

profile中可以用pom元素的一个子集,再加一个额外的元素。可以定义在pom.xml中,可以定义在%USER_HOME%/.m2/settings.xml中,可以定义在${maven.home}/conf/settings.xml

 

profile的触发

-P 参数可以触发,以逗号分割

mvn install -P profile-1,profile-2

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

//下面这两个不会被触发

mvn install -P !profile-1,!profile-2  

-P 命令执行时activation configuration<activeProfiles>中的一并会触发

 

 

通过settings.xml中的profiles节点和activeProfiles来触发,每个包含一个profile id

<settings>

...

<activeProfiles>

<activeProfile>profile-1</activeProfile>

<activeProfile>profile-2</activeProfile>

</activeProfiles>

...

</settings>

 

通过profile元素节点的<activation> section来激活。 可以根据jdk版本,system属性的存在,或者属性的特定值.

<profile>

<activation>

<jdk>1.4</jdk> //jdk1.4 才匹配

    <jdk>[1.3,1.6)</jdk>  //可加开闭区间匹配

</activation>

...

</profile>

 

<activation>

      <os>

        <name>Windows XP</name>

        <family>Windows</family>

        <arch>x86</arch>

        <version>5.1.2600</version>

      </os>

</activation>

 

//debug有值时,无论是何值均激活

 <activation>

      <property>

        <name>debug</name>

      </property>

    </activation>

 

//未设置debug

 <activation>

      <property>

        <name>!debug</name>

      </property>

    </activation>

//未定义,或者定义了一个不是true的值

mvn groupId:artifactId:goal

mvn groupId:artifactId:goal -Ddebug=false

<activation>

      <property>

        <name>debug</name>

        <value>!true</value>

      </property>

</activation>

 

maven3.0开始,pom中的profiles可以被settings.xml中的active profiles中的properties定义的system property激活

 

profile可以更改的内容

settings.xml中的profile仅可以更改<repositories> <pluginRepositories>节点和<properties>节点

可以在pom中以${profile.provided.path}来引用provided profile中的path变量的值

 

pom 可以更改的值

<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>

发现激活的profile

  mvn help:active-profiles

  mvn help:active-profiles -Denv=dev

  mvn help:active-profiles -P appserverConfig-dev

mvn help:effective-pom -P appserverConfig-dev

profile 依环境选择配置

<profile>  

        <id>development</id>  

        <properties>  

            <profiles.active>development</profiles.active>  

            <deploy.url>http://host:port/manager/text</deploy.url>  

        </properties>  

        <activation>  

            <activeByDefault>true</activeByDefault>  

        </activation>  

    </profile>  

    <profile>  

        <!-- 测试环境 -->  

        <id>test</id>  

        <properties>  

            <profiles.active>test</profiles.active>  

            <deploy.url>http://host:port/manager/text</deploy.url>  

        </properties>  

    </profile>  

    <profile>  

        <!-- 生产环境 -->  

        <id>production</id>  

        <properties>  

            <profiles.active>production</profiles.active>  

            <deploy.url>http://host:port/manager/text</deploy.url>  

        </properties>  

    </profile>  

  

针对不同的环境,我们定义不同的配置文件,而这些配置文件都做为资源文件放到maven工程的resources目录下,即src/main/resources目录下,且各个环境的配置分别放到相应的目录下,而所有环境都公用的配置,直接放到src/main/resources/base目录下

 

<build>

....

   <resources>

            <resource>

                <directory>src/main/resources/${conf-dir}</directory>

            </resource>

            <resource>

                <directory>src/main/resources/base</directory>

            </resource>

        </resources>

        <testResources>

            <testResource>

                <directory>src/main/resources/${conf-dir}</directory>

            </testResource>

            <testResource>

                <directory>src/main/resources/base</directory>

            </testResource>

            <testResource>

                <directory>src/test/resources</directory>

            </testResource>

        </testResources>

</build>

 

配置tomcat-maven-plugin插件

<plugin>  

    <groupId>org.codehaus.mojo</groupId>  

    <artifactId>tomcat-maven-plugin</artifactId>  

    <version>1.2-SNAPSHOT</version>  

    <configuration>  

        <url>${deploy.url}</url>  

        <server>tomcat</server>  

        <path>/appcontext</path>  

    </configuration>  

</plugin>

 

 

mvn clean package -Pproduction即构建出生产环境需要的war

mvn tomcat:redeploy -Ptest 即发布到测试环境


原创粉丝点击