[转]Maven中profile和filtering实现多个环境下的属性过滤
背景
项目构建的时候,需要根据不同的场景来改变项目中的属性资源,最为常见的莫过于数据库连接配置了,试想有生产环境、测试缓存、发布环境等,需要为不同的场景下来动态的改变数据库的连接配置。而使用maven就可以帮我们解决这些问题。下面就来分享一下maven中的profile和filtering的属性。
为了便于测试一下功能,需要搭建maven的web项目,具体配置请详见如何用maven创建web项目。
filtering功能
主要用来替换项目中的资源文件(*.xml、*.properties)当中的${...},比如${db.url},那么如果配置了db.url=aaa的话,在项目编译的时候,就会自动的把${db.url}替换为aaa,下面以实例来讲解一下。
创建完maven的web项目后,会看到src/main/resources的目录,在此目录下面创建个“test.properties”,里面随便来上一行,例如Hello ${user.name},好了,接下来修改我们的pom文件,来启动filtering功能。
<span style="font-family:Comic Sans MS;font-size:18px;"><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>testwebProject</groupId>
<artifactId>com.test.web.test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<!--第一种方式,两种方式都需要指定需要编译的目录 -->
<resources>
<resource>
<directory>src/main/resources</directory>
<!--可以在此配置过滤文件 -->
<includes>
<include>**/*.xsd</include>
<include>**/*.properties</include>
</includes>
<!--开启filtering功能 -->
<filtering>true</filtering>
</resource>
</resources> <plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<version>2.5</version>
</configuration>
</plugin>
</plugins>
</build>
</project></span>
然后编译我们的maven项目:
$mvn clean compile -Duser.name=tom
$编译完后,查看输出文件 target/classes/test.properties 的内容,可见原先的 “Hello {user.name}” 已经变成 “Hello Tom”。
上面如果麻烦的话,也可以把filtering用到的变量写在项目得属性段里面,如下面的方式:
<!--也可以配置到外部属性里面 -->
<properties>
<user.name>Lucky</user.name>
<user.age>50</user.age>
</properties>
进行编译,$mvn clean compile,在此查看的话,就会看到属性被替换的效果。
当然了,如果属性比较多的话,那么此时可以把属性单独抽取出来一个*.properties文件来保存,例如我们在pom.xml的同级目录下面创建一个project.properties,里面指明我们的内容
user.name=Lucky
紧接着在修改我们的pom文件,如下:
<span style="font-family:Comic Sans MS;font-size:18px;"><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>testwebProject</groupId>
<artifactId>com.test.web.test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging> <build>
<!--第一种方式,两种方式都需要指定需要编译的目录 -->
<resources>
<resource>
<directory>src/main/resources</directory>
<!--可以在此配置过滤文件 -->
<includes>
<include>**/*.xsd</include>
<include>**/*.properties</include>
</includes>
<!--开启filtering功能 -->
<filtering>true</filtering>
</resource>
</resources> <filters>
<!-- 是以该pom文件路径作为参考 -->
<filter>project.properties</filter>
</filters> <plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<version>2.5</version>
</configuration>
</plugin>
</plugins>
</build>
</project>
</span>
再次执行编译命令的话,就会看到效果。
profile功能
允许在项目文件(pom.xml)里面定义若干个profile段,然后在编译时选择其中的一个用于覆盖项目文件原先的定义。接着上一个例子,如果我们需要为开发环境和生产环境定义不同的 user.name 属性值,则我们在项目目录里创建两个属性文件分别是pre.properties和dev.properties,然后再每个文件里分别写入user.name=lucky和user.name=wangwang,然后在此修改我们的pom文件,修改后如下所示:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>testwebProject</groupId>
<artifactId>com.test.web.test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging> <build>
<!--第一种方式,两种方式都需要指定需要编译的目录 -->
<resources>
<resource>
<directory>src/main/resources</directory>
<!--可以在此配置过滤文件 -->
<includes>
<include>**/*.xsd</include>
<include>**/*.properties</include>
</includes>
<!--开启filtering功能 -->
<filtering>true</filtering>
</resource>
</resources> <profiles>
<profile>
<id>dev</id>
<activation>
<!--默认的编译选项 -->
<activeByDefault>true</activeByDefault>
</activation>
<build>
<filters>
<filter>dev.properties</filter>
</filters>
</build>
</profile> <profile>
<id>pre</id>
<build>
<filters>
<filter>pre.properties</filter>
</filters>
</build>
</profile>
</profiles> <plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<version>2.5</version>
</configuration>
</plugin>
</plugins>
</build>
</project>
在编译项目时,可以使用 -P 参数指定需要使用的 profile 的 id,比如下面命令将会使用 dev profile:
$mvn clean compile -Pdev
如果想使用pre,只需要改为以下即可
$mvn clean compile -Ppre
假如不指定 -P 参数的话,则会使用 activeByDefault=true 的一项(即 dev)。
补充(添加filtering导致二进制文件异常问题)
最近在项目中使用bootstrap时,发现页面中图标显示异常,通过F12调试发现以下告警:
OTS parsing error: Failed to convert WOFF 2.0 font to SFNT
index:1 OTS parsing error: incorrect file size in WOFF header
index:1 OTS parsing error: incorrect entrySelector for table directory
main:1 OTS parsing error: Failed to convert WOFF 2.0 font to SFNT
main:1 OTS parsing error: incorrect file size in WOFF header
main:1 OTS parsing error: incorrect entrySelector for table directory
发现是因为分环境打包,开启filtering功能后,会破坏有二进制内容的文件。 因为项目中的图标引用了一个字库,该字库为二进制文件,打包后被破坏。所以这里我将需要过滤的文件单独列出来,然后其他文件配置成无需过滤。
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>application.yml</include>
<include>app.properties</include>
</includes>
</resource>
<!--以下这个配置很重要,如果不加这个配置,上面2个文件以外的其他文件将不会生成到war中 -->
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<excludes>
<exclude>application.yml</exclude>
<exclude>app.properties</exclude>
</excludes>
</resource>
</resources>
[转]Maven中profile和filtering实现多个环境下的属性过滤的更多相关文章
- Maven里面多环境下的属性过滤(配置)
情景:通常一个项目都为分为开发环境(dev)和测试环境(test)还有正式环境(prod),如果每次一打包都要手动地去更改配置文件,例如数据库连接配置.将会很容易出差错. 解决方案:maven pro ...
- maven中profile的使用
转载:https://blog.csdn.net/java_collect/article/details/83870215 前言 在开发过程中,我们的项目会存在不同的运行环境,比如开发环 ...
- JAVA中调用LevelDB用于Linux和Window环境下快速存储KV结构
一.简介 JAVA中调用LevelDB用于Linux和Window环境下快速存储KV结构 二.依赖 <!-- https://mvnrepository.com/artifact/org.fus ...
- maven中profile的激活方式
1.默认激活 Maven给我们提供了多种不同的profile激活方式.比如我们可以使用-P参数显示的激活一个profile,也可以根据环境条件的设置让它自动激活等. <profile> & ...
- Maven 中maven-assembly-plugin插件的使用笔记 SpringBoot环境
首先创建一个多模块的SpringBoot项目 项目结构 父pom的内容如下: <?xml version="1.0" encoding="UTF-8"?& ...
- Rails4 中 因为secret key 引起在production环境下无法运行
错误信息 Missing `secret_key_base` for 'production' environment, set this value in `config/secrets.yml` ...
- 【maven】Maven根据Profile读取不同配置环境配置文件
开发需求:在日常开发中,我们大多都会有开发环境(dev).测试环境(test).生产环境(product),不同环境的参数肯定不一样,我们需要在打包的时候,不同环境打不同当包,如果手动改,一方面效率低 ...
- Linux环境下配置eclipse,以及创建maven工程
一:maven的安装 1.安装配置maven环境变量 2.验证 二:eclipse的安装 3.解压配置eclipse 4.启动eclipse,必须在虚拟机的eclipse下启动 5.结果 三:修改配置 ...
- 011 Linux环境下配置eclipse,以及创建maven工程
一:maven的安装 1.安装配置maven环境变量 2.验证 二:eclipse的安装 3.解压配置eclipse 4.启动eclipse,必须在虚拟机的eclipse下启动 5.结果 三:修改配置 ...
随机推荐
- ajax乱码解决汇总
ajax乱码解决总结第一,javascript沿用java的字符处理方式,内部是使用unicode来处理所有字符的,第二,utf-8是每个汉字(unicode字符)用3个字节来存储.第三,用utf-8 ...
- Linux文件与目录管理(二)
一.处理目录的常用命令 ls:列出目录 cd:切换目录 pwd:显示当前的目录 mkdir:创建一个新的目录 rmdir:删除一个空的目录 cp:复制文件或者目录 rm:移除文件或者目录 可以使用ma ...
- java并发编程:线程安全管理类--原子操作类--AtomicStampedReference<V>
1.类 AtomicStampedReference<V> AtomicStampedReference 维护带有整数“标志”的对象引用,可以用原子方式对其进行更新. 实现注意事项.通过创 ...
- java并发编程:线程安全管理类--原子操作类--AtomicMarkableReference<V>
1.类 AtomicMarkableReference<V> public class AtomicMarkableReference<V>extends Object Ato ...
- js打开、关闭页面和运行代码那些事
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name ...
- for each...in,for...in, for...of
一.for each ...in explanation: 该语句在对象属性的所有值上迭代指定的变量.对于每个不同的属性,执行指定的语句. 句法: for each (variable in obj ...
- Java——多线程常见面试题
body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...
- 关于C++静态成员函数访问非静态成员变量的问题
静态成员函数不能访问非静态成员,这是因为静态函数属于类而不是属于整个对象,静态函数中的 member可能都没有分配内存.静态成员函数没有隐含的this自变量.所以,它就无法访问自己类的非静态成员 代码 ...
- L1-004 计算摄氏温度
给定一个华氏温度F,本题要求编写程序,计算对应的摄氏温度C.计算公式:C=5×(F−32)/9.题目保证输入与输出均在整型范围内. 输入格式: 输入在一行中给出一个华氏温度. 输出格式: 在一行中按照 ...
- SharePoint 2010 Ribbon with wrong style in Chrome and Safari
When we add custom ribbon to SharePoint 2010, it may display well in IE but not in Chrome and Safari ...