maven - 在 pom.xml 中使用变量

标签 maven variables pom.xml

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

相关文章:

java - Morphia 在保存到 DataStore 时给出 NoSuchMethodError

perl - 比较变量在Perl中的前导零

maven - 读取 POM 时解析错误。原因: Unrecognised tag: 'hibernate.annotations.version'

java - 我的 pom.xml 包含此 maven-formatter-plugin,但未构建。我需要定义格式目标吗?

java - 从 Maven 存储库部署到 destr 服务器

java - 将外部库配置为 Glassfish 模块

java - 如何在 Maven 多模块项目中使用 Spring ROO?

c# - 在 using 语句中使用变量与 SQL Server

c - 从 argv[1] 在 C 中设置字符串变量

hibernate - 为 hibernate 3.6 配置 pom.xml