如何迭代ant属性并将其作为arg传递给exec

时间:2012-10-31 02:52:24

标签: ant

我有一个属性文件,我想将所有键和值传递给命令,我该怎么做?

说属性文件看起来像文件

size=small
perf=true

并且ant任务看起来像

    <exec executable="${prog}" failonerror="true">
<!-- I want to add all properties from file as argument here, -->
    </exec>

以便在运行时,行为符合

<exec executable="${prog}" failonerror="true">
   <arg value = "size"/>
   <arg value = "small"/>
   <arg value = "perf"/>
   <arg value = "true"/>
</exec>

我该怎么做? 感谢

编辑:

根据Vadzim的建议,我现在正在使用脚本。

<property file="test.properties" prefix="testproperty"/>
<target name = "test1">
<script language="javascript"> <![CDATA[
  properties = project.getUserProperties()
  echo = project.createTask( "echo" );
  anotation = project.getProperty("testproperty.size")
  echo.setMessage(anotation);
  echo.perform();
  for(key in properties) {
    echo.setMessage(key);
    echo.perform();
    echo.setMessage(properties[key]);
    echo.perform();
  }
]]> </script>
</target>

但是,getProperty()正在运行,但getProperties只包含java函数,如notify()

$ant test1
test1:
     [echo] small
     [echo] notifyAll
     [echo] function notifyAll() {/*
     [echo] void notifyAll()
     [echo] */}
     [echo] values
     [echo] function values() {/*
     [echo] java.util.Collection values()
     [echo] */}
     [echo] containsKey
     [echo] function containsKey() {/*
     [echo] boolean containsKey(java.lang.Object)
     [echo] */}
     [echo] entrySet
     [echo] function entrySet() {/*
     [echo] java.util.Set entrySet()
     [echo] */}
     [echo] keySet
     [echo] function keySet() {/*
     [echo] java.util.Set keySet()
     [echo] */}
     [echo] put
     [echo] function put() {/*
     [echo] java.lang.Object put(java.lang.Object,java.lang.Object)
     [echo] */}
     [echo] contains
     [echo] function contains() {/*
     [echo] boolean contains(java.lang.Object)
     [echo] */}
     [echo] empty
     [echo] false
     [echo] equals
     [echo] function equals() {/*
     [echo] boolean equals(java.lang.Object)
     [echo] */}
     [echo] notify
     [echo] function notify() {/*
     [echo] void notify()
     [echo] */}
     [echo] class
     [echo] class java.util.Hashtable
     [echo] elements
     [echo] function elements() {/*
     [echo] java.util.Enumeration elements()
     [echo] */}
     [echo] keys
     [echo] function keys() {/*
     [echo] java.util.Enumeration keys()
     [echo] */}
     [echo] isEmpty
     [echo] function isEmpty() {/*
     [echo] boolean isEmpty()
     [echo] */}
     [echo] size
     [echo] function size() {/*
     [echo] int size()
     [echo] */}
     [echo] putAll
     [echo] function putAll() {/*
     [echo] void putAll(java.util.Map)
     [echo] */}
     [echo] clear
     [echo] function clear() {/*
     [echo] void clear()
     [echo] */}
     [echo] wait
     [echo] function wait() {/*
     [echo] void wait()
     [echo] void wait(long)
     [echo] void wait(long,int)
     [echo] */}
     [echo] containsValue
     [echo] function containsValue() {/*
     [echo] boolean containsValue(java.lang.Object)
     [echo] */}
     [echo] toString
     [echo] function toString() {/*
     [echo] java.lang.String toString()
     [echo] */}
     [echo] hashCode
     [echo] function hashCode() {/*
     [echo] int hashCode()
     [echo] */}
     [echo] clone
     [echo] function clone() {/*
     [echo] java.lang.Object clone()
     [echo] */}
     [echo] get
     [echo] function get() {/*
     [echo] java.lang.Object get(java.lang.Object)
     [echo] */}
     [echo] getClass
     [echo] function getClass() {/*
     [echo] java.lang.Class getClass()
     [echo] */}
     [echo] remove
     [echo] function remove() {/*
     [echo] java.lang.Object remove(java.lang.Object)
     [echo] */}

BUILD SUCCESSFUL
Total time: 0 seconds

迭代getProperties()的结果的正确方法是什么?

任何建议都非常适用

EDIT2:

jbeard4有一个很好的解释here。关于javascript问题。 所以正确的方法是

<script language="javascript"> <![CDATA[
  properties = project.getUserProperties()
  var keyIter = properties.keySet().iterator();
  while(keyIter.hasNext()) {
    var key = keyIter.next();
    var value = properties.get(key);
    echo.setMessage(key+value);
    echo.perform();
  }
]]> </script>

1 个答案:

答案 0 :(得分:1)

我认为只有使用自定义script代码才能实现这一目标。

请参阅示例Is it possible to call ant task from a javascript scriptdef task?

只需迭代project.getProperties()并添加exec args through API