Reficio™ - Reestablish your software!

Getting version of the Maven project in a freestyle Jenkins build

Today, I have had to extract the version of a Maven project that is built as a part of a freestyle Jenkins build. There are some stackoverflow posts describing how to do it, but the implicit prerequisite is that the build is a native Maven build – not a freestyle build. That is not what I wanted.

Below, I enclose a script that I have come up with for a Jenkins freestyle build that contains a Maven project step. You may have to adjust the path to the pom.xml file. It is important that the script is executed as a system Groovy script, otherwise the Jenkins’ jars are not on the classpath.

The script sets an environment variable (MAVEN_VERSION) that may be used later on in the build. Enjoy!