-2

Possible Duplicate:
Use Ant for running program with command line arguments

I have written a java program that gets as command line arguments several file names (that are inputted into String[] args array of the main. Now I need to write an Ant build file. How do I make this command:

ant run SampleFile1.txt SampleFile2.txt ...

Get those arguments and pass them to my main?

Here's my sample Ant file:

<?xml version="1.0" ?>
<project name="MyProgram" default="main">

<property name="src.dir"     value="src"/>

<property name="build.dir"   value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir"     value="${build.dir}/jar"/>

<property name="main-class"  value="MyProgram.MainClass"/>

<target name="clean">
    <delete dir="${build.dir}"/>
 </target>

<target name="compile">
    <mkdir dir="${classes.dir}"/>
    <javac srcdir="${src.dir}" destdir="${classes.dir}"/>
</target>

   <target name="jar" depends="compile">
        <mkdir dir="${jar.dir}"/>
        <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
            <manifest>
                <attribute name="Main-Class" value="${main-class}"/>

            </manifest>

        </jar>
    </target>

    <target name="run" depends="jar">
        <java jar="${jar.dir}/${ant.project.name}.jar" fork="true">
//now when I run it here, I want it to get the arguments I input when I do ant run. for example: ant run file1.txt, file2.txt <- I want those to go to my String[] args of the main 
        </java>
    </target>

    <target name="clean-build" depends="clean,jar"/>

    <target name="main" depends="clean,run"/>

Community
  • 1
  • 1
TheNotMe
  • 1,048
  • 2
  • 17
  • 33

1 Answers1

3

Here's a bit of a change in the <java> task:

<target name="run" depends="jar">
    <java jar="${jar.dir}/${ant.project.name}.jar" fork="true">
        <arg value="${file1}"/>
        <arg value="${file2}"/>
    </java>
</target>

Now, you have two new parameters: file1 and file2 You can pass those from the command line like this:

$ ant -Dfile1=foo.txt-Dfile2=bar.txt run

Ant takes a list of targets, and you are executing target run. The -D parameters are the properties you're passing to your program.

I recommend that you put default parameters in your program like this:

<target name="run" depends="jar">
    <property name="file1" value="bar.txt"/>
    <property name="file2" value="foo.txt"/>
    <java jar="${jar.dir}/${ant.project.name}.jar" fork="true">
        <args value="${file1}"/>
        <args value="${file2}"/>
    </java>
</target>

This way, if you don't pass any parameters, it will use your default parameters of foo.txt and bar.txt. Remember that properties set on the command line with the -D parameter override the value of those properties set in the build.xml file itself. Once a property is set, it can never be changed.

David W.
  • 105,218
  • 39
  • 216
  • 337
  • In my homework, I have to run the file like this: "ant run File1.txt File2.txt" So is there a way to make it run without the -D parameter? – TheNotMe Dec 21 '12 at 20:34
  • All non-parameterized arguments used in the Ant command are _targets_. It might be possible to do a bit of trickery to finagle Ant to do what you want. The Ant command line can be found in the ${sun.java.command} property. It might be possible to pull out the targets from there. Ant runs the non-targeted tasks before looking at the tasks, so it may be possible to put your `` task outside any targets, and then parse the command line, pull out your other arguments, execute `` task, and ignore the _targets not found message_. Better off writing a small shell script to do that for you. – David W. Dec 23 '12 at 15:45