码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-07-17 23:27:00      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:file   includes   res   coding   encoding   oca   tor   xsd   sch   

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

解决方案:maven profiles。maven提供了profiles元素让开发者们可以定义相关的变量的值,然后在打包的时候根据profile的id去选择变量写入配置文件中。

例如:配置文件jdbc.proterties里面的内容:

name=${pom.name}
password=${pom.password}

pom.xml 里面的profiles内容:
<profiles>
<profile>
<id>dev</id>
<properties>
<pom.name>dev</pom.name>
<pom.password>123456</pom.password>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<pom.name>test</pom.name>
<pom.password>1.1</pom.password>
</properties>
</profile>
</profiles>





当使用maven命令打包的时候,在命令后加 -P dev 例如:mvn package -P dev 。这时候maven就会选择profile的id是dev的里面的变量写入到jdbc.properties里面。

大概过程就是这样,下面写写重点:
问题:为什么会写入jdbc.properties文件而不是application.properties或者web.xml文件呢。
答案:因为还需要在pom文件里面的build标签下配置filter标签,内容如下:
<filters>
<filter>src/main/resources/jdbc.properties</filter>
</filters>

还需要配置resource标签,这个标签标明需要被过滤的资源文件,内容如下:

<resources>
        <resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>

这个标签的意思就是说资源文件放在
directory 配置的目录下,并且开启了过滤模式,表示为遇到filters里面的文件的时候就改变里面的el表达式变量。
整个pom文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>dubbo-learn</artifactId>
<groupId>com.learn.dubbo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>maven-profiles</artifactId>
<build>
<filters>
<filter>src/main/resources/jdbc.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<profiles>
<profile>
<id>dev</id>
<properties>
<pom.name>dev</pom.name>
<pom.password>123456</pom.password>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<pom.name>test</pom.name>
<pom.password>1.1</pom.password>
</properties>
</profile>
</profiles>
</project>

注意:filters里面的文件都只能是文件的全名不能是 *.properties这样的格式。否则会报错。如果想要同表达式匹配指定的文件。
可以使用resource里面的include标签,修改不使用filters而使用include后的文件为:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>dubbo-learn</artifactId>
<groupId>com.learn.dubbo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>maven-profiles</artifactId>
<build>
<!-- <filters>
<filter>src/main/resources/application.properties</filter>
</filters>-->
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
<profiles>
<profile>
<id>dev</id>
<properties>
<pom.name>dev</pom.name>
<pom.password>123456</pom.password>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<pom.name>test</pom.name>
<pom.password>123456</pom.password>
</properties>
</profile>
</profiles>
</project>

有机会再讲讲include和exclude的内容。晚安

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

标签:file   includes   res   coding   encoding   oca   tor   xsd   sch   

原文地址:https://www.cnblogs.com/JieWen/p/9326299.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!