Ant 命令行参数



程序在使用 eclipse 运行配置运行时工作正常,但是当使用 ant 运行时,它无法从 args[0] 解析 int,我不明白。完整代码可在此处获得 https://gist.github.com/4108950/e984a581d5e9de889eaf0c8faf0e57752e825a97我相信这与蚂蚁有关,

target name="run" description="run the project">
   java dir="${build.dir}" classname="BinarySearchTree" fork="yes">
    <arg value="6 in.txt"/>
   /java>
/target>

arg 值将通过 -D 标志更改,如在 ant -Dargs="6 testData1.txt" run中一样。

任何帮助将不胜感激,这是非常令人沮丧的。

您需要

将参数作为两个不同的arg值提供:

<target name="run" description="run the project">
   <java dir="${build.dir}" classname="BinarySearchTree" fork="yes">
       <arg value="6" />
       <arg value="in.txt" />
   </java>
</target>

您还可以使用 line 属性;从ANT文档中:

<arg value="-l -a"/>

是包含空格字符的单个命令行参数,而不是单独的命令"-> l"和"-a"。

<arg line="-l -a"/>

这是一个带有两个单独参数"-l"和"-a"的命令行。

扩展纪元的答案。

Java Task 支持 sysproperty 和 jvmarg。

例如(来自 ant java 任务页面)

<java classname="test.Main"
    fork="yes" >
<sysproperty key="DEBUG" value="true"/>
<arg value="-h"/>
<jvmarg value="-Xrunhprof:cpu=samples,file=log.txt,depth=3"/>   </java>

因此,您可以从传递给 ant 的命令行构造参数。

<target name="run" description="run the project">
   <java dir="${build.dir}" classname="BinarySearchTree" fork="yes">
      <sysproperty key="testarg"  value="${testarg}"
       <arg value="${arg1}" />
       <arg value="${arg2}" />
   </java>
</target>

现在,如果您用 ant -Dtestarg=test1234 -Darg1=6 -Darg2=in.txt 调用蚂蚁,那么testarg将通过属性获得。其他的将成为Java程序的正常参数。

最新更新