David
David

Reputation: 1101

Does the junit.jar has to be in the ant lib directory to run junit task

I was building an Ant build file that worked properly in my eclipse project but not on our Jenkins autobuild set-up. I installed ant on my computer and ran the build from the console. It worked, but I realized it didn't use the junit-4.10.jar in my project lib like I wished but the junit.jar in the ant lib. After renaming the junit.jar files in my ant lib, the ant build didn't work.

So basically the problem in our Jenkins autobuild setup is that there are no junit.jar in its own ant lib directory. Can I specify the junit task to use the jar in my project lib instead of the one in the ant lib?

EDIT : I have modified my build.xml file and now it looks like this, still doesnt work. My junit-4.10.jar is in the /war/WEB-INF/lib/test directory :

<project name="vlp" default="junit" basedir=".">
    <tstamp />
    <!-- ################# PROPERTIES ################ -->
    <!-- directory properties -->
    <!-- source -->
    <property name="vlp.src" location="src" />
    <property name="vlp.test" location="test" />
    <!-- build -->
    <property name="src.build" location="bin/src" />
    <property name="test.build" location="bin/test" />
    <!-- libraries -->
    <property name="vlp.lib.dir" location="war/WEB-INF/lib" />
    <property name="vlp.testlib.dir" location="war/WEB-INF/lib/test" />
    <!-- compile classpath -->
    <path id="compile.path">
        <fileset dir="${vlp.lib.dir}" includes="*.jar" />
    </path>
    <!-- test classpath -->
    <path id="test.path">
        <fileset dir="${vlp.testlib.dir}" includes="*.jar" />
        <path refid="compile.path" />
    </path>
    <!-- ############### CLEANING ################## -->
    <!-- Cleaning old compile files -->
    <target name="clean" description="Clean all the old build files.">
        <delete dir="${src.build}" />
        <delete dir="${dist}" />
    </target>
    <!-- ############## COMPILATION ############### -->
    <!-- Compile source -->
    <target name="src.compile" depends="clean" description="Compile the source code when everything has been cleaned.">
        <mkdir dir="${src.build}" />
        <javac encoding="utf-8" destdir="${src.build}" nowarn="true">
            <src path="${vlp.src}" />
            <classpath refid="compile.path" />
        </javac>
    </target>
    <!-- Compile test -->
    <target name="test.compile" depends="clean" description="Compile the source code when everything has been cleaned.">
        <mkdir dir="${test.build}" />
        <javac encoding="utf-8" destdir="${test.build}" nowarn="true">
            <src path="${vlp.test}" />
            <classpath>
                <pathelement location="${src.build}" />
                <path refid="test.path" />
            </classpath>
        </javac>
    </target>
    <!-- ########### RUNS JUNIT TEST ############ -->
    <target name="junit" depends="src.compile,test.compile" description="Runs all the unit test in the application. Does not halt build if test are failed.">
        <junit printsummary="on" haltonfailure="false" showoutput="true">
            <formatter type="brief" usefile="false" />
            <classpath>
                <pathelement location="${test.build}" />
                <path refid="test.path" />
            </classpath>
            <batchtest>
                <fileset dir="${vlp.test}">
                    <include name="**/Test*.java" />
                    <exclude name="**/AllTests.java" />
                </fileset>
            </batchtest>
        </junit>
    </target>
</project>

EDIT : Simlar question can be found here with a different answer that works well. Note that it is much easier to install ant-junit on the machine than to try to add it to your libs and everything.

Upvotes: 1

Views: 1721

Answers (1)

Mark O&#39;Connor
Mark O&#39;Connor

Reputation: 77991

See the answer to this question:

H2 database org.h2.Driver ClassNotFoundException

I normally specify the junit jar to be on a test classpath and then use it when calling the junit ANT task


Update

The following build file is an example of a starting template I used for my builds.

Sets up an infrastructure that uses ivy to manage classpaths. Dependencies are downloaded from the Maven Central repository (by default). Makes build much more portable and repeatable.

build.xml

<project name="ivy demo" default="build" xmlns:ivy="antlib:org.apache.ivy.ant">

    <!--
    ==========
    Properties
    ==========
    -->
    <property name="build.dir"  location="build"/>
    <property name="class.dir"  location="${build.dir}/classes"/>
    <property name="report.dir" location="${build.dir}/reports"/>

    <!-- 
    =======
    Targets
    =======
    -->
    <target name="install-ivy" description="Used to install the ivy task jar">
        <mkdir dir="${user.home}/.ant/lib"/>
        <get dest="${user.home}/.ant/lib/ivy.jar" src="http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.2.0/ivy-2.2.0.jar"/>
    </target>

    <target name="init" description="Download dependencies, setup project classpaths and create build directories">
        <ivy:resolve/>

        <ivy:cachepath pathid="compile.path" conf="compile"/>
        <ivy:cachepath pathid="runtime.path" conf="runtime"/>
        <ivy:cachepath pathid="test.path"    conf="test"/>

        <ivy:report todir="${report.dir}" graph="false"/>

        <mkdir dir="${class.dir}"/>
    </target>

    <target name="build" depends="init" description="Build the project">
        <echo message="Build logic goes here"/>
    </target>

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

    <target name="clean-all" depends="clean" description="Purge ivy cache">
        <ivy:cleancache />
    </target>

</project>

ivy.xml

This file is used to specify the project's dependencies. Ivy configurations are used to manage the classpath groupings.

<ivy-module version="2.0">
    <info organisation="org.demo" module="demo"/>
    <configurations>
        <conf name="compile"/>
        <conf name="runtime" extends="compile"/>
        <conf name="test"    extends="runtime"/>
    </configurations>
    <!--
    Dependencies can be found using Maven Central's search site:

        http://search.maven.org/
    -->
    <dependencies>
        <!-- Compile dependencies -->
        <dependency org="org.slf4j" name="slf4j-api" rev="1.6.4" conf="compile->default"/>

        <!-- Runtime dependencies -->
        <dependency org="log4j" name="log4j" rev="1.2.16" conf="runtime->default"/>

        <!-- Test dependencies -->
        <dependency org="junit" name="junit" rev="4.10" conf="test->default"/>
    </dependencies>
</ivy-module>

Upvotes: 1

Related Questions