2
votes

I have an external SDK that I need to set the path to in my ant file. Since this could be different in each of our Dev's environments we have it set via environment variable. In the ant file I have done the following:

<project name="myProj">
  <property environment="env"/>
  <property name ="MY_SDK" value="${env.MY_SDK}"/>
  .
  .
  .
  <target name="compile-code">
    <echo>
      1:  ${env.MY_SDK}
      2:  ${env}
    </echo>

    <javac includeantruntime="false" destdir="${CLASSES}" fork="true" debug="on">
      <classpath>
        <pathelement path="${MY_SDK}" />
      </classpath>
    </javac>
  </target>

When I run "ant -f build_java.xml" however this is the output I get for my echo:

compile-java:
  [echo]
  [echo]          1:  ${env.MY_SDK}
  [echo]          2:  ${env}
  [echo]

I also get a bunch of errors not finding anything from my SDK.

I have ensured that my environment variable is set correctly. If I copy it exactly from my .bash_profile and pasted it into the "VALUE" for my property, it works great. So I have a feeling it has something to do with my environment property.

I am somewhat new to ANT so any advice on what I could be doing wrong here would be greatly appreciated. Thanks.

1
I tried that as well. Same result.Cameron Jones

1 Answers

1
votes

The Ant code is correct. If the environment property is unset, the default fallback behaviour is to print the property value without interpolation.

In my environment (Mac OS 10.9) I ran your build.xml and got the results you mentioned above

Then I set a MY_SDK enviroment variable:

$ MY_SDK=/my-sdk
$ echo $MY_SDK
/my-sdk

And ran your code:

$ ant compile-code
Buildfile: /Users/helderdarocha/Desktop/build.xml

compile-code:
     [echo] 
     [echo]       1:  /my-sdk
     [echo]       2:  ${env}
     [echo]     

BUILD FAILED
/Users/helderdarocha/Desktop/build.xml:12: srcdir attribute must be set!

As you can see, the variable was printed correctly.

You can test it. Try printing ${env.PATH} for example.