[转]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.结果 三:修改配置 ...
随机推荐
- 秒杀多线程第七篇 经典线程同步 互斥量Mutex
本文转载于:http://blog.csdn.net/morewindows/article/details/7470936 前面介绍了关键段CS.事件Event在经典线程同步问题中的使用.本篇介绍用 ...
- 高精度乘法,string中的坑
#include "bits/stdc++.h" using namespace std; ]; ]; int main() { while(cin >> a > ...
- overflow属性-摘自网友
关于我们 版权声明 网站地图 前端观察 专注于网站前端设计与前端开发 用IE6抢不到火车票的!!! Home 首页 CSS样式之美 Front News前端资讯 JavascriptAjax与JS技术 ...
- learning uboot part command
=> part --helppart - disk partition related commands Usage:part uuid <interface> <dev> ...
- 安装淘宝cnpm镜像
$ npm install -g cnpm --registry=https://registry.npm.taobao.org
- Nodejs之mssql模块的封装
在nodejs中,mssql模块支持sqlserver数据库操作.今天将mssql模块的某些功能封装为一个类,方便以后调用.封装的功能有执行存储过程,执行查询语句操作等.如果本篇文章对大家有帮助,那就 ...
- 使用word2013写博客
额额 要使用的话首先要配置一下: 选择word2013的创建,然后点击模版,搜索博客. 然后就是创建账户了,账户主要填写的下面这些信息: 注意,cnblogs后面的子域名应该使用你自己的子域名 下面 ...
- L1-023 输出GPLT
给定一个长度不超过10000的.仅由英文字母构成的字符串.请将字符重新调整顺序,按GPLTGPLT....这样的顺序输出,并忽略其它字符.当然,四种字符(不区分大小写)的个数不一定是一样多的,若某种字 ...
- sonarqube 代码检查
再好的程序员也会出bug,所以代码检查很有必要.今天就出一个简单的检查工具代替人工检查. 参考: http://www.cnblogs.com/qiaoyeye/p/5249786.html 环境及版 ...
- js方法参数问题
大家都知道java是强类型语言,而js是弱类型语言,其实,它们之间的区别不止这一点,还有方法参数及调用问题,参看下例: js中:这里定义了一个query()方法 function query() { ...