How to access values ​​specified in profiles (in pom.xml) using java code

I have a maven project with different profiles set in pom.xml with different values. But I do not know how to access the values ​​specified in the profile through java code. For instance -

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 is

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

In the java code above, I want to use the proxy node as 172.19.17.13 and the port as 9444, as defined in pom.xml, but how to access these values ​​from pom ?? I will be grateful for your help.

+5
source share
2 answers

You should use the maven filter function.

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

Just add the properties file to 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> 
+4
source

I'm not sure if this depends on the maven profile. You can try using properties-maven-plugin (or another solution) as described here . Just to write your properties to a file and then use it in Java code.

0
source

Source: https://habr.com/ru/post/1239904/


All Articles