Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional Ant 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>
like image 658
Nico Huysamen Avatar asked Feb 25 '11 07:02

Nico Huysamen


People also ask

How to pass command line arguments in Ant?

It is a single line command line argument having space characters. A command line argument with two separate options : -l and -a. when we run only ant from command line without any argument, Ant look for the default file build. xml and execute target.

What does Ant command does?

Ant - Property Task Ant uses the property element which allows you to specify the properties. This allows the properties to be changed from one build to another or from one environment to another.

What is an Ant build file?

Ant is a Java-based build tool created as part of the Apache open-source project. You can think of it as a Java version of make. Ant scripts have a structure and are written in XML. Similar to make, Ant targets can depend on other targets.

What is Ant target?

An Ant target is a sequence of tasks to be executed to perform a part (or whole) of the build process. Ant targets are defined by the user of Ant. Thus, what tasks an Ant target contains depends on what the user of Ant is trying to do in the build script.


1 Answers

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>
like image 160
DoctorRuss Avatar answered Nov 06 '22 17:11

DoctorRuss