maven的过滤资源需要结合maven的2个定义才能实现,分别是:
- profile
- resources
下面分开来做介绍。
profile
profile可以让我们定义一系列的配置信息,然后指定其激活条件。这样我们就可以定义多个profile,然后每个profile对应不同的激活条件和配置信息,从而达到不同环境使用不同配置信息的效果。需要掌握profile的定义以及激活条件。后面结合resources会介绍。
resources
resources是指定maven编译资源文件指定到何处的,例如maven的标准资源目录结构是src/main/resources(这个在超级pom中定义到了),maven进行编译时候就会将resources中的资源文件放到web的WEB-INF/classes下.具体如何和资源目录有关系,后面结合的时候后讲到。
超级pom中定义的resources:
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>${project.basedir}/src/test/resources</directory>
</testResource>
</testResources>
maven标准目录filter
很多互联网项目中,测试环境和线上环境都是分离的,那么为了方便开发测试,maven项目可以在编译时选取不同的配置文件,如何设置呢,看看以下例子?。
例子如下:
我在java/src/resources目录中定义了jdbc.properties文件内容如下:
#dataSource configure
#jdbc.connection.url=jdbc:mysql://localhost:3306/test
#jdbc.connection.username=root
#jdbc.connection.password=123456
jdbc.connection.url=${jdbc.url}
jdbc.connection.username=${jdbc.username}
jdbc.connection.password=${jdbc.password}
- 通过maven编译后再WEB-INF/classes中得到的jdbc.properties文件内容如下:
#dataSource configure
#jdbc.connection.url=jdbc:mysql://localhost:3306/test
#jdbc.connection.username=root
#jdbc.connection.password=123456
jdbc.connection.url=abcd
jdbc.connection.username=cccc
jdbc.connection.password=dddd
具体是怎么做到的呢?属性在使用${}的方式获取,属性值肯定得在pom中定义,这个在项目pom.xml中的定义方式如下:
<profiles>
<!-- 开发/测试环境,默认激活 -->
<profile>
<id>test</id>
<properties>
<jdbc.url>abcd</jdbc.url>
<jdbc.username>cccc</jdbc.username>
<jdbc.password>dddd</jdbc.password>
</properties>
<activation>
<!--默认启用的是dev环境配置 -->
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!-- 生产环境 -->
<profile>
<id>product</id>
<properties>
<env>product</env>
</properties>
</profile>
</profiles>
为了能让profiles中的内容能让resources中的文件使用到,还需要上面说到的resources插件,定义信息如下:
<build>
<finalName>idea-maven-introduce</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
filtering设置为true很关键,不然引用不到profiles中的内容。但是这样做,就算设置好了吗,如何切换不同的属性的呢,还是没能体现到啊
profiles的激活方式:
- 默认激活方式
根据上面的例子,定义了一个
<activation>
<!--默认启用的是dev环境配置 -->
<activeByDefault>true</activeByDefault>
</activation>
这个是默认的激活方式,意思就是你什么都不做,直接使用标准的package打包时候会将该test定义属性能让resources下面的文件获取到。
使用标准的maven目录file进行管理profiles
src/main/java/filters中的文件如下:
aaa.properties
jdbc.url=aaajdbc:mysql://192.168.120.220:3306/testdb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
jdbc.username=aaatestuser
jdbc.password=aaa123456
bbb.properties
jdbc.url=bbbjdbc:mysql://192.168.120.220:3306/testdb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
jdbc.username=bbbtestuser
jdbc.password=bbb123456
file管理配置文件例子1:
pom.xml文件的内容:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lgy</groupId>
<artifactId>idea-maven-introduce</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>idea-maven-introduce Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>idea-maven-introduce</finalName>
<filters> <!-- 指定使用的 filter -->
<filter>src/main/filters/aaa.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
此时去掉了profiles,直接用filters指定要使用的filter,此时,resources中要用到的值都会从aaa.properties.
结合profiles的激活机制能更好的使用filers目录中的内容,pom.xml中的内容如下:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lgy</groupId>
<artifactId>idea-maven-introduce</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>idea-maven-introduce Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<!-- 开发/测试环境,默认激活 -->
<profile>
<id>test</id>
<properties>
<dev.name>aaa</dev.name>
</properties>
<activation>
<!--默认启用的是dev环境配置 -->
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!-- 生产环境 -->
<profile>
<id>product</id>
<properties>
<dev.name>bbb</dev.name>
</properties>
</profile>
</profiles>
<build>
<finalName>idea-maven-introduce</finalName>
<filters> <!-- 指定使用的 filter -->
<filter>src/main/filters/${dev.name}.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
此时默认的激活方式就是profiles中id为test,filters就会去寻找aaa.peroperties中的对应的属性值给resources中的资源文件进行使用!
总结
有关知识点的内容讲解有如下:
- maven profiles标签的使用
- resources 资源标签的使用
- filters 标签的使用