资料内容:
Chapter 2: Access Maven informations in
code
Introduction
It is sometimes useful to get the maven properties, such as the current version, in code. Here are
some ways to to it.
Examples
Getting the version number from within a jar
If you package your application in a jar using the maven-jar-plugin or the maven-assembly-plugin, an
easy way to get the current pom version is to add an entry in the manifest, which is then available
from Java.
The secret is to set the addDefaultImplementationEntries flag to true (and the
addDefaultSpecificationEntries is you also need the artifact id).
jar plugin configuration:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>...</mainClass>
<addDefaultImplementationEntries>
true
</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>