java Ant 命令行参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13447796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:45:43  来源:igfitidea点击:

Ant command line arguments

javaant

提问by kqualters

Program works fine when run with eclipse run configurations, but when run with ant, it is unable to parse int from args[0], which I do not understand. Full code is available here https://gist.github.com/4108950/e984a581d5e9de889eaf0c8faf0e57752e825a97I believe it has something to do with 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>

the arg value will be changed via the -D flag, as in ant -Dargs="6 testData1.txt" run.

arg 值将通过 -D 标志更改,如 ant -Dargs="6 testData1.txt" 运行。

Any help would be much appreciated, it is very frustrating.

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

回答by epoch

You need to supply the arguments as two different argvalues:

您需要将参数提供为两个不同的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>


You can also use the lineattribute; From the ANTdocs:

您也可以使用该line属性;从ANT文档:

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

is a single command-line argument containing a space character, not separate commands "-> l" and "-a".

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

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

This is a command line with two separate arguments, "-l" and "-a".

这是一个带有两个独立参数“-l”和“-a”的命令行。

回答by Jayan

Expanding epoch 's answer.

扩展 epoch 的答案。

java task supports sysproperty and jvmarg.

java 任务支持 sysproperty 和 jvmarg。

For example (from ant java task page)

例如(来自 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>
<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>

So you could construct the args from the command line passed to ant.

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

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

Now if you call ant with ant -Dtestarg=test1234 -Darg1=6 -Darg2=in.txt, then testargwill be available via property. Others will become normal arguments to the java program.

现在,如果您使用 ant 调用ant -Dtestarg=test1234 -Darg1=6 -Darg2=in.txt,则testarg可以通过属性获得。其他的将成为 java 程序的正常参数。