Maven 教程(17)— Maven Profile 和 Filtering 简介
原文地址:https://blog.csdn.net/liupeifeng3514/article/details/79774572
每个项目都会有多套运行环境(开发,测试,正式等等),不同的环境配置也不尽相同(如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>
关于OS值的更多信息可以参考这里
根据JDK版本激活
<!-- 如果jdk的版本为1.8则激活该profile -->
<profiles>
<profile>
<activation>
<jdk>1.8</jdk>
</activation>
</profile>
</profiles>
也可以通过[1.6,1.8)匹配多个jdk版本,关于匹配模式的详细信息可以参考这里
根据环境变量激活
<!-- 如果环境变量里有`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>
Maven 教程(17)— Maven Profile 和 Filtering 简介的更多相关文章
- Maven教程:tutorialspoint-maven
来自turorialspoint的Maven教程(英文),官网:http://www.tutorialspoint.com/maven/index.htm 这个教程在国内已经被翻译成中文,官网:htt ...
- [转]Maven中profile和filtering实现多个环境下的属性过滤
背景 项目构建的时候,需要根据不同的场景来改变项目中的属性资源,最为常见的莫过于数据库连接配置了,试想有生产环境.测试缓存.发布环境等,需要为不同的场景下来动态的改变数据库的连接配置.而使用maven ...
- 利用maven的filter和profile实现不同环境使用不同的配制
在我们平常的java开发中,会经常使用到很多配制文件(xxx.properties,xxx.xml),而当我们在本地开发(dev),测试环境测试(test),线上生产使用(product)时,需要不停 ...
- Maven教程4(私服-nexus)
仓库管理器也叫私服或代理仓库 仓库管理器有两个服务目的:首先它的角色是一个高度可配置的介于你的组织与公开Maven仓库之间的代理,其次它为你的组织提供了一个可部署你组织内部生成的构件的地方. 1Nex ...
- Maven教程2(Eclipse配置及maven项目)
Maven教程1(介绍安装和配置) Eclipse中配置maven 1.Eclipse中默认集成的有Maven 可以使用默认的,本地仓库在当前用户下的.m2文件夹下. 2.配置我们自己安装的maven ...
- Maven 教程(19)— Maven的六类属性
原文地址:https://blog.csdn.net/liupeifeng3514/article/details/79776666 内置属性 主要有两个常用内置属性:${basedir}项目的根目录 ...
- Maven教程
找了一个很详细的maven教程 收藏下 学习网址:http://www.yiibai.com/maven/
- Maven 教程
Maven 教程 序:几次对Maven 的学习,都因为各种原因 而中途切断了,再一次学习的时候,又不得不重新开始,结果发现 又不记得步骤 又找不到对应的文档.别人写的再好,终究比不过自己亲手实践的得出 ...
- Maven教程(转载)
转载自:http://www.yiibai.com/maven/ Apache Maven是一个软件项目管理和综合工具.基于项目对象模型(POM)的概念,Maven可以从一个中心资料片管理项目构建,报 ...
随机推荐
- sql比较字符串,比的到底是什么?
sql里有nvarchar类型的日期数据time1:2019-10-09 00:00:00, 现给定string类型日期time2:2019-10-01 23:59:59,比较两个日期的大小, 发现可 ...
- OpenGL笔记(4)纹理
纹理单元的理解 Shader中要用到纹理: uniform sampler2D texture1; Main读取图片数据,创建了纹理: unsigned int texture1; 怎么把c++里加载 ...
- nginx配置ssl证书实现https
一,环境说明 服务器系统:centos6 服务器IP地址:0.0.0.0 域名:bjubi.com 二,域名解析到服务器 在阿里云控制台-产品与服务-云解析DNS-找到需要解析的域名点“解析”,进入解 ...
- Mybatis中的缓存管理
目录 Mybatis中的缓存管理 查询缓存工作原理: 配置缓存: 默认配置: 使用二级缓存: 刷新缓存过程: 配置EHcache 产生脏数据 使用原则: Mybatis中的缓存管理 查询缓存工作原理: ...
- English--倒装句
English|倒装句 这一块主要进行英语中倒装句与强调句的透析,希望大家可以掌握倒装句.因为倒装句,实在是太常见了,加油哦~~ 前言 目前所有的文章思想格式都是:知识+情感. 知识:对于所有的知识点 ...
- vue项目进入mui.js报错 typeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode .....
在做项目时,引用mui报错如下 看样子就是不支持严格模式吧. 解决方法是在根目录 下的.babelrc文件里面添加过滤掉.不用严格模式
- FPM十一:点击POPUP显示明细
沿接着前面的Search和List.在LIST中点击一列,弹出窗口显示明细. 1.list中定义事件: METHOD if_fpm_guibb_list~get_definition. DATA:gt ...
- html5表单上传控件Files筛选指定格式的文件:accept属性过滤excel文件
摘自:http://blog.csdn.net/jyy_12/article/details/9851349 (IE9及以下不支持下面这些功能,其它浏览器最新版本均已支持.) 1.允许上传文件数量 允 ...
- Linux的yum管理
前面介绍了软件的管理的方式rpm.但有个缺点,rpm不能解决依赖. 下面介绍的yum软件管理.可以完美的解决这个问题. 使用yum的方式管理rpm软件 优势:自动解决软件的依赖关系 ...
- oracle 导入导出表
imp username/pwd@orcl file=c:\temp\exp.dmp tables=(table1, table2)#imp username/pwd@ip:1521/orcl ful ...