直接上代码:
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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.habakkuk.demo</groupId>
<artifactId>mvn_filter_demo</artifactId>
<version>1.0-SNAPSHOT</version>
<profiles>
<profile>
<id>dev</id>
<build>
<filters>
<filter>filters/dev.properties</filter>
</filters>
</build>
</profile>
<profile>
<id>pro</id>
<build>
<filters>
<filter>filters/pro.properties</filter>
</filters>
</build>
</profile>
</profiles>
<build>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
</project>
资源目录:
filters 目录是 需要过滤的配置,resource目录里面的配置文件 使用 @name@ 作为占位符。
测试代码:
public class M {
public static void main(String[] args) throws IOException {
Properties properties=new Properties();
properties.load(M.class.getResourceAsStream("/p.properties"));
System.out.println("result:"+properties.getProperty("p.need.change"));
}
}
默认直接运行 并没有替换 , 是因为没有 active 的profile
可以再pom 文件里面 指定 也可以 再 mvn 命令 中指定 -P 参数。
再idea 中可以 直接勾选