5

I am using Jenkins as CI. I have an build.xml. Build.xml has code like below.

<property name="environment" value="$environment}" />

How can i pass value to build.xml from Jenkins ? Can i pass it through environment variables?

user755806
  • 6,565
  • 27
  • 106
  • 153

5 Answers5

14

Your environment variables are available via the environment property. In the example below, the environment variable VIEW is printed from the simple hello world ant script via ${env.VIEW}. Change VIEW to the name of the environment variable of interest.

<?xml version="1.0" encoding="UTF-8"?>
<project name="Hello World" default="Hello" basedir=".">
    <property environment="env"/>
    <property name="HelloText" value="Hello"/>
    <target name="Hello">
        <echo>VIEW=${env.VIEW}</echo>
    </target>
</project> 

IMPORTANT! Note that this line is needed in order for env.VIEW to be understood by ant:

<property environment="env"/>
Eric Duminil
  • 52,989
  • 9
  • 71
  • 124
Straff
  • 141
  • 1
  • 4
  • Maybe I am missing something here..But I tried to leverage a Jenkins variable like NODE_NAME or NODE_LABELS in Ant and it did not work. Using ${env.NODE_NAME} – Geddon May 20 '15 at 15:54
  • 2
    You need to set the before you can leverage any of the Jenkins variables. I see that now... Thanks. – Geddon May 20 '15 at 16:20
5

If your ant build dependeds on the property environment:

<property
  name="environment"
  value="a_value_I_edit_just_before_run_ant_default_target_mannually" />

you could configure your Jenkins' job to build with parameter: enter image description here

so Jenkins will log (i.e. on Windows) something like:

cmd.exe /C '"ant.bat ant -file build.xml -Djenkins_environment=myValue && exit %%ERRORLEVEL%%"'

Then you could modify your ant build to check for system property:

<property
  name="manually_edited_environment"
  value="a_value_I_edit_just_before_run_ant_default_target_manually" />

<condition property="environment"
           value="${jenkins_environment}"
           else="${manually_edited_environment}">
    <isset property="jenkins_environment" />
</condition>

if the jenkins_environment property is set then environment gets its value, else environment gets manually_edited_environment's value; the remaining part of your build still depend on the property environment.

If you have trouble in correctly matching the -DpropertyName use the echoproperties ant task to make ant logging all system properties.

taringamberini
  • 2,719
  • 21
  • 29
0

your ant script should look like this:-

<target name="test" >
    <property environment="env" />
    <echo>BUILD_NUMBER: ${env.BUILD_NUMBER}</echo>
</target>
-1

You can set properties with build parameters (ie: myParameter) and get them in an ant script with ${myParameter}.

Just make sure you don't use dots in parameters names in jenkins because there might be problems getting them in the ant script.

As for environment variables, i don't know, sorry.

neomega
  • 712
  • 5
  • 19
  • neomega, "You can set properties with build parameters (ie: myParameter) and get them in an ant script with ${myParameter}." i could not understand it. could you please explain me how to do? – user755806 Jul 18 '14 at 09:17
  • I answered "How can i pass value to build.xml from Jenkins ?". Jenkins adds all of the build parameters of the job to the ant build : `$ ant -file build_jenkins.xml -Dversion=7.1 -DsendFtp=true`. These parameters values are in the ant properties `${version}` and `${sendFtp}` – neomega Jul 18 '14 at 09:59
-1

Considering parametrized build as in the case above e.g you must have set variable called environment in the job and getting its value from user.Now you can refer to its value in build.xml using property name="environment" value="$environment}

benomatis
  • 5,536
  • 7
  • 36
  • 59
abhinav
  • 411
  • 6
  • 7