Can Ant expand environment variables from a properties file?

白昼怎懂夜的黑 提交于 2020-01-12 13:57:52

问题


I have a question regarding Ant and its treatment of environment variables. To illustrate I have a small sample.

Given the Ant build file test.xml:

<project name="myproj" default="testProps">

    <property environment="env"/>

    <target name="testProps">
            <echo message="${env.MyEnvVar}"/>
            <echo message="${MY_PROPERTY}"/>
    </target>
</project>

And the properties file test.props:

MY_PROPERTY=${env.MyEnvVar}

Now set the environment variable MyEnvVar to some value (foo in my case) and run Ant using this command line:

ant -f test.xml -propertyfile test.props testProps

The output I get is:

[echo] foo
[echo] ${env.MyEnvVar}

What I would like to know is whether there is any way to structure the input properties file such that I get

[echo] foo
[echo] foo

That is, I would like to name an environment variable in the properties file which is replaced within the Ant script. Note - I know how to access environment variables directly (as is done here). What I need to do is make use of a set of Ant scripts that expect one collection of properties in an environment that defines the same properties using different names. Thus the thought of "bridging" them in a properties file.

I am using Ant version 1.6.5.


回答1:


You need to read the test.props property file after the environment - you could do so using another property task, i.e. add

<property file="test.props" />

after your existing property environment task.

In full:

<property environment="env" />
<property file="test.props" />

<target name="testProps">
    <echo message="${env.MyEnvVar}"/>
    <echo message="${MY_PROPERTY}"/>
</target>

When you supply the properties file on the command line this gets processed before the content of the build, but at that time ${env.MyEnvVar} is not yet set.



来源:https://stackoverflow.com/questions/3241274/can-ant-expand-environment-variables-from-a-properties-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!