In Ant, how can I dynamically build a property tha

2020-03-25 23:58发布

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.

Example properties file:

# build.properties

# Some Server Credentials
west.1.server = TaPwxOsa
west.2.server = DQmCIizF
east.1.server = ZCTgqq9A

Example build file:

<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>

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}"/>

then Ant will dutifully retrieve the string from the properties file.

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?

标签: ant
3条回答
看我几分像从前
2楼-- · 2020-03-26 00:34

Additional example using the Props antlib.
Needs Ant >= 1.8.0 (works fine with latest Ant version 1.9.4) and Props antlib binaries.

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".

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

Otherwise get the binaries from MVNRepository or here

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>

output :

Buildfile: c:\area51\ant\tryme.xml
    [input] Enter Location:
west
    [input] Enter Sandbox:
1
     [echo] TaPwxOsa

BUILD SUCCESSFUL
Total time: 4 seconds
查看更多
放荡不羁爱自由
3楼-- · 2020-03-26 00:36

Solution is : Consider problem is this, where you want to achieve this :

<property name="prop" value="${${anotherprop}}"/> (double expanding the property)?

You can use javascript:

<script language="javascript">
    propname = project.getProperty("anotherprop");
    project.setNewProperty("prop", propname);
</script>

I gave it a try and this is working for me.

查看更多
Lonely孤独者°
4楼-- · 2020-03-26 00:40

The props antlib provides support for this but as far as I know there's no binary release available yet so you have to build it from source.

An alternative approach would be to use a 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}"/>
查看更多
登录 后发表回答