在pom.xml中使用变量

use*_*916 3 variables pom.xml maven

我想根据环境使用属性文件中具有不同值的变量.我想在我的pom.xml中使用该变量.

Sea*_*key 7

您正在寻找Maven资源过滤

使用资源过滤时需要遵循以下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>
Run Code Online (Sandbox Code Playgroud)

请注意,该Prod配置文件已被标记:<activeByDefault>.

第2步:

在属性文件中,使用pom样式的变量分界来添加变量值占位符,匹配<property>pom中使用的标记名称:

proxyServer=${proxyServer}
proxyPort=${proxyPort}
Run Code Online (Sandbox Code Playgroud)

第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>
Run Code Online (Sandbox Code Playgroud)

然后,当您运行Maven构建时,划分的属性值将替换为pom <profile>条目中定义的值.