在pom.xml中使用变量 [英] Using variables in pom.xml
问题描述
我想使用一个变量,该变量在属性文件中取决于环境. 我想在我的pom.xml中使用该变量.
I want to use a variable which has different values in the properties file depending on the environment. I want to use that variable in my pom.xml.
推荐答案
您正在寻找 Maven资源过滤
使用资源过滤时需要遵循3个步骤:
步骤1:
在pom中添加一组适当的<profile>
条目,并将所需的变量包括在<properties>
的列表中:
You are looking for Maven Resource Filtering
There are 3 steps to follow when using resource filtering:
Step 1:
Add a set of appropriate <profile>
entries in your pom and include the variables you need in a list of <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>
.
Notice that the Prod
profile has been tagged: <activeByDefault>
.
在属性文件中,使用pom样式变量划分来添加变量值占位符,以匹配pom中使用的<property>
标记名称:
Within the properties file, use pom-style variable demarcation to add variable value placeholders, matching the <property>
tag names used in the pom:
proxyServer=${proxyServer}
proxyPort=${proxyPort}
步骤3:
在pom的<build>
部分中,添加一个<resources>
条目(假设您的属性位于 src/main/resources 目录中),包括一个<filtering>
标记,并设置值至:true
:
Step 3:
Within the pom's <build>
section, add a <resources>
entry (assuming that your properties are in the src/main/resources directory), include a <filtering>
tag, and set the value to: true
:
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>settings.properties</include>
</includes>
</resource>
</resources>
然后,当您运行Maven构建时,标出的属性值将替换为pom <profile>
条目中定义的值.
Then, when you run your Maven build, the demarcated property values will be replaced with the values that are defined in the pom <profile>
entries.
这篇关于在pom.xml中使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!