Dici
Dici

Reputation: 25980

Using environment properties in ant

It's probably a trivial question but I'm puzzled by the use of the environment in Ant. I want to update a target which I know works fine on Jenkins, and I need to test my change locally before pushing to prod. However, Ant does not seem to want to read my environment variables even when I write a trivial target such as :

<target name="test">
    <property environment="env" />
    <echo>
        ${env.user.country}
        ${env.user.language}
        ${env.TARGET}
    </echo>
</target>

which prints

Buildfile: /home/local/ANT/.../Workplace/dra/draModelsConfig/build.xml

test:
     [echo] 
     [echo]             ${env.user.country}
     [echo]             ${env.user.language}
     [echo]             ${env.TARGET}
     [echo]         

BUILD SUCCESSFUL Total time: 0 seconds

I call the target exactly how the Jenkins host logs it in the build log (simplified for this question):

ant -file build.xml -DTARGET=build test

Why doesn't env contain the environment variables as it should ?

Upvotes: 0

Views: 1492

Answers (1)

Chad Nouis
Chad Nouis

Reputation: 7041

In the command:

ant -file build.xml -DTARGET=build test

-DTARGET=build is setting a property named TARGET, not an environment variable.

TARGET can be echo'ed like a regular property:

build.xml

<project name="ant-echo-command-line-prop">
    <echo>My TARGET: ${TARGET}</echo>
</project>

Output

$ ant -DTARGET=build
[echo] My TARGET: build

Upvotes: 1

Related Questions