Monday, February 20, 2012

Ant - setting Environment variables in a Hudson/Jenkins build

Today, while trying to setup Jenkins for a project, one of the job includes a ant build for a java project. This build has been working pretty well for some time, but i wanted to automate in inside Jenkins. I encountered a problem when i tried to create the new jenkins job, because the ant build references some environment variables.

The manual build itself was composed of two part, a shell script that set some environment variables, and the build.xml file itself which reference those variables. Obviously depending on different environment, the variables would have different values.

Inside the build file the variables are referenced like this:

And the shell script simply does an "export SOME_DIR=blablabla" before calling ant.

So now the trick is to be able to define those environment variable inside the Jenkins job in such a way that ant would pick it up (without calling the actual shell script). When you setup the job, under Build > Invoke Ant, click on "Advanced" and in the "Properties" textbox, simple define the variable as follow:

And you're good to go.