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.