5

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

DD1
  • 357
  • 4
  • 14
  • 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. – Gaël J Jan 05 '16 at 15:09
  • Is your pom in source control? Do you want credentials (user/secret) in source control? – Andreas Jan 05 '16 at 20:21
  • 1
    [One approach is described in this question](http://stackoverflow.com/q/2469922/521799) – Lukas Eder Jan 05 '16 at 20:34

2 Answers2

4

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>
Jérôme
  • 101
  • 5
  • Thanks for the info, but how the code will access those values? Can you give some example please? – DD1 Jan 05 '16 at 16:41
  • 1
    you can find an example here http://stackoverflow.com/questions/1318347/how-to-use-java-property-files – Jérôme Jan 05 '16 at 16:54
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.

Community
  • 1
  • 1
Vitaliy Borisok
  • 822
  • 3
  • 11
  • 21