简体   繁体   English

在Ant build.properties中添加环境变量

[英]Adding environment variables in Ant build.properties

Is it possible to use $XXX_HOME style variables in a build.properties file? 是否可以在build.properties文件中使用$XXX_HOME样式变量?

I set path of variable in build.properties like this 我像这样在build.properties设置变量的路径

ant.home=/opt/java/myanthome

I have a $ANT_HOME environment variable with the correct path. 我有一个带有正确路径的$ANT_HOME环境变量。 Can I use this environment variable to set my ant.home in b uild.properties ? 我可以使用此环境变量在ant.home中设置uild.properties吗? I tried to add this in build.xml 我试图在build.xml中添加它

<property environment="env"/>
<property file="build.properties" />

and in the build.properties file: 并在build.properties文件中:

ant.home=${env.ANT_HOME}

but this does not work,it says: 但这不起作用,它说:

${env.ANT_HOME} does not exist ${env.ANT_HOME}不存在

It is possible but there is no sense in doing it. 可能,但没有意义。

You can add a filter that pre-processes your property file. 您可以添加用于预处理属性文件的filter Consider the following property file: 考虑以下属性文件:

ant.home=@ANT_HOME@

Then, you can filter it and load the file into properties, like this: 然后,您可以对其进行过滤并将文件加载到属性中,如下所示:

<filter token="ANT_HOME" value="${env.ANT_HOME}"/>
<copy file="build.properties" tofile="build.properties.filtered" filtering="true" />
<property file="build.properties.filtered" />

The filter task, in addition with filtering=true , will replace the @ANT_HOME@ token by the value of ${env.ANT_HOME} . 除具有filtering=true之外, filtering=true器任务还将用${env.ANT_HOME}的值替换@ANT_HOME@令牌。

But after having done all this, the question is, why filter the property file to begin with? 但是在完成所有这些操作之后,问题是,为什么要过滤属性文件呢? You can just remove the ant.home property from your file and directly use ${env.ANT_HOME} where necessary. 您可以仅从文件中删除ant.home属性,并在必要时直接使用${env.ANT_HOME}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM