6

I have a maven project with different profiles set in pom.xml with different values. But I don't know how to access those values set in profile via java code. For example-

My pom.xml:

<profile>
        <id>scaler</id>
        <properties>
            <user>xxxxxxx</user>
            <secret>yyyyyyyy</secret>
            <proxyHost>172.19.17.13</proxyHost>
            <proxyPort>9444</proxyPort>
            <environment>SCALER</environment>
        </properties>
    </profile>

Java code-

String serviceurl = "http://"<proxyhost>":<proxyPort>/";

In the above java code, i want to use proxy host as 172.19.17.13 & port as 9444 as defined in pom.xml but how to access those values from pom?? I will appreciate your help

3
  • 2
    Maven properties are used for compile-time variables, not really for runtime variables. I believe that, in your case, using a properties file would be a better solution. Commented Jan 5, 2016 at 15:09
  • Is your pom in source control? Do you want credentials (user/secret) in source control? Commented Jan 5, 2016 at 20:21
  • 1
    One approach is described in this question Commented Jan 5, 2016 at 20:34

2 Answers 2

5

You should use the maven filtering feature.

http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html

Just add a property file in src/main/resources with some placeholders:

key=${myvalue}

then myvalue should be defined as a property in your pom.xml

Be sure to activate the filter on your resources:

<resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
</resource>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the info, but how the code will access those values? Can you give some example please?
you can find an example here stackoverflow.com/questions/1318347/…
0

I'm not sure it depends on maven profile. You can try to use properties-maven-plugin (or other solution) like it described here. Just to write your properties into file and then use it in java code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.