【问题标题】:In Ant, how can I dynamically build a property that references a property file?在 Ant 中,如何动态构建引用属性文件的属性?
【发布时间】:2014-10-29 17:43:28
【问题描述】:

我正在使用输入任务来收集特定的属性值,我想将它们连接成一个引用我的属性文件的属性值。

我可以生成属性的格式,但在运行时它被视为字符串而不是属性引用。

示例属性文件:

# 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 接受动态生成的属性值并将其视为要从属性文件中检索的属性?

【问题讨论】:

标签: ant


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    否则从MVNRepositoryhere 获取二进制文件

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

    【讨论】:

      【解决方案3】:

      解决办法是: 考虑问题是这个,你想在哪里实现这个:

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

      你可以使用javascript:

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

      我试了一下,这对我有用。

      【讨论】:

        猜你喜欢
        • 2016-09-08
        • 1970-01-01
        • 2014-03-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-12
        • 1970-01-01
        • 2020-01-08
        相关资源
        最近更新 更多