How to include classpath jars into a jar in 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 destfi开发者_如何学Cle="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>
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.
精彩评论