3

I'm completely new to Ant and need to add a couple jars to my classpath, compile a couple .java files, and run a junit test, all in Ant. I've been looking at a few online tutorials and manuals, but can't seem to wrap my head around the entire xml writing process.

All the previously-written code resides in a single directory called XXX.

In XXX there are two jars I need to add to my classpath with export CLASSPATH=$CLASSPATH:jar1:jar2, two java files I compile with javac *.java, one of which contains several junit tests that I run with java org.junit.runner.JUnitCore Tests. The build.xml would reside in XXX as well (I believe).

So far I have the following for just compiling, although I think there's a lot missing.

<?xml version="1.0"?>
<project name="EtlAutomation" default="compile" basedir=".">

    <property name="src" value="${basedir}"/>

    <target name="compile">
        <!-- Compile .java files -->
        <javac srcdir="${src}" destdir="${src}"/>
    </target>

</project>

What else do I need to add to compile *.java in the current directory? How can I run the export CLASSPATH command, and finally the junit commend?

I'm not asking for anyone to write my code, but it would be appreciated. If anyone knows a good beginner tutorial for a unix environment, that would be awesome. I'm a total beginner with ant so I'll take what I can get.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Chris
  • 1,465
  • 1
  • 18
  • 36
  • You should start with the documentation on the `javac` task in the Ant manual, it's all described there (http://ant.apache.org/manual/index.html). In particular, look for nested `` elements, and forget about `export`, it's not used in Ant. – skaffman May 15 '12 at 17:46
  • Thanks for the tip (and question edit)! I'm looking through the manual now, will check back on this in a little bit and hopefully be able to understand whatever answer(s) I get. – Chris May 15 '12 at 17:57

1 Answers1

1

Here is a previous question addressing this. And this may work for you:

<project name="EtlAutomation" default="compile" basedir=".">
    <property name="src" value="${basedir}"/>
    <path id="compile.classpath">
        <fileset dir="./">
            <include name="*.jar"/>
        </fileset>
    </path>
    <target name="compile" >
        <javac destdir="${src}" srcdir="${src}">
            <classpath refid="compile.classpath"/>
        </javac>
    </target>
    <target name="run" depends="compile">
        <junit>
           <classpath refid="compile.classpath" />
               <test name="TestExample" />
        </junit>
    </target>
</project>
Community
  • 1
  • 1
Sridhar
  • 2,416
  • 1
  • 26
  • 35
  • Hey RajChola, what are your thoughts on [this?](http://pastebin.com/chVzWvcG) Am I leaving anything big out of the picture? Nothing specific, but any general important things to consider/readdress? – Chris May 15 '12 at 18:55
  • I think you've a couple of errors. I don't think you can use args like that. If it works you aren't leaving anything out. Doesn't matter what my thoughts are on it. Run it and see what errors you get and then try to correct them. – Sridhar May 15 '12 at 20:49
  • Change the following 1) default="run" 2) instead of args="..." 3) Fix the error at – Sridhar May 15 '12 at 21:17
  • Thank you! A problem I'm facing is I can't really test this before it's working correctly since we're dealing with some delicate files, which is why I'm being as careful as possible. Obviously I could test the compiling but anything with running or classpath would give a bunch of people a pretty big headache. I'll do my best to finish it up based on your help. – Chris May 16 '12 at 13:34