每个项目都会有多套运行环境(开发,测试,正式等等),不同的环境配置也不尽相同(如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. 想学spark但是没有集群也没有数据?没关系,我来教你白嫖一个!

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是spark专题的第六篇文章,这篇文章会介绍一个免费的spark平台,我们可以基于这个平台做一些学习实验. databricks 今天要 ...

  2. vue端口号被占用

    今天在启动一个Vue项目的时候,遇到了一个问题. 得知是Vue项目端口号占用的问题.   解决方法: 换一个端口号. 在调用  npm run dev 的时候,实际上是在调用根目录下的 package ...

  3. 【JUC】CountDownLatch和Java枚举的使用例子

    public enum CountryEnum { ONE(1,"春"), TWO(2,"夏"), THREE(3,"秋"), FOUR(4 ...

  4. 项目readme文件目录生成工具 treer

    生成目录的工具呢有tree和treer,但是tree不知道怎么忽略node_modules文件夹, 而treer就简单了,下面就是基本的命令了 其中-i是指忽略xxx, -e是指导出 安装 npm i ...

  5. 引入mybatis-plus报 Invalid bound statement错误怎么办,动动手指改一个地方就行

    错误 Mybatis-Plus (简称MP) 是mybatis的一个增强工具,在mybatis的基础上只做增强不做改变,简化了开发效率.其实就是帮我们封装了一些简单的curd方法,可以直接调用,不必再 ...

  6. 上古神器vim系列之移动三板斧

    [导读] 前文总结了vim如何进入,如何保存退出,如何进入编辑模式.本文来总结一些稍微进阶的内容,在normal模式下如何高效的浏览代码. 模式回顾 在normal模式下主要用于浏览代码,那么有哪些方 ...

  7. [SD心灵鸡汤]005.每月一则 - 2015.09

    精彩的激励人生的话都是从成功者嘴里说出来的,他们成功的过程我们仅仅知道一二,结果却是名满天下.既然看这个激励语,就是想要成功的人,所以大家好好读懂,然后执行,那么你离成功就不远了! 1.万里寻山历百艰 ...

  8. while与do-while循环的使用

    /* 1.格式: ①初始化条件 while(②循环条件){ ④循环体 ③迭代条件 } 2.for循环与while可以相互转换的 . */ public class TestWhile { //100以 ...

  9. 01 . Squid原理配置和使用

    Squid简介 Squid是一个支持HTTP,HTTPS,FTP等服务的Web缓存代理软件,它可以通过缓存页面来提高服务器的相应速度并降低带宽占用.并且,Squid还具有强大的访问控制功能.Squid ...

  10. Rocket - decode - Inst Decode

    https://mp.weixin.qq.com/s/WvepB3yAzjMbQalO3Z82pQ   介绍RocketChip Instruction解码逻辑的实现.   1. RISC-V   R ...