Wednesday, 3 October 2018

How to get Properties (key,value) from pom.xml in your java file





The scenario was related to put the integration test server credentials such as server url, server url, username and password in pom.xml and to call them in your java class such as Junit test. The test used the credentials and call the web service.




FileReader reader = new FileReader("pom.xml");
MavenXpp3Reader mavenReader = new MavenXpp3Reader();
Model model = mavenReader.read(reader);
Properties mavenProperties = model.getProperties();
String propertyValue = mavenProperties.getProperty("yourProperty");




On internet you may find something additional such as putting the File of POM.xml and setting it into the model and the creation of MavenProject. However, this works fine and returns all the required properties from the pom.xml.


In addition to this, we can also use the above code to fetch other POM objects such as dependencies, project name, version etc.



No comments:

Post a Comment