user3293916
user3293916

Reputation: 81

Using variables in 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.

Upvotes: 8

Views: 38623

Answers (1)

Sean Mickey
Sean Mickey

Reputation: 7726

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.

Upvotes: 13

Related Questions