如何根据测试、开发环境打包不同的参数配置
<project> <profiles> <profile> <!-- 本地开发环境 --> <id>development</id> <properties> <name>zhang san</name> </properties> <!-- 默认使用 --> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <!-- 测试环境 --> <id>test</id> <properties> <name>li si</name> </properties> </profile> </profiles> <build> <resources> <resource> <directory>src/main/resources</directory> <!-- 编译哪些资源文件到jar包中 --> <includes> <include>*.properties</include> </includes> <!-- 排除哪些资源文件到jar包中 --> <excludes> <exclude>*.xml</exclude> </excludes> <!-- 对资源文件中的 ${} 变量是否替换成值 --> <filtering>true</filtering> </resource> </resources> </build> </project>
a=1 b=${name} # 根据maven不同的参数替换不同的值
public static void main(String[] args) throws IOException { Properties pps = new Properties(); pps.load(App1.class.getClassLoader().getResourceAsStream("test.properties")); for(Object key:pps.keySet()) System.out.println(pps.getProperty(key+"")); }
maven clean package -P development # ${name}替换成 zhang san maven clean package -P test # ${name}替换成 li si