Optional Ant arg

心已入冬 提交于 2019-12-21 05:08:14

问题


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>

回答1:


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>

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

FURTHER EDIT: 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>


来源:https://stackoverflow.com/questions/5114824/optional-ant-arg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!