Jenkins: How to use a variable from a pre-build shell in the Maven "Goals and options"

You're on the right track here, but missed a third feature of the EnvInject-Plugin: The "Inject environment variables" build step that can inject variables into following build steps based on the result of a script or properties.

We're using the EnvInject plugin just like that; A script sets up a resource and communicates its parameters using properties that are then propagated by the plugin as environment variables.

i.e. setting up a temporary database for the build: Create a database for the build


I had a very similar problem, trying to compute a build version and inject it into the build. After running into all the same issues (not expanding, etc), I used the "Generate environment variables from script" option, which interprets the output as tag=value pairs into Jenkins variables. The script :

  #generate a version code that is high enough to surpass previously published clients
  val=`expr 150000 + $BUILD_NUMBER`
  echo VERSION_CODE=$val

After this, I was able to inject $VERSION_CODE into maven as follows :

  -Dbuild.vercode=${VERSION_CODE}

Hope that works for you.


This issue is caused by a bug in the Jenkins Maven Project Plugin as detailed in this bug report opened on 2012-06-22. The plugin has not yet been fixed as of version 2.1.

A fix has been proposed for the Maven Project Plugin, but has not yet been integrated. Here is the link to the pull request: https://github.com/jenkinsci/maven-plugin/pull/14

If you build the plugin yourself with the pull request patch applied, the variables are injected and made available to the "goals and options" field as expected.

Tags:

Maven

Jenkins