6

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.

user3293916
  • 61
  • 1
  • 1
  • 4
  • Can give an real example of what you like to do? and best would be to show your full pom file and what you have tried so far and what exactly does not work as you expected. – khmarbaise Feb 19 '14 at 21:57

1 Answers1

10

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.

Sean Mickey
  • 7,618
  • 2
  • 32
  • 58
  • Thanks for the detailed response but is there any other way if I don't want to use profiles? Here is the directory structure: src/main/resources/dev/configservice.properties ; src/main/resources/test/configservice.properties ; src/main/resources/prod/configservice.properties I have a deployUrl variable in each of these .properties file. I am using properties-maven-plugin to get the value of that variable but the issue is how do I distinguish between values in different environments? – user3293916 Feb 20 '14 at 02:58
  • @user3293916, there are multiple options available for what you want. How would you prefer to convey to Maven which of the properties files should be used? A command-line parameter? An environment variable? The contents of a file? If you can give me a response to these questions, I can help you move forward... – Sean Mickey Feb 20 '14 at 17:48
  • Thanks Sean for your help. My first preference would be to use the contents of a file and second would be to use an environment variable. – user3293916 Feb 20 '14 at 17:52
  • @user3293916 Do you still need help with this? I have been sick and in bed; just following up if you still need any assistance - – Sean Mickey Feb 26 '14 at 19:33