gpt4 book ai didi

maven - 在 pom.xml 中使用变量

转载 作者:行者123 更新时间:2023-12-04 23:51:04 24 4
gpt4 key购买 nike

我想根据环境使用属性文件中具有不同值的变量。
我想在我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com