Reputation: 1440
How do I access maven properties defined in the pom in a normal maven project, and in a maven plugin project?
Upvotes: 97
Views: 185921
Reputation: 2199
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
Upvotes: 0
Reputation: 3454
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.
Upvotes: 9
Reputation: 13957
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");
Upvotes: 93
Reputation: 1348
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
Upvotes: 9
Reputation: 17893
Set up a System variable from Maven and in java use following call
System.getProperty("Key");
Upvotes: 29
Reputation: 1167
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
Upvotes: 7