11

How do I get the Maven version of my project programatically?

In other words:

static public String getVersion()
{
    ...what goes here?...
}

For example, if my project would generate the jar CalculatorApp-1.2.3.jar, I want getVersion() to return 1.2.3.

Kevin Wong
  • 14,656
  • 11
  • 42
  • 52
  • 1
    Do you mean in a plugin, or in the app itself? – Dave Newton Jan 06 '12 at 21:09
  • What do you intend to do with the maven version? If the need is to like include the info in some file during the buil process then you can utilize the [build-helper-maven-plugin](http://mojo.codehaus.org/build-helper-maven-plugin/maven-version-mojo.html) that will give you the maven version. – CoolBeans Jan 06 '12 at 21:11
  • I may have misconstrued your question. If you want to get the version of your project (not the version of maven like my earlier comment) then take a peek [here](http://blog.nigelsim.org/2011/08/31/programmatically-getting-the-maven-version-of-your-project/). – CoolBeans Jan 06 '12 at 21:13
  • In the app itself. As an example, I want to show the version in an About box. – Kevin Wong Jan 06 '12 at 21:15
  • 2
    this is covered in: http://stackoverflow.com/questions/3697449/retrieve-version-from-maven-pom-xml-in-code – Mason Bryant Jan 06 '12 at 21:15
  • @KevinWong - Yup, my second link was the correct one. – CoolBeans Jan 06 '12 at 21:16
  • @MasonBryant - good catch. Voting to close it as duplicate. – CoolBeans Jan 06 '12 at 21:17
  • 1
    Sorry, I did several searches but didn't find that one. Also voting to close as dup. – Kevin Wong Jan 06 '12 at 21:21

2 Answers2

19

Create file version.prop in src/main/resources with the following contents:

version=${project.version}

Add the following to your project's pom:

<build>
...
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/version.prop</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>false</filtering>
            <excludes>
                <exclude>**/version.prop</exclude>
            </excludes>
        </resource>
    </resources>
...
</build>

Add the following method:

public String getVersion()
{
    String path = "/version.prop";
    InputStream stream = getClass().class.getResourceAsStream(path);
    if (stream == null)
        return "UNKNOWN";
    Properties props = new Properties();
    try {
        props.load(stream);
        stream.close();
        return (String) props.get("version");
    } catch (IOException e) {
        return "UNKNOWN";
    }
}

p.s. Found most of this solution here: http://blog.nigelsim.org/2011/08/31/programmatically-getting-the-maven-version-of-your-project/#comment-124

Kevin Wong
  • 14,656
  • 11
  • 42
  • 52
  • Why the 2nd resource definition with filtering set to false? – demaniak May 29 '15 at 13:08
  • @demaniak the first copies just the version.properties and filters it, the second copies everthing but the version.properties and does no filtering. – pillingworth Dec 04 '15 at 14:31
  • Probably worth mentioning that this works only when you are using the maven-resources-plugin, which should be configured in the pom.xml build - plugins section. – simon Sep 26 '21 at 11:29
1

For jar files, you have the MANIFEST.MF as the default place to put the Implementation-Version there. Maven supports building the jar file like this.

See also How do I add an Implementation-Version value to a jar manifest using Maven?

Frischling
  • 2,100
  • 14
  • 34