Pass environment variables to Ant build.xml from Jenkins?

21,152

Solution 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.

Solution 2

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"/>

Solution 3

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.

Share:
21,152
user755806
Author by

user755806

Updated on October 26, 2020

Comments

  • user755806
    user755806 over 2 years

    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
    user755806 almost 9 years
    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?
  • neomega almost 9 years
    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}
  • Geddon
    Geddon about 8 years
    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
    Geddon about 8 years
    You need to set the <property environment="env"/> before you can leverage any of the Jenkins variables. I see that now... Thanks.