Netbeans Ant environment variables

2019-03-04 13:43发布

I am having a problem accessing environment variables in a netbeans build.xml ant script. Basically, when I use this code:

<target name="-pre-jar">

    <property environment="env"/>

    <echo message="${env.ANT_HOME}"/>
    <echo message="${env.JAVA_HOME}"/>

</target>

I receive output as follows:

compile:
${env.ANT_HOME}
${env.JAVA_HOME}

This is a problem because I need to access those filepaths later in the script. Also, I am sure that those two are defined as environment variables. Here is some proof from my terminal:

Lukas-Rezeks-MacBook-Pro:ant lukas$ echo $JAVA_HOME - $ANT_HOME
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home - /ant

Am I doing something wrong in the ant build scripts variable declaration, or am I missing something obvious? Any feedback would be appreciated. Thanks.

2条回答
萌系小妹纸
2楼-- · 2019-03-04 14:18

This is completely working for me on my OSX system:

[mike@numbersix ~/temp]$ ant -version
Apache Ant(TM) version 1.8.2 compiled on June 3 2011
[mike@numbersix ~/temp]$ echo $JAVA_HOME - $ANT_HOME
/Library/Java/Home - /usr/share/ant
[mike@numbersix ~/temp]$ cat build.xml 
<project name="blah">
    <target name="foo">
        <property environment="env"/>

        <echo message="${env.ANT_HOME}"/>
        <echo message="${env.JAVA_HOME}"/>
    </target>
</project>
[mike@numbersix ~/temp]$ ant foo
Buildfile: /Users/mike/temp/build.xml

foo:
     [echo] /usr/share/ant
     [echo] /Library/Java/Home

BUILD SUCCESSFUL
Total time: 0 seconds
查看更多
孤傲高冷的网名
3楼-- · 2019-03-04 14:43

You can either put

<property environment="env"/>

outside your target,

OR use "depends=" instead of "antcall" in your compile task.

I did the following test on my Linux:

<target name="pre-jar">
    <property environment="env"/>
    <echo message="${env.ANT_HOME}"/>
    <echo message="${env.JAVA_HOME}"/>
</target>

<target name="compile">
    <antcall target="pre-jar" />
    <echo message="${env.ANT_HOME}"/>
    <echo message="${env.JAVA_HOME}"/>
</target>

and when I ran "ant compile" I got the following:

compile:

pre-jar:
[echo] /opt/apache-ant-1.8.2
[echo] /opt/jdk1.7.0
[echo] ${env.ANT_HOME}
[echo] ${env.JAVA_HOME}

and when I used depends="pre-jar", the result was:

pre-jar:
[echo] /opt/apache-ant-1.8.2
[echo] /opt/jdk1.7.0

compile:
[echo] /opt/apache-ant-1.8.2
[echo] /opt/jdk1.7.0

when I put outside any target, the result was:

compile:

pre-jar:
[echo] /opt/apache-ant-1.8.2
[echo] /opt/jdk1.7.0
[echo] /opt/apache-ant-1.8.2
[echo] /opt/jdk1.7.0

查看更多
登录 后发表回答