首页 > 技术文章 > Maven Profile

pixy 2015-12-09 11:23 原文

profile可以让我们定义一系列的配置信息,然后指定其激活条件。这样我们就可以定义多个profile,然后每个profile对应不同的激活条件和配置信息,从而达到不同环境使用不同配置信息的效果。

profile可定义的位置和内容

  • 项目的pom.xml文件 (针对特定项目)
    • repositories
    • pluginRepositories
    • properties
  • ~/.m2/settings.xml (针对特定用户)
    • repositories
    • pluginRepositories
    • properties
  • M2_HOME/conf/settings.xml (全局)
    • repositories
    • pluginRepositories
    • dependencies
    • plugins
    • properties
    • dependencyManagement
    • distributionManagement
    • build/defaultGoal
    • build/resources
    • build/testResources
    • build/finalName
    • ......

示例

<profiles>
   <profile>
        <!-- 本地开发环境 -->
      <id>development</id>
      <properties>
      <myprop.name>dev</myprop.name>      
    </properties>
      <activation>
            <activeByDefault>true</activeByDefault>
      </activation>
    <build>
      <filters>
        <filter>dev.properties</filter>
      </filters>
    </build>
    </profile>
    <profile>
        <!-- 测试环境 -->
        <id>test</id>
        <properties>
            <myprop.name>test</myprop.name>   
        </properties>
     <build>
      <filters>
        <filter>test.properties</filter>
      </filters>
     </build>
    </profile>
    <profile>
        <!-- 生产环境 -->
        <id>production</id>
        <properties> 
            <myprop.name>product</myprop.name> 
        </properties>
     <build>
      <filters>
        <filter>product.properties</filter>
      </filters>
    </build>
    </profile>
</profiles>

 

激活activiation

构建时指定激活的profile

mvn clean package -P profile_id      #激活
mvn clean package -P !profile_id #不激活

 

默认激活

<activation>
  <activeByDefault>true</activeByDefault>
</activation>

 

jdk版本

<activation>
    <jdk>1.6</jdk>
</activation>

可以同时指定多个版本

<jdk>[1.4,1.7)</jdk>  <!--1.4, 1.5, 1.6-->

os

<activation>   
    <os>  
       <name>Windows XP</name>  
       <family>Windows</family>  
       <arch>x86</arch>  
       <version>5.1.2600</version>  
    </os> 
</activation> 

 

属性

<activation>   
    <property>  
       <name>mavenVersion</name>  
       <value>3.3.2</value>  
    </property> 
</activation> 
  • mvn package -Dprop=value  也能激活 
  • 只有<name>没有<value>时,可用任何值激活

 

文件状态

<activation> 
    <file>  
        <exists>file2.properties</exists>  
        <missing>file1.properties</missing>  
    </file>  
</activation>

 

 

maven的settings.xml中配置激活方式

settings.xml位置可以为${M2_HOME}/conf(全局配置)或~/.m2(当前用户配置)。

<settings>
     ......
  <activeProfiles>
    <activeProfile>profile_id</activeProfile>
  </activeProfiles>
</settings> 

 

同时激活的多个profile中相同属性值

根据profile元素定义的先后顺序来进行覆盖取值,而不是activeProfile定义的先后顺序。

 

查看处于激活状态的profile

mvn help:active-profiles

 

 

属性properties

为不同profile定义不同的属性变量或属性值。

<properties>
      <prop_name>prop_value</prop_name>   
</properties>

 

模块modules/module

<modules>
    <module>module-name</module>
</modules>

profile激活时才构建指定模块

 

 

过滤器build/filters/filter

filter的作用是从该元素指定的配置文件中读取变量。如果资源文件配置中启用了过滤( pom文件的project/build/resources/resource元素中包含<filtering>true</filtering> ),那么资源文件中占位符${...}会用filter读取的对应变量替代。

从配置文件中读取变量

<build>
  <filters>
    <filter>test.properties</filter>
  </filters>
</build>

假设test.properties中内容为

### database connection configuration
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GBK
jdbc.username=aaaa
jdbc.password=bbbb

 

 

资源文件的配置

      <resources>
            <!--需要过滤的资源文件-->
            <resource> 
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
            <!--不需要过滤的资源文件-->
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>
        </resources>
        <testResources>
            <testResource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </testResource>
            <testResource>
                <directory>src/main/webapp</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </testResource>
        </testResources>            

如果src/main/resources下某个资源文件包含如下内容,其中的占位符会被替换为filter变量值

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName">
     <value>${jdbc.driverClassName}</value>
  </property>
  <property name="url">
     <value>${jdbc.url}</value>
  </property>
  <property name="username">
     <value>${jdbc.username}</value>
  </property>
  <property name="password">
     <value>${jdbc.password}</value>
  </property>
</bean>

 

 

参考文档

http://haohaoxuexi.iteye.com/category/269897

推荐阅读