How to access the values ​​defined in the profiles (in pom.xml) via the java code

advertisements

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


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>