1、one-jar下载址:http://one-jar.sourceforge.net/index.php?page=downloads&file=downloads
下载包one-jar-sdk-0.97.jar
2、安装解压到相关目录
3、与ant 结合
最主要是引入<import file="one-jar-ant-task.xml"/>
官方例子
<import file="one-jar-ant-task.xml"/> <target name="hello" depends="init"> <!-- Build lib.jar --> <javac destdir="${classes.dir}/lib"> <src path="${lib.dir}" /> </javac> <jar destfile="${build.dir}/lib.jar" > <fileset dir="${classes.dir}/lib"/> </jar> <!-- Build classes for main.jar --> <javac destdir="${classes.dir}/src"> <src path="${src.dir}" /> <classpath path="${build.dir}/lib.jar"/> </javac> <!-- Construct the One-JAR file --> <one-jar destfile="hello.jar" manifest="hello.mf"> <main> <!-- Construct main.jar from classes and source code --> <fileset dir="${classes.dir}/src"/> </main> <lib> <fileset file="${build.dir}/lib.jar" /> </lib> </one-jar> <echo> Now you can run the Hello One-JAR example using $ java -jar hello.jar </echo> </target>
个人实践例子,包括第三方包,依赖项目结合
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="jar" name="TestDemo">
<property name="one-jar.dist.dir" value="../contrib/tool/one-jar" />
<import file="${one-jar.dist.dir}/one-jar-ant-task.xml" optional="true" />
<property name="debuglevel" value="source,lines,vars"/>
<property name="target" value="1.6"/>
<property name="source" value="1.6"/>
<property name="libs" value="../contrib/libs/" />
<property name="game_common" value="../Game_Common" />
<path id="lib_classpath">
<fileset dir="${libs}">
<include name="*.jar"/>
</fileset>
</path>
<target name="clean" >
<echo message="Java Version: ${ant.java.version}" />
<delete dir="build" />
<ant dir="${game_common}" target="jar"/>
<copy todir="${libs}">
<fileset dir="${game_common}">
<include name="game_common.jar" />
</fileset>
</copy>
</target>
<target name="build" depends="clean">
<mkdir dir="build"/>
<mkdir dir="build/class"/>
<javac debug="true" srcdir="src/resource" destdir="build/class" source="${source}" target="${target}" >
<classpath refid="lib_classpath"/>
</javac>
<javac debug="true" srcdir="src/main" destdir="build/class" source="${source}" target="${target}" >
<classpath refid="lib_classpath"/>
</javac>
</target>
<!--<target name="run" depends="main">
<java classname="HelloGame">
<classpath>
<pathelement path="build/class"/>
</classpath>
<classpath refid="lib_classpath"/>
</java>
</target>-->
<target name="jar" depends="build">
<one-jar destfile="hello.jar">
<manifest>
<attribute name="One-Jar-Main-Class" value="HelloGame"/>
</manifest>
<main>
<!-- Construct main.jar from classes and source code -->
<fileset dir="build/class"/>
</main>
<lib>
<fileset file="${libs}/*.jar" />
</lib>
</one-jar>
</target>
<!--<target name="jar" depends="run">
<pathconvert property="mf.classpath" pathsep=" ">
<mapper>
<chainedmapper>
<flattenmapper/>
<globmapper from="*" to="src/main/lib/*"/>
</chainedmapper>
</mapper>
<path refid="lib_classpath" />
</pathconvert>
<jar destfile="helloGame.jar" basedir="build/class">
<manifest>
<attribute name="Main-class" value="HelloGame" />
<attribute name="Class-Path" value="${mf.classpath}" />
</manifest>
</jar>
</target>-->
</project>