Coming Up for Air

Maven Project Version from the Command Line

Tuesday, October 30, 2012 |

A friend asked me today how to get a project’s version out of a Maven POM file without having to read and parse it. A quick Google search brought up the answer, which I thought I’d share here.

The short answer is this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
$ mvn help:evaluate -Dexpression=project.version
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building GlassFish Admin REST Service 4.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-help-plugin:2.1.1:evaluate (default-cli) @ rest-service ---
[INFO] No artifact parameter specified, using 'org.glassfish.main.admin:rest-service:jar:4.0-SNAPSHOT' as project.
[INFO]
4.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.468s
[INFO] Finished at: Tue Oct 30 12:11:47 CDT 2012
[INFO] Final Memory: 11M/148M
[INFO] ------------------------------------------------------------------------

There we have the version number for GlassFish’s rest-service module: 4.0-SNAPSHOT. Clearly, though, this isn’t optimal. There’s still all that Maven noise around the value we want. Let’s do this, then:

1
2
$ mvn help:evaluate -Dexpression=project.version | grep -v "^\["
4.0-SNAPSHOT

And there’s our value, nice and clean. You’re probably scripting this, though, so you’d like to capture that value, so, for those not as familiar with bash scripting, here’s how that’s done:

1
2
3
$ VERSION=`mvn help:evaluate -Dexpression=project.version 2>/dev/null| grep -v "^\["`
$ echo $VERSION
4.0-SNAPSHOT

Voila! The value of expression can be, it seems, any valid POM property. I’ve tried project.name, project.description, etc., and they’ve all worked. Even project.dependencies works, though its output might not be as useful to a script.

Search

    Quotes

    Sample quote

    Quote source

    About

    My name is Jason Lee. I am a software developer living in the middle of Oklahoma. I’ve been a professional developer since 1997, using a variety of languages, including Java, Javascript, PHP, Python, Delphi, and even a bit of C#. I currently work for Red Hat on the WildFly/EAP team, where, among other things, I maintain integrations for some MicroProfile specs, OpenTelemetry, Micrometer, Jakarta Faces, and Bean Validation. (Full resume here. LinkedIn profile)

    I am the president of the Oklahoma City JUG, and an occasional speaker at the JUG and a variety of technical conferences.

    On the personal side, I’m active in my church, and enjoy bass guitar, running, fishing, and a variety of martial arts. I’m also married to a beautiful woman, and have two boys, who, thankfully, look like their mother.

    My Links

    Publications