我正在使用输入任务来收集特定的属性值,我想将它们连接到一个引用我的属性文件的属性值。
我可以生成属性的格式,但在运行时它被视为字符串而不是属性引用。
示例属性文件:
# 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},但它不会从属性文件中检索属性值。
如果我在消息中硬编码属性值:
<echo message="${west.1.server}"/>
然后Ant将尽职地从属性文件中检索字符串。
如何让Ant接受动态生成的属性值并将其视为要从属性文件中检索的属性?
使用Props antlib的其他示例。 需要Ant> = 1.8.0(适用于最新的Ant版本1.9.4)和Props antlib二进制文件。
官方Props antlib GIT Repository(或here)中的当前build.xml不能开箱即用:
BUILD FAILED
Target "compile" does not exist in the project "props".
获取props antlib的源代码并在文件系统中解压缩。
获取antlibs-common的来源并将内容解压缩到../ant-antlibs-props-master/common
运行ant antlib
来构建jar:
[jar] Building jar: c:\area51\ant-antlibs-props-master\build\lib\ant-props-1.0Alpha.jar
否则从MVNRepository或here获取二进制文件
../antunit中的例子非常有用。对于嵌套属性,请查看nested-test.xml 将ant-props.jar放在ant类路径上。
<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
props antlib提供了对此的支持,但据我所知,目前还没有二进制版本,所以你必须从源代码构建它。
另一种方法是使用macrodef
:
<macrodef name="setToken">
<attribute name="loc"/>
<attribute name="box"/>
<sequential>
<property name="token" value="${@{loc}.@{box}.server}" />
</sequential>
</macrodef>
<setToken loc="${loc}" box="${box}"/>
解决方案是:考虑问题是这个,你要实现这个目标:
<property name="prop" value="${${anotherprop}}"/> (double expanding the property)?
你可以使用javascript:
<script language="javascript">
propname = project.getProperty("anotherprop");
project.setNewProperty("prop", propname);
</script>
我试一试,这对我有用。