在 Ant 中,如何动态构建引用属性文件的属性? [英] In Ant, how can I dynamically build a property that references a property file?
问题描述
我正在使用输入任务来收集特定的属性值,并且我想将这些值连接成一个引用我的属性文件的属性值.
I am using Input tasks to collect specific property values and I want to concatenate those into one property value that references my properties file.
我可以生成属性的格式,但在运行时它被视为字符串而不是属性引用.
I can generate the format of the property but at runtime it is treated as a string and not a property reference.
示例属性文件:
# build.properties
# Some Server Credentials
west.1.server = TaPwxOsa
west.2.server = DQmCIizF
east.1.server = ZCTgqq9A
示例构建文件:
<property file="build.properties"/>
<target name="login">
<input message="Enter Location:" addproperty="loc" />
<input message="Enter Sandbox:" addproperty="box" />
<property name="token" value="\$\{${loc}.${box}.server}" />
<echo message="${token}"/>
</target>
当我调用 login 并为输入值提供west"和1"时,echo 将打印 ${west.1.server} 但它不会从属性文件中检索属性值.
When I call login and provide "west" and "1" for the input values, echo will print ${west.1.server} but it will not retrieve the property value from the properties file.
如果我对消息中的属性值进行硬编码:
If I hardcode the property value in the message:
<echo message="${west.1.server}"/>
然后 Ant 将尽职尽责地从属性文件中检索字符串.
then Ant will dutifully retrieve the string from the properties file.
如何让 Ant 接受动态生成的属性值并将其视为要从属性文件中检索的属性?
How can I get Ant to accept the dynamically generated property value and treat it as a property to be retrieved from the properties file?
推荐答案
使用 Props antlib<的附加示例/a>.
需要 Ant >= 1.8.0(适用于最新的 Ant 版本 1.9.4)和 Props antlib 二进制文件.
Additional example using the Props antlib.
Needs Ant >= 1.8.0 (works fine with latest Ant version 1.9.4)
and Props antlib binaries.
官方Props antlib GIT 存储库(或此处)不起作用开箱即用:
The current build.xml in official Props antlib GIT Repository (or here) doesn't work out of the box :
BUILD FAILED
Target "compile" does not exist in the project "props".
获取props antlib的源代码并解压到文件系统中.
获取 antlibs-common 的来源并将内容解压到 ../ant-antlibs-props-master/common
运行 ant antlib
来构建 jar :
Get the sources of props antlib and unpack in filesystem.
Get the sources of antlibs-common and unpack contents to ../ant-antlibs-props-master/common
Run ant antlib
for building the jar :
[jar] Building jar: c:\area51\ant-antlibs-props-master\build\lib\ant-props-1.0Alpha.jar
否则从 MVNRepository 获取二进制文件或这里
Otherwise get the binaries from MVNRepository or here
../antunit 很有帮助.对于嵌套属性,请查看 nested-测试.xml
将 ant-props.jar 放在 ant 类路径上.
The examples in ../antunit are quite helpful.
For nested properties look in nested-test.xml
Put the ant-props.jar on ant classpath.
<project xmlns:props="antlib:org.apache.ant.props">
<!-- Activate Props antlib -->
<propertyhelper>
<props:nested/>
</propertyhelper>
<property file="build.properties"/>
<input message="Enter Location:" addproperty="loc" />
<input message="Enter Sandbox:" addproperty="box" />
<property name="token" value="${${loc}.${box}.server}"/>
<echo message="${token}"/>
</project>
输出:
Buildfile: c:\area51\ant\tryme.xml
[input] Enter Location:
west
[input] Enter Sandbox:
1
[echo] TaPwxOsa
BUILD SUCCESSFUL
Total time: 4 seconds
这篇关于在 Ant 中,如何动态构建引用属性文件的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!