Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

xml

maven

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

like image 474
DD1 Avatar asked Sep 25 '22 18:09

DD1


1 Answers

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>
like image 102
Jérôme Avatar answered Oct 05 '22 00:10

Jérôme