可选的 Ant arg [英] Optional Ant arg

查看:26
本文介绍了可选的 Ant arg的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有一个 ant arg 值可选地包括在内,而不必制作 2 个目标,除了额外的 arg 外,这些目标基本相同.例如:

I would like to have an ant arg value optionally included without having to make 2 targets which would be basically the same except for the extra arg. For example:

<target name="A" depends="C">...</target>

<target name="B" depends="C">...</target>

<target name="C">
    <java fork="true" ...>
        <jvmarg .../>
        <arg .../>
        <arg .../>
        ...
        # now, if the dependency is from A, no more args
        # if from B
            <arg value="xxx"/>
    </java>
</target>

推荐答案

与其依赖任务 C,不如使用 Antcall 任务将 B 参数作为 param 传递.

Rather than depending on task C, you could use the Antcall task to pass the B argument as a param.

<target name="A" >
  <antcall target="C" />
  ....
</target>

<target name="B" >
  <antcall target="C" >
    <param name="extra_arg" value="xxx" />
  </antcall>
  ...
</target>

<target name="C">
    <java fork="true" ...>
        <jvmarg .../>
        <arg .../>
        <arg .../>
        <arg value="${extra_arg}"/>
    </java>
</target>

正如 Nico 在评论中指出的那样,如果该值未从 A 中设置,这将不起作用.答案可以扩展为使用 condition 任务将参数设置为 null字符串.

As Nico points out in the comment, this doesn't work if the value is unset from A. The answer can be extended to use the condition task to set the argument to a null string.

<condition property="argToUseIfFromB" else="">
  <isset property="extra_arg" />      
</condition>
<java fork="true" ...>
    <jvmarg .../>
    <arg .../>
    <arg .../>
    <arg value="${argToUseIfFromB}"/>
</java>

进一步由于我们无法将参数识别为可选,因此我们可以从每个父任务中传入整个命令行.目标 A 只会传递公共参数;B 会传递一个额外的参数.参数的 Ant 手册比我解释得更好.

FURTHER Since we can't get the arguments to be recognised as optional, we can pass in the whole command line from each parent task. Target A would only pass the common arguments; B would pass through an extra argument. The Ant manual on arguments explains it better than me.

<target name="A" >
  <antcall target="C">
    <param name="java_args" value="arg_a arg_b" /> 
  </antcall>
  ....
</target>

<target name="B" >
  <antcall target="C" >
    <param name="java_args" value="arg_a arg_b extra_arg" />
  </antcall>
  ...
</target>

<target name="C">
    <java fork="true" ...>
        <jvmarg .../>
        <arg line="${java_args}"/>
    </java>
</target>

这篇关于可选的 Ant arg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