RandomQuestion
RandomQuestion

Reputation: 6998

Pass variable command line args via ant macrodef

I have a java program MyClass that accepts a required argument reqArg and an optional argument myOptionalArg. And I'm trying to write ant target for same.

  <macrodef name="run-class">
    <attribute name="reqArg" />
    <sequential>
      <condition property="additionalArgs" value="${args}" else="">
        <isset property="args" />
      </condition>
      <java classname="MyClass" failonerror="true" fork="true" classpath="${classpath}">
        <arg line="--reqArg @{reqArg}" />
        <arg line="${additionalArgs}" />
      </java>
    </sequential>
  </macrodef>

  <!--Separate target for each value of reqArg -->
  <target name="run-class-arg1">
    <run-class reqArg="arg1"/>
  </target>

With above I can successfully invoke the program like this and it works:

ant run-class-arg1 -Dargs="--myOptionalArg value"

Is there a way to adapt the target above so that caller can just do following without explicitly having to type -Dargs="...". It'll make things a bit easier for the caller and not remember the syntax.

ant run-class-arg1 --myOptionalArg value

Thanks

Upvotes: 2

Views: 717

Answers (1)

Chad Nouis
Chad Nouis

Reputation: 7051

I don't know of any way to pass arbitrary arguments directly to Ant.

Instead, consider wrapping the call to Ant in a shell script or batch script.

Test build.xml

<project name="ant-shell-arbitrary-args">
    <echo>${myArgs}</echo>
</project>

Bash script named call-ant.sh

#!/bin/bash
ant -DmyArgs="$*"

Example:

$ ./call-ant.sh hi there
...
 [echo] hi there

Windows batch script named call-ant.bat

@echo off
ant -DmyArgs="%*"

Example:

C:\>call-ant.bat hi there
...
 [echo] hi there

Upvotes: 1

Related Questions