How do I access maven properties defined in the pom in a normal maven project, and in a maven plugin project?
-
1What kind of information are you interested in? If it's just versions or similar things, then it's probably a better idea to put it in the manifest and read it from there.– Joachim SauerJul 16, 2012 at 8:31
-
1There is a lot of post in Stack Overflow about this : stackoverflow.com/search?q=read+pom+in+java, at least, take a look before post. SO edit helpers should show them. Google too.– Jean-Rémy RevyJul 17, 2012 at 9:27
-
Thanks guys. I now realize this is really bad practice. Gonna try other approach.– SparedWhisleJul 18, 2012 at 3:01
-
possible duplicate of Get MavenProject from just the POM.xml - pom parser?– oersJul 18, 2012 at 10:03
7 Answers
Use the properties-maven-plugin to write specific pom properties
to a file at compile time, and then read that file at run time.
In your pom.xml:
<properties>
<name>${project.name}</name>
<version>${project.version}</version>
<foo>bar</foo>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>write-project-properties</goal>
</goals>
<configuration>
<outputFile>${project.build.outputDirectory}/my.properties</outputFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
And then in .java:
java.io.InputStream is = this.getClass().getResourceAsStream("my.properties");
java.util.Properties p = new Properties();
p.load(is);
String name = p.getProperty("name");
String version = p.getProperty("version");
String foo = p.getProperty("foo");
-
That worked, but made Eclipse fall into infinite build / deploy-to-Tomcat loop (build -> generate my.properties -> resource changed -> build), so I had to change phase to
compile
. Not sure if it's proper solution though. Mar 29, 2016 at 15:05 -
@atoregozh suggested using Guava Resource's instead for loading the properties file. OK. But reverted change to keep this example minimal with standard API's. Oct 5, 2016 at 2:02
-
2
Set up a System variable from Maven and in java use following call
System.getProperty("Key");
-
8This will only work if you are running the resulting build from Maven, perhaps using the maven-exec-plugin. If you are just using Maven to compile the code, this will not work. Jul 13, 2015 at 6:27
-
@DavidPashley, thats true. In the compile only case, I guess the variable needs to be set from the compiler plugin.– SantoshJul 14, 2015 at 6:32
Maven already has a solution to do what you want:
Get MavenProject from just the POM.xml - pom parser?
btw: first hit at google search ;)
Model model = null;
FileReader reader = null;
MavenXpp3Reader mavenreader = new MavenXpp3Reader();
try {
reader = new FileReader(pomfile); // <-- pomfile is your pom.xml
model = mavenreader.read(reader);
model.setPomFile(pomfile);
}catch(Exception ex){
// do something better here
ex.printStackTrace()
}
MavenProject project = new MavenProject(model);
project.getProperties() // <-- thats what you need
-
1Single-link answers are discouraged here (mostly because they can become useless when the linked-to resource goes away). You should at least summarize the information from the linked to post here. Jul 16, 2012 at 8:31
-
3This is really a very bad practice, just use the maven resource plugin! Jul 16, 2012 at 8:43
-
1Why should this be bad practice if he wants to access the properties from java code?– SWoesteJul 16, 2012 at 8:45
-
2But only if you assume that he wants to/could change his pom(s). I agree whith you that it might be bad to have the properties directly in the pom file instead of an external file. That file could be read with the java properties class - sure. But what in the other case? Then the maven-model is the best solution to do the trick ... but before David Dai dont tell us what exactly he wants to do we all are only guessing.– SWoesteJul 16, 2012 at 9:04
-
1It was my understanding too that we have one mvn pom file (containing properties) and one java application (from where we want to read details of that pom file). Without actually running maven. Jul 16, 2012 at 9:04
This can be done with standard java properties in combination with the maven-resource-plugin
with enabled filtering on properties.
For more info see http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html
This will work for standard maven project as for plugin projects
Update for Leif Gruenwoldt answer:
It's important to use
this.getClass().getClassLoader().getResourceAsStream("maven.properties");
instead of just
this.getClass().getResourceAsStream("maven.properties");
Especially, if you write your maven.properties
file right to project.build.outputDirectory
:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>write-project-properties</goal>
</goals>
<configuration>
<outputFile>${project.build.outputDirectory}/maven.properties</outputFile>
</configuration>
</execution>
</executions>
</plugin>
Explanation is right there.
-
I tried your code and it generate the properties under the path classes, but when I try to retrieve it gives me null– RobsJan 20 at 8:13
I use the build-info goal in spring-boot-maven-plugin:
In my pom.xml:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
...
<execution>
<id>build-info</id>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
<configuration>
...
</configuration>
</plugin>
And in my code:
@Autowired
BuildProperties buildProperties;
...
@GetMapping
public Map<String, String> getVersion() {
return Map.of(
"build.name", buildProperties.getName(),
"build.version", buildProperties.getVersion(),
"build.date", buildProperties.getTime().toString());
}
More information about this plugin goal can be found here: https://docs.spring.io/spring-boot/docs/current/maven-plugin/reference/htmlsingle/#goals-build-info