Chris
Chris

Reputation: 1505

Classpath, Compile, and Run with Ant?

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.

Upvotes: 3

Views: 1207

Answers (1)

Sridhar
Sridhar

Reputation: 2532

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>

Upvotes: 1

Related Questions