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

本文介绍如何在Eclipse中使用Ant构建一个简单的Java项目,包括创建项目、配置build.xml文件、执行构建任务及运行打包后的程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

    

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

 

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

     

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

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

     

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

     

 

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

      

  1. <project name="HelloWorld" default="jar" basedir=".">  
  2.     <description>  
  3.         building HelloWorld!  
  4.     </description>  
  5.   <!-- set global properties for this build -->  
  6.   <property name="src" location="src"/>  
  7.   <property name="build" location="classes"/>  
  8.   <property name="dist"  location="dist"/>  
  9.   <property name="user.name" value="qiuqiu"/>  
  10.   
  11.   <target name="init">  
  12.     <!-- Create the time stamp -->  
  13.     <tstamp/>  
  14.     <!-- Create the classes directory structure used by compile -->  
  15.     <mkdir dir="${build}"/>  
  16.   </target>  
  17.   
  18.   <target name="build" depends="init"  description="build the source code" >  
  19.     <!-- Compile the java code from ${src} into ${build} -->  
  20.     <javac srcdir="${src}" destdir="${build}"/>  
  21.   </target>  
  22.   
  23.   <target name="jar" depends="build"  
  24.         description="generate the distribution" >  
  25.     <!-- Create the distribution directory -->  
  26.     <mkdir dir="${dist}/lib"/>  
  27.   
  28.     <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->  
  29.   <jar destfile="${dist}/HelloWorld-${DSTAMP}.jar" basedir="${build}">  
  30.    <manifest>  
  31.     <attribute name="Built-By" value="${user.name}"/>  
  32.     <attribute name="Main-Class" value="org.javaresearch.HelloWorld"/>  
  33.    </manifest>  
  34.   </jar>    
  35.  </target>  
  36.     
  37.   <target name="run" depends="build" description="run HelloWorld">  
  38.     <java classpath="${build}" classname="org.javaresearch.HelloWorld"></java>  
  39.   </target>  
  40.   
  41.   <target name="runjar" depends="jar" description="run HelloWorld in jar file">  
  42.          <java jar="${dist}/HelloWorld-${DSTAMP}.jar"  
  43.            fork="true"  
  44.            failonerror="true"  
  45.            maxmemory="128m"  
  46.            >  
  47.          <arg value="-h"/>  
  48.          <classpath>  
  49.            <pathelement location="${dist}/HelloWorld-${DSTAMP}.jar"/>  
  50.            <pathelement path="${java.class.path}"/>  
  51.          </classpath>  
  52.        </java>  
  53.   </target>  
  54.     
  55.   <target name="clean"  
  56.         description="clean up" >  
  57.     <!-- Delete the ${build} and ${dist} directory trees -->  
  58.     <delete dir="${build}"/>  
  59.     <delete dir="${dist}"/>  
  60.   </target>  
  61. </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.youkuaiyun.com/source/3529167 

有不足之处欢迎指正

转载于:https://www.cnblogs.com/yangkai-cn/p/4017300.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值