R
R
raner1342017-09-27 00:03:21
Java
raner134, 2017-09-27 00:03:21

NoClassDefFoundError. How to properly include third-party libraries in Ant?

Can't include library when launched via ant. Interestingly, this library is compiled with the task that is responsible for the test.
build.xml itself

<?xml version="1.0" encoding="UTF-8"?>
<project name="Calculator" basedir="." default="run">

    <property name="src.dir"  value="./src"/>
    <property name="build.dir" value="./classes"/>
    <property name="classes.dir" value="./classes"/>
    <property name="jar.dir"   value="${build.dir}/jar"/>
    <property name="client-class"  value="com.test.calc.Client"/>
    <property name="server-class" value="com.test.calc.Server"/>
    <property name="junit" value="lib/junit-4.12.jar"/>

    <property name="lib.dir"  value="${basedir}/lib"/>


    <path id="classpath">
        <fileset dir="lib">
            <include name="**/*.jar" />
        </fileset>
    </path>


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

    <target name="compile" depends="clean">
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath" includeantruntime="false">
        </javac>
        <mkdir dir="${classes.dir}/lib"/>

        <copy todir="${classes.dir}/lib">
            <fileset dir="${lib.dir}">
                <include name="**/*.jar"/>
            </fileset>
        </copy>
    </target>

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

        <jar destfile="${jar.dir}/Server.jar" basedir="${classes.dir}">
            <manifest>
                <attribute name="Main-Class" value="${server-class}"/>
            </manifest>
        </jar>
    </target>


    <target name="run" depends="build">
        <parallel>
            <java jar="${jar.dir}/Server.jar" fork="true" classpath="classpath">

            </java>

            <!--<waitfor maxwait="5" maxwaitunit="second" timeoutproperty="failtimeout">-->
                <!--<socket server="localhost" port="8189"/>-->
            <!--</waitfor>-->

        <java jar="${jar.dir}/Client.jar" fork="true" classpath="classpath">


        </java>

    </parallel>
    </target>


    <target name="test" depends="compile">
        <junit printsummary="on" fork="true" showoutput="true">
            <classpath>
                <pathelement location="${classes.dir}"/>
                <pathelement location="lib/junit-4.12.jar"/>
                <pathelement location="lib/hamcrest-core-1.3.jar"/>
                <pathelement location="lib/MathParser.org-mXparser-4.1.1.jar"/>
            </classpath>
            <formatter type="brief" usefile="false"/>
            <test name="test_class.CalculatorTest"/>
        </junit>
    </target>

</project>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2017-09-27
@zolt85

Well, let's continue, young man.
Tip number one - in properties that point to file paths, it's better to use location instead of value right away.
Those.
It would also be nice to tell us what version of Ant you are using.
Now, regarding the launch of the test, the dependency on the compile target hangs on the test target, but if you look at the compile target, you can see that nothing but compilation happens there (I'm hinting that the jar is not created there), and the assembly of the compiled classes in the jar happens in the build target. Therefore, the record of the target test should be brought to the form

<target name="test" depends="build">
...
</target>

Thus, you will start compiling your code first, then everything will be assembled into jars, and only then the test will start

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question