Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include classpath jars into a jar in Ant

Tags:

java

ant

I am developing a desktop application using swingx, spring and using ant to build them. This is my first project in ant. How can add classpath jars to the executable jar build by ant? I have swingx, spring jar into my classpath. This my target for jar:

<target name="jar">
    <mkdir dir="build/jar" />
    <jar destfile="build/jar/MainClass.jar" basedir="build/classes">
        <manifest>
            <attribute name="Main-Class" value="MainClass" />
        </manifest>         
    </jar>
</target>

Edit:

<project name="Number">
    <description>
            description
    </description>

    <property name="lib" value="lib" />
    <property name="src" value="src" />
    <property name="build" value="build" />
    <property name="classes" value="classes" />
    <property name="jar" value="jar"/>

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

    <path id="classpath">
        <fileset dir="${lib}" includes="**/*.jar" />
    </path>

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

    <target name="jar" depends="clean, compile">
        <mkdir dir="${build}/${jar}" />
        <jar destfile="build/jar/Number.jar" basedir="${build}/${classes}">
            <manifest>
                <attribute name="Main-Class" value="MainClass" />
            </manifest>
            <fileset file="test.txt">
            </fileset>
        </jar>
    </target>

    <target name="run">
        <java jar="${build}/${jar}/MainClass.jar" fork="true" />
    </target>
</project>
like image 749
Tapas Bose Avatar asked Aug 14 '11 13:08

Tapas Bose


People also ask

How do I find the CLASSPATH of a jar file?

To check our CLASSPATH on Windows we can open a command prompt and type echo %CLASSPATH%. To check it on a Mac you need to open a terminal and type echo $CLASSPATH.


1 Answers

You can add them with the following code:

<target name="compile">
    <mkdir dir="${classes.dir}"/>
    <javac srcdir="${src.dir}" destdir="${classes.dir}">
        <classpath>
               <fileset dir="${lib}">
                  <include name="**/*.jar" />
               </fileset>
         </classpath>
    </javac>
</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>
        <zipgroupfileset dir="${lib}" includes="**/*.jar"/>
    </jar>

</target>

This would include all *.jar files which are under the ${lib} folder.

But this code should be in an ant compile target. Because before generating the jar bundle you need to compile it. And therefore you need the swingx, spring jars.

like image 63
Prine Avatar answered Sep 29 '22 02:09

Prine