背景
项目构建的时候,需要根据不同的场景来改变项目中的属性资源,最为常见的莫过于数据库连接配置了,试想有生产环境、测试缓存、发布环境等,需要为不同的场景下来动态的改变数据库的连接配置。而使用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实现多个环境下的属性过滤的更多相关文章

  1. Maven里面多环境下的属性过滤(配置)

    情景:通常一个项目都为分为开发环境(dev)和测试环境(test)还有正式环境(prod),如果每次一打包都要手动地去更改配置文件,例如数据库连接配置.将会很容易出差错. 解决方案:maven pro ...

  2. maven中profile的使用

    转载:https://blog.csdn.net/java_collect/article/details/83870215 前言       在开发过程中,我们的项目会存在不同的运行环境,比如开发环 ...

  3. JAVA中调用LevelDB用于Linux和Window环境下快速存储KV结构

    一.简介 JAVA中调用LevelDB用于Linux和Window环境下快速存储KV结构 二.依赖 <!-- https://mvnrepository.com/artifact/org.fus ...

  4. maven中profile的激活方式

    1.默认激活 Maven给我们提供了多种不同的profile激活方式.比如我们可以使用-P参数显示的激活一个profile,也可以根据环境条件的设置让它自动激活等. <profile> & ...

  5. Maven 中maven-assembly-plugin插件的使用笔记 SpringBoot环境

    首先创建一个多模块的SpringBoot项目 项目结构 父pom的内容如下: <?xml version="1.0" encoding="UTF-8"?& ...

  6. Rails4 中 因为secret key 引起在production环境下无法运行

    错误信息 Missing `secret_key_base` for 'production' environment, set this value in `config/secrets.yml` ...

  7. 【maven】Maven根据Profile读取不同配置环境配置文件

    开发需求:在日常开发中,我们大多都会有开发环境(dev).测试环境(test).生产环境(product),不同环境的参数肯定不一样,我们需要在打包的时候,不同环境打不同当包,如果手动改,一方面效率低 ...

  8. Linux环境下配置eclipse,以及创建maven工程

    一:maven的安装 1.安装配置maven环境变量 2.验证 二:eclipse的安装 3.解压配置eclipse 4.启动eclipse,必须在虚拟机的eclipse下启动 5.结果 三:修改配置 ...

  9. 011 Linux环境下配置eclipse,以及创建maven工程

    一:maven的安装 1.安装配置maven环境变量 2.验证 二:eclipse的安装 3.解压配置eclipse 4.启动eclipse,必须在虚拟机的eclipse下启动 5.结果 三:修改配置 ...

随机推荐

  1. Superset

    Superset是一款可自助.可交互,可视化非常不错的产品 Superset is a data exploration platform designed to be visual, intuiti ...

  2. 无法安装Java,以下开关中存在错误:“0”

    无法安装Java,以下开关中存在错误:“0”:. 解决方法:以管理员运行

  3. JavaScript语言简介

    Web程序不论是B/S还是C/S构架,分为客户端程序与服务器端程序两种. ASP.NET是开发服务器端程序的强大工具,但有时为了降低服务器负担与通信流量,这就需要编写能够在客户端执行的程序. 脚本语言 ...

  4. 使用POI导入EXCEL报java.lang.IncompatibleClassChangeError

    使用POI导入xls格式的excel报java.lang.IncompatibleClassChangeError异常,而导入xlsx正常. oracle.apps.fnd.framework.OAE ...

  5. PHP:第三章——PHP中函数的实参多余形参的处理方法

    <?phpheader("Content-Type:text/html;charset=utf-8");//传参的函数/*function F($a){    echo $a ...

  6. PHP:第一章——PHP中的魔术常量

    <?php //__LINE__输出常量所在的行 //echo __LINE__; //2.__FILE__常量返回文件的完整路径和文件名; //echo __FILE__; //3.__DIR ...

  7. java基本类型的大小

    1个字节是8位byte 1字节short 2字节int 4字节long 8字节float 4字节double 8字节char 2字节boolean 1字节======================= ...

  8. RM报表,点击保存,为何每次都显示 另存为的对话框?

    function TRMDesignerForm.FileSave: Boolean; var lSaved: Boolean; lFileName: string; begin Result := ...

  9. Bat脚本:通过端口号查找进程号

    最近在用jenkins做自动化web部署,web服务器是tomcat. 遇到了这样一个问题:在服务器上执行tomcat的shutdown.bat命令可以正常关机,但用jenkins执行shutdown ...

  10. 2014.04.14 Lastpass活动,2年高级版

    点击进入活动地址. 我的邀请地址,^_^