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

Maven Profile的更多相关文章

  1. 项目实现不同环境不同配置文件-maven profile

    最近接触的项目都是在很多地方都落地的项目,需要支持不同的环境使用不同的配置文件.一直以来都以为是人工的去写不同的配置文件,手动的去修改运用的配置文件.感觉自己还是太low呀.maven的使用的还停留在 ...

  2. 使用maven profile实现多环境可移植构建(转自CSDN)

    使用maven profile实现多环境可移植构建 标签: maven profilemaven自动构建maven自动部署maven可移植构建持续集成 2014-04-25 23:37 26905人阅 ...

  3. 通过maven profile 打包指定环境配置

    背景 最近换了个新公司接手了一个老项目,然后比较坑的是这个公司的项目都没有没有做多环境打包配置,每次发布一个环境都要手动的去修改配置文件.今天正好有空就来配置下. 解决这个问题的方式有很多,我这里挑选 ...

  4. maven profile实现多环境配置

    每次项目部署上线都需要手动去修改配置文件(比如数据库配置,或者一个自定义的配置)然后才能打包,很麻烦,网上找到 maven profile可以完成这个工作,记录如下: 环境:eclipse + spr ...

  5. 在eclipse激活maven profile配置

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

  6. CAS (13) —— CAS 使用Maven Profile支持多环境编译

    CAS (13) -- CAS 使用Maven Profile支持多环境编译 摘要 CAS 使用Maven Profile支持多环境编译 版本 tomcat版本: tomcat-8.0.29 jdk版 ...

  7. maven profile 优先级

    maven profile是有优先级别 也就是说在setting.xml的profile优先级比pom中同名的profile高. 可以使用 mvn help:active-profiles 这个命令是 ...

  8. How to activate maven profile inside eclipse

    How to activate maven profile inside eclipse Normally maven is use for project dependency management ...

  9. 记录一次诡异的Maven Profile不生效的问题

    记录一次诡异的Maven Profile不生效的问题 现象 maven 打包之后,复制的 profile对应的resource文件总是不正确的. 即便是加了 mvn clean package -P ...

  10. maven profile的使用

    作为一名程序员,在开发的过程中,经常需要面对不同的运行环境(开发环境.测试环境.生产环境.内网环境.外网环境等等),在不同的环境中,相关的配置一般不一样,比如数据源配置.日志文件配置.以及一些软件运行 ...

随机推荐

  1. Swift3.0语言教程使用路径字符串

    Swift3.0语言教程使用路径字符串 Swift3.0语言教程使用路径字符串,路径其实是字符串的一种,我们称为路径字符串.本小节将讲解如何使用路径字符串. 1.组合路径 开发者可以将数组快速的组合成 ...

  2. JS日期函数

    JS的日期函数有以下几个: getFullYear(); //获取当前年 getMonth(); //获取当前月,需要加1,而且只有一位数字,如果小于10需要前面加0 getDate(); //获取当 ...

  3. 02_Swift2基础之常量和变量+注释+分号

    1. 常量和变量 常量 和 变量 把一个名字(比如 'number' 或者 'welcomeMessage')和一个指定类型的值(比如数字'10'或者字符串 ' "Hello" ' ...

  4. Android 读取蓝牙设备信息开发

    (1)Android手机一般以客户端的角色主动连接SPP协议设备(接上蓝牙模块的数字传感器),连接流程是: 1.使用registerReceiver注册BroadcastReceiver来获取蓝牙状态 ...

  5. Android Intent (可通过URL启动 Activity)

    Intent分为两大类: (1)显性的(Explicit) (2)隐性的(Implicit) 对于隐性意图,在某些时候, 应用程序只是想启动具有某种特征的组件, 并不想和某个特定的组件耦合. 使用In ...

  6. 【BZOJ】1006: [HNOI2008]神奇的国度

    http://www.lydsy.com/JudgeOnline/problem.php?id=1006 题意:在一个弦图中找最少染色数.(n<=10000, m<=1000000) #i ...

  7. Find和FirstOrDefault()有什么区别?

    Find方法和FirstOrDefault方法效果相同,都是返回满足条件的第一个元素,如果没有该元素,则返回null. 那么这两个扩展方法有什么不同? 1)Find方法是.netFramework2. ...

  8. Java EE 学习总结

    1.Java EE WEB 工程项目文件结构 组成:静态HTML页.Servlet.JSP和其他相关的class: 每个组件在WEB应用中都有固定的存放目录. WEB应用的配置信息存放在web.xml ...

  9. [LintCode] Toy Factory 玩具工厂

    Factory is a design pattern in common usage. Please implement a ToyFactory which can generate proper ...

  10. Struts2中的Action类(解耦方式,耦合方式)

    一.解耦方式 特点:对web资源进行了封装,便于单元测试. 实现:ActionContext和接口方式 1.ActionContext 特点:Action执行的上下文对象.保存了执行Action所需要 ...