今天尝试了下使用Ant来打包可执行jar,要求: 不将其他依赖的jar中的class打包至目标jar中。(初学,当做学习笔记记录下)
环境: Eclipse3.5.1(自带Ant) + Java1.5
1. 建立META-INF/MANIFEST.MF文件
如下,注意Class-Path中不带“,”分隔符
2. 建立build.xml
3. 运行Ant Build ,生成可执行jar
注意:因为这个jar没有包含所依赖的那些jar,所以如果想要双击可执行的话,需要在当前目录建立lib文件夹,并复制依赖的jar至其中,然后即可。
环境: Eclipse3.5.1(自带Ant) + Java1.5
1. 建立META-INF/MANIFEST.MF文件
如下,注意Class-Path中不带“,”分隔符
Manifest-Version: 1.0
Built-By: XXX
Main-Class: aa.bb.cc.MainClass
Class-Path: . lib/commons-lang-2.5.jar lib/sqljdbc.jar lib/commons-con
figuration-1.6.jar lib/log4j-1.2.15.jar lib/org.eclipse.swt.win32.win
32.x86_3.5.1.v3555a.jar lib/org.eclipse.nebula.widgets.datechooser_1.
0.0.HEAD.jar lib/commons-logging-1.1.1.jar lib/org.eclipse.nebula.wid
gets.formattedtext_1.0.0.HEAD.jar lib/commons-collections-3.2.1.jar
2. 建立build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="XXX" default="build" basedir=".">
<property name="src.dir" value="src" />
<property name="classes.dir" value="bin"/>
<property name="javadoc.dir" value="docs"/>
<property name="obj.dir" value="obj"/>
<property name="jar.name" value="XXX.jar"/>
<property name="lib.dir" value="lib"/>
<!-- Class path, lib-->
<path id="classpath">
<pathelement location="${lib.dir}/commons-collections-3.2.1.jar"/>
<pathelement location="${lib.dir}/commons-configuration-1.6.jar"/>
<pathelement location="${lib.dir}/commons-lang-2.5.jar"/>
<pathelement location="${lib.dir}/commons-logging-1.1.1.jar"/>
<pathelement location="${lib.dir}/log4j-1.2.15.jar"/>
<pathelement location="${lib.dir}/org.eclipse.nebula.widgets.datechooser_1.0.0.HEAD.jar"/>
<pathelement location="${lib.dir}/org.eclipse.nebula.widgets.formattedtext_1.0.0.HEAD.jar"/>
<pathelement location="${lib.dir}/org.eclipse.swt.win32.win32.x86_3.5.1.v3555a.jar"/>
<pathelement location="${lib.dir}/sqljdbc.jar"/>
</path>
<!-- Prepare -->
<target name="delObj">
<delete dir="${obj.dir}" />
</target>
<target name="makeObj" depends="delObj">
<mkdir dir="${obj.dir}" />
</target>
<target name="delBin">
<delete dir="${classes.dir}" />
</target>
<target name="makeBin" depends="delBin">
<mkdir dir="${classes.dir}" />
</target>
<!-- Compile -->
<target name="compile" depends="makeObj,makeBin" description="编译" >
<javac fork="yes" srcdir="${src.dir}" destdir="${classes.dir}" debug="off" encoding="UTF-8">
<classpath refid="classpath"/>
<include name="**/*.java"/>
</javac>
</target>
<target name="copyConfig" depends="compile">
<copy todir="${classes.dir}">
<fileset dir="${src.dir}/log4j"/>
</copy>
</target>
<target name="build" depends="copyConfig" description="包装成jar文件" >
<jar jarfile="${obj.dir}/${jar.name}" basedir="${classes.dir}" manifest="META-INF/MANIFEST.MF" >
</jar>
</target>
</project>
3. 运行Ant Build ,生成可执行jar
注意:因为这个jar没有包含所依赖的那些jar,所以如果想要双击可执行的话,需要在当前目录建立lib文件夹,并复制依赖的jar至其中,然后即可。