Accessing maven project properties from Jenkins build jobs

No Comments

Sometimes it is useful to have access to maven project properties like project.artifactId or project.version in a Jenkins job. For example you could calculate the next version number to set in a release job. Another use case I had in one of my projects lately is coping the build results to a remote directory using a shell script. For this to work I needed to inject the artifact’s name into the shell script as parameter. But how to do this if the artifact name constantly chances due to an updated version number in the pom? Luckily Jenkins provides an easy way to access this kind of information.

Mapped maven properties in Jenkins

Jenkins exposes general maven project properties as environment variables. Of corse this only works in maven build jobs, but not in freestyle jobs that execute maven goals. This feature has only been implemented a while ago and surprisingly it is not well documented yet. The following table shows a full list of how maven project properties are mapped to Jenkins environment variables:

maven project propertyJenkins environment variable

Example usage

In my project I’m using a managed script to extract the results of a maven webstart build which are assembled as a zip archive to a remote windows share. Since I’m deploying from trunk and the version number in trunk changes frequently (after each production deployment), I need to determine the name of the archive and pass it to the script. My deploy script basically looks like this:

rm -rf $DEPLOY_PATH/*
unzip target/$1 -d $DEPLOY_PATH

Now the only thing I have to do is to pass the archive’s name as a parameter to the shell script:

If you’re trying this in your project: I had some trouble setting up the mount of the windows share I’m deploying to correctly. Make sure that the user that is running Jenkins has permissions to write to that directory. If you’re running Jenkins as a service, the user will be “jenkins”. The only way I could make it work in my environment was to mount the share using the jenkins user’s uid. This will make the jenkins user to the owner of the mounted directory. You also need a user in the windows domain, that has access to the shared directory:

su root
JENKINS_UID = id -u jenkins
mount -t cifs -o user=<domain-user>,password=<domain-users-pw>,uid=$JENKINS_UID //remote_host/remote_dir /mnt/mount_point

An alternative for using a shell script would be to use the maven-antrun-plugin. But I personally don’t like describing this kind of steps using ant’s xml syntax.

Benedikt Ritter works as a Software Craftsman at codecentric AG in Solingen since September 2013. His joy for creating reliable software is not limited to coding at work: Benedikt is member of the Apache Software Foundation and Committer for the Apache Commons project.

Share on FacebookGoogle+Share on LinkedInTweet about this on TwitterShare on RedditDigg thisShare on StumbleUpon


Your email address will not be published. Required fields are marked *