我想根据环境使用属性文件中具有不同值的变量。
我想在我的 pom.xml 中使用该变量。
最佳答案
您正在寻找Maven Resource Filtering
使用资源过滤时需要遵循 3 个步骤:
第1步:
添加一组合适的<profile>
您的 pom 中的条目,并在 <properties>
列表中包含您需要的变量:
<profile>
<id>Dev</id>
<properties>
<proxyServer>dev.proxy.host</proxyServer>
<proxyPort>1234</proxyPort>
</properties>
</profile>
<profile>
<id>QA</id>
<properties>
<proxyServer>QA.PROXY.NET</proxyServer>
<proxyPort>8888</proxyPort>
</properties>
</profile>
<profile>
<id>Prod</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<proxyServer>PROD.PROXY.NET</proxyServer>
<proxyPort>8080</proxyPort>
</properties>
</profile>
注意
Prod
个人资料已被标记:<activeByDefault>
.第2步:
在属性文件中,使用 pom 样式的变量划分来添加变量值占位符,匹配
<property>
pom中使用的标签名称:proxyServer=${proxyServer}
proxyPort=${proxyPort}
第 3 步:
在 pom 的
<build>
部分,添加 <resources>
条目(假设您的属性位于 src/main/resources 目录中),包括 <filtering>
标签,并将值设置为:true
:<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>settings.properties</include>
</includes>
</resource>
</resources>
然后,当您运行 Maven 构建时,划分的属性值将替换为 pom
<profile>
中定义的值。条目。
关于maven - 在 pom.xml 中使用变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21892905/