Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inject Jenkins environment variable into maven build?

I need to get some of the Jenkins environment variables like BUILD_NUMBER and BUILD_URL and to inject them as variables in my Maven Java build.

I have added this to the pom.xml

<properties>
    <jenkins.buildUrl>${env.BUILD_URL}</jenkins.buildUrl>
</properties>

and while building with just mvn install I'm trying to get the variable by

private static final String JENKINS_BUILD_URL = System.getProperty("jenkins.buildUrl");

but unfortunately the result is null...

What I'm doing wrong guys?

like image 751
Atanas Kanchev Avatar asked Jan 17 '14 14:01

Atanas Kanchev


Video Answer


1 Answers

Guessing you are trying to read this value into your unit tests? Then you would have to configure the environment variables of the surefire plugin:

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-surefire-plugin</artifactId>
   <configuration>
       <environmentVariables>
           <jenkins.buildUrl>${env.BUILD_URL}</jenkins.buildUrl>
       </environmentVariables>
   </configuration>
</plugin>

As stated in this documentation: http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#environmentVariables

Note that it's possible to do the same in other plugin, like the Maven Tomcat Plugin for example.

like image 148
Aurélien Thieriot Avatar answered Sep 24 '22 21:09

Aurélien Thieriot