Set environment variable before executing maven (In a jenkins build)

Martin Nielsen picture Martin Nielsen · Oct 3, 2013 · Viewed 23.3k times · Source

I am setting up a jenkins slave on a machine that is supposed to build a maven project containing some native C code. The machine itself however, is proving to be a problem. The machine is hosting a fairly old program, that uses a lot of environment variables, like JAVA_HOME, that maven needs to function properly. In order to run maven on the slave machine, i need to set these environment variables, before executing maven, or maven, and other dependencies, such as ssh, will not run. I cant just change the variables permanently, so i need a way to change en environment variable before executing the maven build.

I can execute the build manually by setting the environment variables before executing mvn, and the build works like a charm. But i need jenkins to trigger the build automatically.

As i see it i need to either: Configure jenkins to set environment variables before executing maven or Configure maven use specific values instead of certain environment variables (In such a way that jenkins can handle the execution)

Your help is much appreciated.

Answer

Peter Schuetze picture Peter Schuetze · Oct 3, 2013

There are lots of options.

  • Configure the node to set the environment variable (under Node properties, check "environment variables")
  • for tools Jenkins set the necessary environments itself. In the node configuration you can set different paths for tools that are configured in the main configuration (under Node properties, check "Tool Locations"), I believe you don't need a plugin to have this option.
  • Use the EnvInject plugin (the Setenv Plugin is deprecated)
  • Use the Envfile Plugin
  • set the environments variables before the manual launch of Maven. You need to have both in the same build step. (worst solution)
  • create a java/maven configuration that is only valid on that particular node and use it in your job. This is only work if your job only runs on your special node and nowhere else.

I suggest the first or second Option