图解教你如何使用ANT打包java程序

1:在eclipse中建立如下的工程

值得注意的就是build.xml文件(这个是重点后面会提到) ,其他HelloWorld中的就是一句简单的输出语句

2: 使用build打包(右键然后选择运行),运行后在console下可以看到如下图的过程

当看到了BUILD SUCCESSFUL 表面打包成功了!!!!

3:然后来到你的**\dist下可以看到刚刚打包成功的包 如下图 :

4:ANT很智能的,如果你已经实现了上面三个过程的打包,再次打包的话它会默认的执行空的操作 (如下图)

5:最重要的bulid.xml文件(此中有很多ANT内置的命令可以具体参考一下ant的用法)

[java] view plaincopyprint?

    <projectname="HelloWorld"default="jar"basedir="."><description>buildingHelloWorld!</description><!–setglobalpropertiesforthisbuild–><propertyname="src"location="src"/><propertyname="build"location="classes"/><propertyname="dist"location="dist"/><propertyname="user.name"value="qiuqiu"/><targetname="init"><!–Createthetimestamp–><tstamp/><!–Createtheclassesdirectorystructureusedbycompile–><mkdirdir="${build}"/></target><targetname="build"depends="init"description="buildthesourcecode"><!–Compilethejavacodefrom${src}into${build}–><javacsrcdir="${src}"destdir="${build}"/></target><targetname="jar"depends="build"description="generatethedistribution"><!–Createthedistributiondirectory–><mkdirdir="${dist}/lib"/><!–Puteverythingin${build}intotheMyProject-${DSTAMP}.jarfile–><jardestfile="${dist}/HelloWorld-${DSTAMP}.jar"basedir="${build}"><manifest><attributename="Built-By"value="${user.name}"/><attributename="Main-Class"value="org.javaresearch.HelloWorld"/></manifest></jar></target><targetname="run"depends="build"description="runHelloWorld"><javaclasspath="${build}"classname="org.javaresearch.HelloWorld"></java></target><targetname="runjar"depends="jar"description="runHelloWorldinjarfile"><javajar="${dist}/HelloWorld-${DSTAMP}.jar"fork="true"failonerror="true"maxmemory="128m"><argvalue="-h"/><classpath><pathelementlocation="${dist}/HelloWorld-${DSTAMP}.jar"/><pathelementpath="${java.class.path}"/></classpath></java></target><targetname="clean"description="cleanup"><!–Deletethe${build}and${dist}directorytrees–><deletedir="${build}"/><deletedir="${dist}"/></target></project>
<project name="HelloWorld" default="jar" basedir=".">    <description>        building HelloWorld!    </description>  <!-- set global properties for this build -->  <property name="src" location="src"/>  <property name="build" location="classes"/>  <property name="dist"  location="dist"/>  <property name="user.name" value="qiuqiu"/>  <target name="init">    <!-- Create the time stamp -->    <tstamp/>    <!-- Create the classes directory structure used by compile -->    <mkdir dir="${build}"/>  </target>  <target name="build" depends="init"  description="build the source code" >    <!-- Compile the java code from ${src} into ${build} -->    <javac srcdir="${src}" destdir="${build}"/>  </target>  <target name="jar" depends="build"        description="generate the distribution" >    <!-- Create the distribution directory -->    <mkdir dir="${dist}/lib"/>    <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->  <jar destfile="${dist}/HelloWorld-${DSTAMP}.jar" basedir="${build}">   <manifest>    <attribute name="Built-By" value="${user.name}"/>    <attribute name="Main-Class" value="org.javaresearch.HelloWorld"/>   </manifest>  </jar>   </target>    <target name="run" depends="build" description="run HelloWorld">    <java classpath="${build}" classname="org.javaresearch.HelloWorld"></java>  </target>  <target name="runjar" depends="jar" description="run HelloWorld in jar file">         <java jar="${dist}/HelloWorld-${DSTAMP}.jar"           fork="true"           failonerror="true"           maxmemory="128m"           >         <arg value="-h"/>         <classpath>           <pathelement location="${dist}/HelloWorld-${DSTAMP}.jar"/>           <pathelement path="${java.class.path}"/>         </classpath>       </java>  </target>    <target name="clean"        description="clean up" >    <!-- Delete the ${build} and ${dist} directory trees -->    <delete dir="${build}"/>    <delete dir="${dist}"/>  </target></project>

6:ant使用指南(为了方便没有资源分的朋友下载,这里给大家0资源分): http://download.csdn.net/source/3529167

有不足之处欢迎指正

上帝从不埋怨人们的愚昧,人们却埋怨上帝的不公平

图解教你如何使用ANT打包java程序

相关文章:

你感兴趣的文章:

标签云: