将命令行参数传递给在exec中使用它们的目标

pet*_*oke 16 ant

ant bootstrap arg1 arg2 arg3

我需要回显"arg1 arg2 arg3",以便我可以使用这些参数调用程序

在网上搜索以下内容应该有效,但不能.

 <target name="bootstrap">
     <echo>${arg0} ${arg1} ${arg2} </echo>
     <!--exec executable="cmd">
        <arg value="${arg0}"/>
        <arg value="${arg1}"/>
        <arg value="${arg2}"/>
     </exec-->
 </target>
Run Code Online (Sandbox Code Playgroud)

如果用户传递5个args或1个arg,也会有任何想法.我需要失败它没有正确数量的args.

coo*_*fan 34

没有.

您不能以这种方式传递将在构建文件中使用的参数.将ant bootstrap arg1 arg2 arg3与您试图调用下面的目标将得到解决bootstrap,arg1,arg2,arg3- ,很明显,只有目标bootstrap存在.

如果您确实要传递将在构建文件中使用的参数,则需要使用该-DpropertyName=value格式.例如:

ant bootstrap -Darg1=value1 -Darg2=value2 -Darg3=value3
Run Code Online (Sandbox Code Playgroud)

对于其他方式,您可以在构建文件中编写嵌入脚本(如beanshell或javascript,使用ant的脚本支持库),首先处理参数.例如,您可以通过以下方式传递参数:

ant bootstrap -Dargs=value1,value2,value3,...
Run Code Online (Sandbox Code Playgroud)

现在你有一个名为args"value1,value2,value3,..." 的属性(对于......我的意思是用户可以键入3个以上的值).您可以使用的BeanShell的分裂argsarg1,arg2arg3通过,,并且也做一下检查...

<script language="beanshell" classpathref="classpath-that-includes-the-beanshell-lib">
    String[] args = project.getProperty("args").split(",");
    project.setUserProperty("arg1", args[0].trim());
    project.setUserProperty("arg2", args[1].trim());
    project.setUserProperty("arg3", args[2].trim());
</script>
Run Code Online (Sandbox Code Playgroud)