每个项目都会有多套运行环境(开发,测试,正式等等),不同的环境配置也不尽相同(如jdbc.url),借助Jenkins和自动部署提供的便利,我们可以把不同环境的配置文件单独抽离出来,打完包后用对应环境的配置文件替换打包后的文件,其实maven已经给我们提供了替换方案:profile + filtering

Filtering

Filtering 是 maven 的 resource 插件 提供的功能,作用是用环境变量、pom文件里定义的属性和指定配置文件里的属性替换属性(*.properties)文件里的占位符(${jdbc.url}),具体使用如下: 
src/main/resources目录有个配置文件jdbc.properties,内容如下:

jdbc.url=${pom.jdbc.url}
jdbc.username=${pom.jdbc.username}
jdbc.passworkd=${pom.jdbc.password}

配置 resource 插件,启用filtering功能并添加属性到pom:

<project>
...
<!-- 用pom里定义的属性做替换 -->
<properties>
<pom.jdbc.url>jdbc:mysql://127.0.0.1:3306/dev</pom.jdbc.url>
<pom.jdbc.username>root</pom.jdbc.username>
<pom.jdbc.password>123456</pom.jdbc.password>
</properties>
<build>
...
<!-- 可以把属性写到文件里,用属性文件里定义的属性做替换 -->
<filters>
<filter>src/main/filters.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
...
</build>
...
</project>

编译包后target目录下的jdbc.properties

jdbc.url=jdbc:mysql://127.0.0.1:3306/dev
jdbc.username=root
jdbc.passworkd=123456

Profile 简介

什么是profile?<profile>就像<dependencies>一样是pom文件里的一个xml元素,在profile里几乎可以定义所有在pom里的定义的内容(<dependencies><properties>,插件配置等等,不过不能再定义他自己了)。当一个profile被激活时,它定义的<dependencies><properties>等就会覆盖掉原pom里定义的相同内容,从而可以通过激活不同的profile来使用不同的配置。

<!-- profile 的感性认识 -->
<project>
...
<profiles>
<profile>
<id>dev</id>
<properties>
<active.profile>dev</active.profile>
<pom.jdbc.url>jdbc:mysql://127.0.0.1:3306/dev</pom.jdbc.url>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependencies>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
</dependencies>
...
</project>

Profile 如何配置

可以在两个位置配置profile:settings.xml 和 pom.xml

  • settings.xml里定义的profile是全局的,对所有的项目都可用,在里面定义的配置项也稍微少了些,只能定义远程服务器的信息和属性信息(<repositories>,<pluginRepositories>,<properties>)。这些信息在pom.xml里也是可以定义的。
  • pom.xml里可以定义的配置如下:
<repositories>
<pluginRepositories>
<dependencies>
<plugins>
<properties>
<modules>
<reporting>
<dependencyManagement>
<distributionManagement> 以及build下的: <defaultGoal>
<resources>
<testResources>
<finalName>

如果profile被激活,profile里的配置和原pom的配置会做覆盖合并。

如何激活Profile

可以通过多种方式激活profile(显式的,隐式的)

显式的激活

通过maven 的-P参数激活指定的profile,参数的值是profile的id,多个profile以逗号分割,如果不想激活某个默认的profile,就在它的id前加个!

mvn -U clean package -Ptest,local,!ignore

IDEA里则可以在 Maven Projects 里直接勾选想要激活的profile

隐式的激活

配置profile时,可以在<profile>的 <activation>元素下配置隐式激活的信息。

默认激活
  • pom.xml文件里
<!-- 默认激活 -->
<profiles>
<profile>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
  • settings.xml文件里则是通过<activeProfiles>来配置默认激活的profile列表
<activeProfiles>
<activeProfile>artifactory</activeProfile>
</activeProfiles>
根据操作系统类型激活
<profiles>
<profile>
<activation>
<os>
<!-- 不必指定所有信息 -->
<name>linux</name>
<family>unix</family>
<arch>amd64</arch>
<version>3.19.0-30-generic</version>
</os>
</activation>
</profile>
</profiles>
根据JDK版本激活
<!-- 如果jdk的版本为1.8则激活该profile -->
<profiles>
<profile>
<activation>
<jdk>1.8</jdk>
</activation>
</profile>
</profiles>
根据环境变量激活
<!-- 如果环境变量里有`debug`且它的值为`true`则激活 -->
<!-- 也可以不指定`<value>`元素, 则只有环境变量里有`debug`就激活 -->
<profiles>
<profile>
<activation>
<property>
<name>debug</name>
<value>true</value>
</property>
</activation>
</profile>
</profiles>
mvn -U clean package -Ddebug=true
通过判断文件是否存在激活
<profiles>
<profile>
<activation>
<file>
<missing>/path/to/missing/file</missing>
<exists>/path/to/exists/file</exists>
</file>
</activation>
...
</profile>
</profiles>

不同类型的隐式激活方式可以组合使用,如根据同时指定根据操作系统类型和JDK版本来激活profile,只有但两个条件都匹配是才激活之。

Filtering + Profile

思路:在不同的profile里配置不同的属性(properties),然后激活相应的profile,用其中的属性去替换jdbc.properties里的占位符。

继续使用介绍Filtering时的例子,现在添加三个profile配置,分别对应开发,测试,正式环境。 
修改后的pom文件如下:

<project>
...
<build>
<filters>
<filter>src/main/filters-${active.profile}.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<profiles>
<profile>
<id>dev</id>
<properties>
<active.profile>dev</active.profile>
</properties>
<!-- 把当前profile设置为默认profile,可以同时这是多个为默认 -->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<active.profile>test</active.profile>
</properties>
</profile>
<profile>
<id>product</id>
<properties>
<active.profile>product</active.profile>
</properties>
</profile>
...
</project>

