Ant 可以从属性文件中扩展环境变量吗? [英] Can Ant expand environment variables from a properties file?
问题描述
我有一个关于 Ant 及其对环境变量的处理的问题.为了说明,我有一个小样本.
I have a question regarding Ant and its treatment of environment variables. To illustrate I have a small sample.
给定 Ant 构建文件 test.xml:
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>
和属性文件 test.props:
And the properties file test.props:
MY_PROPERTY=${env.MyEnvVar}
现在将环境变量 MyEnvVar
设置为某个值(在我的情况下为 foo)并使用此命令行运行 Ant:
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
我得到的输出是:
[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
也就是说,我想在 Ant 脚本中替换的属性文件中命名一个环境变量.注意 - 我知道如何直接访问环境变量(就像这里所做的那样).我需要做的是使用一组 Ant 脚本,这些脚本期望在使用不同名称定义相同属性的环境中包含一组属性.因此想到了在属性文件中桥接"它们.
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.
我使用的是 Ant 版本 1.6.5.
I am using Ant version 1.6.5.
推荐答案
您需要阅读test.props
属性文件 环境 - 您可以使用另一个property
任务,即添加
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" />
在您现有的属性环境
任务之后.
全文:
<property environment="env" />
<property file="test.props" />
<target name="testProps">
<echo message="${env.MyEnvVar}"/>
<echo message="${MY_PROPERTY}"/>
</target>
当您在命令行上提供属性文件时,它会在构建内容之前处理,但当时 ${env.MyEnvVar}
尚未设置.
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.
这篇关于Ant 可以从属性文件中扩展环境变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!