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](http://stackoverflow.com/questions/2712970/how-to-get-maven-artifact-version-at-runtime/2713013#2713013). – Joachim Sauer Jul 16 '12 at 08:31
-
1There is a lot of post in Stack Overflow about this : http://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 Revy Jul 17 '12 at 09:27
-
Thanks guys. I now realize this is really bad practice. Gonna try other approach. – SparedWhisle Jul 18 '12 at 03:01
-
possible duplicate of [Get MavenProject from just the POM.xml - pom parser?](http://stackoverflow.com/questions/4381460/get-mavenproject-from-just-the-pom-xml-pom-parser) – oers Jul 18 '12 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");

- 3,869
- 2
- 39
- 70

- 13,561
- 5
- 60
- 64
-
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. – Nikita Bosik Mar 29 '16 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. – Leif Gruenwoldt Oct 05 '16 at 02:02
-
3
Set up a System variable from Maven and in java use following call
System.getProperty("Key");
-
9This 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. – David Pashley Jul 13 '15 at 06:27
-
@DavidPashley, thats true. In the compile only case, I guess the variable needs to be set from the compiler plugin. – Santosh Jul 14 '15 at 06:32
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

- 179,855
- 19
- 132
- 245

- 1,278
- 8
- 19
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. – Joachim Sauer Jul 16 '12 at 08:31
-
I accidently posted the wrong link, its a stackoverflow post, that should not go away ;) – SWoeste Jul 16 '12 at 08:33
-
But i agree whit you, just a link is a bit unpleasant. So based on the source from the link i added some comments and the method call you are searching for (last line). – SWoeste Jul 16 '12 at 08:41
-
3This is really a very bad practice, just use the maven resource plugin! – Mark Bakker Jul 16 '12 at 08:43
-
1Why should this be bad practice if he wants to access the properties from java code? – SWoeste Jul 16 '12 at 08: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. – SWoeste Jul 16 '12 at 09: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. – Andreas Dolk Jul 16 '12 at 09:04
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.

- 2,325
- 2
- 16
- 34
-
I tried your code and it generate the properties under the path classes, but when I try to retrieve it gives me null – Robs Jan 20 '23 at 08: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

- 1,991
- 20
- 17