views:

24

answers:

2

I am new to writing ant targets. I've to pass 3 arguments from the command line to java program. All these can be optional and can have spaces. How can i do that using ant target.

I tried using but if it delimits the arguments by space. So, if my first argument has space then it takes the word after space as second argument.

I also tried using It takes spaces but makes the arguments mandatory.

Im my java code, i've to set default values for these arguments if any of these is missing.

How can i do that.

A: 

If you have a JAR file ready, you can make this target:

<target name="runJar">
   <java jar="/home/phil/application.jar" failonerror="true" fork="true" inputstring="">
      <arg value="arg1" />
      <arg value="arg2" />
      <arg value="arg3" />
   </java>
</target>

If you need to compile, then run, then give this a shot:

<target name="buildJava">
   <javac srcdir="srcDir" destdir="binDir" />
</target>
<target name="runJava" depends="buildJava">
   <java classname="classname" failonerror="true" fork="true" inputstring="">
     <arg value="arg1"/>
     <arg value="arg2"/>
     <arg value="arg3"/>
     <classpath>
       <pathelement path="binDir"/>
     </classpath>
   </java>
</target>

The failonerror specifies that if you do System.exit(1) it will cause the build to fail.

Edit to add output

I had the class spit out the arguments in brackets first thing upon entry to the class, so you can see it treats spaces the way you want. I used arg1 as "arg1 2 3"

buildJava:

runJava:
[java] Arg = [arg1 2 3]
[java] Arg = [arg2]
[java] Arg = [arg3]
Phil
A: 

Your question is no very clear to me. But i think what you want is in java program you can pass argument . If you have only one argument then it makes other two as default value. So you can do something like this. I haven't tried this though

<!-- These properties will no set since you are passing argument from -D param 

They are only set to empty when there is no argument. -->

    <target name="run">
       <java jar="YourJavaClassOrJAR" failonerror="true" fork="true" inputstring="">
          <arg value="${arg1}" />
          <arg value="${arg2}" />
          <arg value="${arg3}" />
       </java>
    </target>

and from command line you can say ant -Darg1=xxx

Jigar Shah