Access maven properties defined in the pom

JavaMaven

Java Problem Overview


How do I access maven properties defined in the pom in a normal maven project, and in a maven plugin project?

Java Solutions


Solution 1 - Java

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");

Solution 2 - Java

Set up a System variable from Maven and in java use following call

System.getProperty("Key");

Solution 3 - Java

Maven already has a solution to do what you want:

https://stackoverflow.com/questions/4381460/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

Solution 4 - Java

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

Solution 5 - Java

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.

Solution 6 - Java

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

Solution 7 - Java

You can parse the pom file with JDOM (http://www.jdom.org/).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSparedWhisleView Question on Stackoverflow
Solution 1 - JavaLeif GruenwoldtView Answer on Stackoverflow
Solution 2 - JavaSantoshView Answer on Stackoverflow
Solution 3 - JavaSWoesteView Answer on Stackoverflow
Solution 4 - JavaMark BakkerView Answer on Stackoverflow
Solution 5 - JavaVolodya LombrozoView Answer on Stackoverflow
Solution 6 - JavaNaor BarView Answer on Stackoverflow
Solution 7 - Javaalien01View Answer on Stackoverflow