Using variables in pom.xml

前端 未结 1 1657
猫巷女王i
猫巷女王i 2021-01-13 19:46

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.

相关标签:
1条回答
  • 2021-01-13 20:27

    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>
    

    Notice that the Prod profile has been tagged: <activeByDefault>.

    Step 2:

    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}
    

    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>
    

    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.

    0 讨论(0)
提交回复
热议问题