资源配置
资源过滤
资源过滤(Resource Filtering) 是 Maven 在复制资源文件时,自动替换文件中的变量 的一种机制。Maven在构建过程中,会将资源复制到target/classes。如果开启 资源过滤,Maven 会在复制时:
- 扫描文件中的
${变量} - 在
pom.xml中查找变量值 - 替换成真实内容
- 在
build中配置资源过滤:
xml
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>maven-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdbc.username>root</jdbc.username>
<jdbc.password>123456</jdbc.password>
</properties>
<modules>
<module>dao</module>
<module>service</module>
<module>controller</module>
</modules>
<build>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>properties
username=${jdbc.username}
password=${jdbc.password}经过资源过滤,配置文件会编译成:
properties
username=root
password=123456请注意测试资源是分开的:
xml
<build>
<testResources>
<testResource>
<directory>${project.basedir}/src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
</build>