284

What is the simplest way to retrieve version number from maven's pom.xml in code, i.e., programatically?

Hamedz
  • 639
  • 15
  • 23
mikhail
  • 3,905
  • 7
  • 21
  • 30

12 Answers12

297

Assuming you're using Java, you can:

  1. Create a .properties file in (most commonly) your src/main/resources directory (but in step 4 you could tell it to look elsewhere).

  2. Set the value of some property in your .properties file using the standard Maven property for project version:

    foo.bar=${project.version}
    
  3. In your Java code, load the value from the properties file as a resource from the classpath (google for copious examples of how to do this, but here's an example for starters).

  4. In Maven, enable resource filtering. This will cause Maven to copy that file into your output classes and translate the resource during that copy, interpreting the property. You can find some info here but you mostly just do this in your pom:

    <build>
      <resources>
        <resource>
          <directory>src/main/resources</directory>
          <filtering>true</filtering>
        </resource>
      </resources>   
    </build>
    

You can also get to other standard properties like project.name, project.description, or even arbitrary properties you put in your pom <properties>, etc. Resource filtering, combined with Maven profiles, can give you variable build behavior at build time. When you specify a profile at runtime with -PmyProfile, that can enable properties that then can show up in your build.

Sergey Brunov
  • 11,755
  • 7
  • 39
  • 71
Alex Miller
  • 65,227
  • 26
  • 112
  • 160
  • 2
    I found a code [this](http://stackoverflow.com/questions/2712970/get-maven-artifact-version-at-runtime) that no change Maven config. – Wendel Jul 08 '16 at 14:30
  • 12
    Beware of using filtering directly on `src/main/resources`, as this may process all files located in this directory, including binaries files. To avoid unpredictable behaviours, it's better to do filtering on a `src/main/resources-filtered` directory, [as suggested here](https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html). Anyway, thank you for this nice trick! – SiZiOUS Sep 13 '18 at 12:12
  • 2
    The answer below using the MavenXppReader to get the actual model is really useful, as it doesn't need to run anything to find the value. In cases where you need to know the version before anything is run, look at the answers below; it was very helpful for me to let gradle know what version a checked out maven project has, so I could know the output jar location beforehand. – Ajax Feb 21 '19 at 22:39
  • Beware that this does not work if using it during unit testing. If you need the project information during unit testing see answer by @kriegaex – ormurin Mar 04 '21 at 08:54
  • From [here](https://stackoverflow.com/questions/36501017/maven-resource-filtering-not-working-because-of-spring-boot-dependency), if you are using spring boot, you need to use `@project.version@` instead of `${project.version}` – Vivere Apr 23 '21 at 21:36
111

The accepted answer may be the best and most stable way to get a version number into an application statically, but does not actually answer the original question: How to retrieve the artifact's version number from pom.xml? Thus, I want to offer an alternative showing how to do it dynamically during runtime:

You can use Maven itself. To be more exact, you can use a Maven library.

<dependency>
  <groupId>org.apache.maven</groupId>
  <artifactId>maven-model</artifactId>
  <version>3.3.9</version>
</dependency>

And then do something like this in Java:

package de.scrum_master.app;

import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

import java.io.FileReader;
import java.io.IOException;

public class Application {
    public static void main(String[] args) throws IOException, XmlPullParserException {
        MavenXpp3Reader reader = new MavenXpp3Reader();
        Model model = reader.read(new FileReader("pom.xml"));
        System.out.println(model.getId());
        System.out.println(model.getGroupId());
        System.out.println(model.getArtifactId());
        System.out.println(model.getVersion());
    }
}

The console log is as follows:

de.scrum-master.stackoverflow:my-artifact:jar:1.0-SNAPSHOT
de.scrum-master.stackoverflow
my-artifact
1.0-SNAPSHOT

Update 2017-10-31: In order to answer Simon Sobisch's follow-up question I modified the example like this:

package de.scrum_master.app;

import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Application {
  public static void main(String[] args) throws IOException, XmlPullParserException {
    MavenXpp3Reader reader = new MavenXpp3Reader();
    Model model;
    if ((new File("pom.xml")).exists())
      model = reader.read(new FileReader("pom.xml"));
    else
      model = reader.read(
        new InputStreamReader(
          Application.class.getResourceAsStream(
            "/META-INF/maven/de.scrum-master.stackoverflow/aspectj-introduce-method/pom.xml"
          )
        )
      );
    System.out.println(model.getId());
    System.out.println(model.getGroupId());
    System.out.println(model.getArtifactId());
    System.out.println(model.getVersion());
  }
}
kriegaex
  • 50,139
  • 12
  • 92
  • 145
  • 1
    This is nearly identical to what I use and works fine when started from eclipse, but doesn't when started from the normal `package`d jar (dependency classes aren't integrated) and doesn't work when packaged with maven-assembly-plugin `jar-with-dependencies` I get a `java.io.FileNotFoundException: pom.xml` (it is in the final jar as `META-INF/maven/my.package/myapp/pom.xml`) - any hints how to solve this? – Simon Sobisch Oct 28 '17 at 15:36
  • 1
    My solution is meant to work dynamically in development environments, e.g. when used in tests or tools started from IDE or console. The accepted answer to this question shows several ways to package the version number statically into your artifacts. I was not assuming that pom.xml would be available within JARs at all. Nice for you that you have it there, though. Maybe you could just adjust the path when opening the file reader and maybe make it dependent on the classloader situation. I would have to try for myself. Feel free to ask follow-up questions if this does not help. – kriegaex Oct 31 '17 at 12:12
  • 2
    Hi @SimonSobisch, I have just updated my answer so as to show you how to do what you want. But please be aware of the fact that I just did it quick & dirty, I do not particularly like the code with the nested constructors. – kriegaex Oct 31 '17 at 12:52
77

Packaged artifacts contain a META-INF/maven/${groupId}/${artifactId}/pom.properties file which content looks like:

#Generated by Maven
#Sun Feb 21 23:38:24 GMT 2010
version=2.5
groupId=commons-lang
artifactId=commons-lang

Many applications use this file to read the application/jar version at runtime, there is zero setup required.

The only problem with the above approach is that this file is (currently) generated during the package phase and will thus not be present during tests, etc (there is a Jira issue to change this, see MJAR-76). If this is an issue for you, then the approach described by Alex is the way to go.

Pascal Thivent
  • 535,937
  • 127
  • 1,027
  • 1,106
  • 11
    for people looking for an example reading the properties, this post goes over several methods - http://www.javablog.fr/javaspring-get-and-display-the-version-from-manifest-mf.html – chrismarx Nov 26 '14 at 15:15
53

There is also the method described in Easy way to display your apps version number using Maven:

Add this to pom.xml

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>test.App</mainClass>
            <addDefaultImplementationEntries>
              true
            </addDefaultImplementationEntries>
          </manifest>
        </archive>
      </configuration>
    </plugin>
  </plugins>
</build>

Then use this:

App.class.getPackage().getImplementationVersion()

I have found this method to be simpler.

Steffen Opel
  • 61,065
  • 11
  • 183
  • 208
adam.lofts
  • 1,052
  • 9
  • 10
21

If you use mvn packaging such as jar or war, use:

getClass().getPackage().getImplementationVersion()

It reads a property "Implementation-Version" of the generated META-INF/MANIFEST.MF (that is set to the pom.xml's version) in the archive.

electrobabe
  • 961
  • 9
  • 14
21

To complement what @kieste has posted, which I think is the best way to have Maven build informations available in your code if you're using Spring-boot: the documentation at http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready-application-info is very useful.

You just need to activate actuators, and add the properties you need in your application.properties or application.yml

Automatic property expansion using Maven

You can automatically expand info properties from the Maven project using resource filtering. If you use the spring-boot-starter-parent you can then refer to your Maven ‘project properties’ via @..@ placeholders, e.g.

project.artifactId=myproject
project.name=Demo
project.version=X.X.X.X
project.description=Demo project for info endpoint
info.build.artifact=@project.artifactId@
info.build.name=@project.name@
info.build.description=@project.description@
info.build.version=@project.version@
coolnodje
  • 681
  • 1
  • 6
  • 19
  • This answer helped in that I needed to use the @..@ notation to read the properties from maven. Something else is using the ${..} notation and it was conflicting. – Crompy Nov 18 '20 at 02:25
6

Use this Library for the ease of a simple solution. Add to the manifest whatever you need and then query by string.

 System.out.println("JAR was created by " + Manifests.read("Created-By"));

http://manifests.jcabi.com/index.html

yegor256
  • 93,933
  • 106
  • 409
  • 558
feder
  • 1,705
  • 5
  • 23
  • 36
6

Sometimes the Maven command line is sufficient when scripting something related to the project version, e.g. for artifact retrieval via URL from a repository:

mvn help:evaluate -Dexpression=project.version -q -DforceStdout

Usage example:

VERSION=$( mvn help:evaluate -Dexpression=project.version -q -DforceStdout )
ARTIFACT_ID=$( mvn help:evaluate -Dexpression=project.artifactId -q -DforceStdout )
GROUP_ID_URL=$( mvn help:evaluate -Dexpression=project.groupId -q -DforceStdout | sed -e 's#\.#/#g' )
curl -f -S -O http://REPO-URL/mvn-repos/${GROUP_ID_URL}/${ARTIFACT_ID}/${VERSION}/${ARTIFACT_ID}-${VERSION}.jar
t0r0X
  • 3,058
  • 1
  • 29
  • 26
  • It's fine, just incredibly slow, especially if the artifacts aren't downloaded yet. – ingyhere Sep 02 '20 at 07:09
  • I completely agree with that :-( The only advantage is that it works with every kind of Maven project/module, even those which inherit their version from some parent pom.xml – t0r0X Sep 02 '20 at 07:30
5

When using spring boot, this link might be useful: https://docs.spring.io/spring-boot/docs/2.3.x/reference/html/howto.html#howto-properties-and-configuration

With spring-boot-starter-parent you just need to add the following to your application config file:

# get values from pom.xml
pom.version=@project.version@

After that the value is available like this:

@Value("${pom.version}")
private String pomVersion;
Horst Krause
  • 497
  • 5
  • 16
3
    <build>
            <finalName>${project.artifactId}-${project.version}</finalName>
            <pluginManagement>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-war-plugin</artifactId>
                        <version>3.2.2</version>
                        <configuration>
                            <failOnMissingWebXml>false</failOnMissingWebXml>
                            <archive>
                                <manifest>
                                    <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                                    <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                                </manifest>
                            </archive>
                        </configuration>
                    </plugin>
                 </plugins>
            </pluginManagement>
</build>

Get Version using this.getClass().getPackage().getImplementationVersion()

PS Don't forget to add:

<manifest>
    <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
    <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
ketankk
  • 2,304
  • 1
  • 23
  • 25
0

With reference to ketankk's answer:

Unfortunately, adding this messed with how my application dealt with resources:

<build>
  <resources>
    <resource>
      <directory>src/main/resources</directory>
      <filtering>true</filtering>
    </resource>
  </resources>   
</build>

But using this inside maven-assemble-plugin's < manifest > tag did the trick:

<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>

So I was able to get version using

String version = getClass().getPackage().getImplementationVersion();
Ardent Coder
  • 3,309
  • 9
  • 18
  • 39
Leo Lamas
  • 129
  • 8
0

Step 1: If you are using Spring Boot, your pom.xml should already contain spring-boot-maven-plugin. You just need to add the following configuration.

<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>
</plugin>

It instructs the plugin to execute also build-info goal, which is not run by default. This generates build meta-data about your application, which includes artifact version, build time and more.

Step2: Accessing Build Properties with buildProperties bean. In our case we create a restResource to access to this build info in our webapp

@RestController
@RequestMapping("/api")
public class BuildInfoResource {
    @Autowired
    private BuildProperties buildProperties;

    
    @GetMapping("/build-info")
    public ResponseEntity<Map<String, Object>> getBuildInfo() {
        Map<String, String> buildInfo = new HashMap();
        buildInfo.put("appName", buildProperties.getName());
        buildInfo.put("appArtifactId", buildProperties.getArtifact());
        buildInfo.put("appVersion", buildProperties.getVersion());
        buildInfo.put("appBuildDateTime", buildProperties.getTime());
        return ResponseEntity.ok().body(buldInfo);
    }
}

I hope this will help

Salim Hamidi
  • 18,947
  • 1
  • 20
  • 28