然后在src/main下新建三个文件:filters-dev.properties,filters-test.properties,filters-product.properties,文件内容如下(以filters-dev.properties为例):

pom.jdbc.url=jdbc:mysql://127.0.0.1:3306/dev
pom.jdbc.username=root
pom.jdbc.password=123456

用 dev profile 打开发包mvn clean package -Pdev, 打包后jdbc.properties文件内容如下:

jdbc.url=jdbc:mysql://127.0.0.1:3306/dev
jdbc.username=root
jdbc.password=123456

如果不同的运行环境只是属性值的不同,用上面的 profile + filtering 进行下变量替换可以很好的满足打包需求,如果不是简单的替换(如log4j.xml,开发环境只要输出到标准输出,测试和线上环境则还需要打到文件且文件的位置和策略也不相同),这个就需要借助maven 的 ant 插件。src/main/resources目录下有三个log4j的配置文件,分别对应三个运行环境:

resources
├── log4j-product.xml
├── log4j-test.xml
└── log4j.xml

配置如下profile:

<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>config.*.properties</exclude>
<exclude>log4j-*.xml</exclude>
</excludes>
</resource>
</resources>
</build>
</profile>
<profile>
<id>test</id>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<delete file="${project.build.outputDirectory}/log4j.xml" />
<delete file="${project.build.outputDirectory}/log4j-product.xml" />
<move file="${project.build.outputDirectory}/log4j-test.xml" tofile="${project.build.outputDirectory}/log4j.xml" />
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>product</id>
<properties>
<active.profile>product</active.profile>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<delete file="${project.build.outputDirectory}/log4j.xml" />
<delete file="${project.build.outputDirectory}/log4j-test.xml" />
<move file="${project.build.outputDirectory}/log4j-product.xml" tofile="${project.build.outputDirectory}/log4j.xml" />
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

原文链接:https://blog.csdn.net/liupeifeng3514/article/details/79774572

(七)Maven Profile 和 Filtering的更多相关文章

  1. Maven 教程(17)— Maven Profile 和 Filtering 简介

    原文地址:https://blog.csdn.net/liupeifeng3514/article/details/79774572 每个项目都会有多套运行环境(开发,测试,正式等等),不同的环境配置 ...

  2. 使用 Maven Profile 和 Filtering 打各种环境的包(转)

    http://tunzao.me/articles/maven-profile/ https://blog.csdn.net/syani/article/details/52237470

  3. [转]Maven中profile和filtering实现多个环境下的属性过滤

    背景 项目构建的时候,需要根据不同的场景来改变项目中的属性资源,最为常见的莫过于数据库连接配置了,试想有生产环境.测试缓存.发布环境等,需要为不同的场景下来动态的改变数据库的连接配置.而使用maven ...

  4. maven profile实现多环境打包

    快速解决: 项目目录 1.pom文件中添加profile <profiles> <profile> <!-- 本地开发环境 --> <id>dev< ...

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

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

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

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

  7. How to activate maven profile inside eclipse

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

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

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

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

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

随机推荐

  1. BZOJ1077 并查集

    1077: [SCOI2008]天平 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 416  Solved: 224[Submit][Status][ ...

  2. UVALive5846

    题目大意:见刘汝佳<算法竞赛入门经典——训练指南>P173. 解题思路: 如果要直接求所有单色三角形的个数似乎不简单,正难则反,先求出所有非单色三角形 cnt,answer = C(n,3 ...

  3. [前端开发]Vue mixin

    两个非常相似的组件,他们的基本功能是一样的,但他们之间又存在着足够的差异性,此时的你就像是来到了一个分岔路口:我是把它拆分成两个不同的组件呢?还是保留为一个组件,然后通过props传值来创造差异性从而 ...

  4. 【数据结构的JavaScript版实现】data-struct-js的npm包初版作成

    [数据结构的JavaScript版实现]data-struct-js的npm包初版作成 码路工人 CoderMonkey [数据结构的JavaScript版实现] 拖了这么久,终于趁着春节假期把初版( ...

  5. VS中自定义代码片段

    VS - 工具 - 代码片段管理器 实现:propnotify 加 Tab 键 生成属性定义代码片段 (包含一个字段定义,一个属性get/set定义,其中set会触发属性值变更事件) <?xml ...

  6. Java IO(四) InputStream 和 OutputStream

    Java IO(四) InputStream 和 OutputStream 一.介绍 InputStream 和 OutputStream 是字节流的超类(父类),都是抽象类,都是通过实例化它们的子类 ...

  7. BZOJ1001 狼抓兔子 题解

    裸的最小割,转化成最大流即可. #include <bits/stdc++.h> int n,m; int S,T; int mincost; int head[6001000],tot= ...

  8. 13 . Python3之并发编程

    什么是操作系统? 为什么要有操作系统? 现代的计算机系统主要是由一个或者多个处理器,主存,硬盘,键盘,鼠标,显示器,打印机,网络接口及其他输入输出设备组成. 一般而言,现代计算机系统是一个复杂的系统. ...

  9. 学习使用pyquery解析器爬小说

    一.背景:个人喜欢在网上看小说,但是,在浏览器中阅读小说不是很方便,喜欢找到小说的txt版下载到手机上阅读,但是有些小说不太好找txt版本,考虑自己从网页上爬一爬,自己搞定小说的txt版本.正好学习一 ...

  10. Rocket - interrupts - Crossing

    https://mp.weixin.qq.com/s/nSX4prXFb4K5GSUhPtOUCg 简单介绍Crossing的实现. 1. IntXing 这是一个LazyModule: 1) 参数 ...