Ant学习笔记(二)

        前面已经介绍了怎样通过Ant来编译和运行一个Java程序,下面我们接着来学习如何通过Ant来发布一个这样的工程。文件结构如图所示,先建立相应的目录结构,我们这里继续使用前面的HelloAnt类。

发布后的目录结构如下:

     hello-ant/src : 用来存放java源文件。
     hello-ant/docs : 项目用到的一些资源文件。
     hello-ant/lib:项目用到的第3方jar包和自己的jar包,这些jar包最后要发布到hello-ant/e/deploy/lib下。

     hello-ant/build:由Ant动态创建,存放编译后的classes文件。
     hello-ant/deploy:工程对外发布的目录,由ant动态创建。

   

     build.xml:

  1. <?xml version="1.0"  encoding="GB2312"?>
  2. <project default="deploy"  basedir=".">
  3. <!--  定义全局变量 -->
  4. <property name="root"  value="." />
  5. <property name="build.classes" value="${root}/build/classes" />
  6. <property name="app.name" value="hello-ant" />
  7. <property name="app.jar" value="${app.name}.jar" />
  8. <!--  初始化  构建相应目录 -->
  9. <target name="clearup" >
  10.        <delete dir="${root}/deploy" />
  11.        <mkdir dir="${root}/deploy/bin" />
  12.        <mkdir dir="${root}/deploy/docs" />
  13.        <mkdir dir="${root}/deploy/lib" />
  14. </target>
  15. <!-- 编译 -->
  16. <target name="compile" >
  17.         <delete dir="${root}/build" />
  18.         <mkdir dir="${root}/build/classes" />
  19.         <javac srcdir="src/hello/ant" destdir="${root}/build/classes" />
  20. </target>
  21. <!-- 打包build/classes/下的所有class 文件到 hello-ant.jar 中-->
  22. <target name="jar" depends="compile">
  23.         <jar basedir="${build.classes}" jarfile="${root}/lib/${app.jar}" />
  24. </target>
  25. <!-- 发布 -->
  26. <target name="deploy" depends="clearup, jar">
  27.        <copy todir="${root}/deploy/docs">
  28.            <fileset dir="${root}/docs">
  29.                 <include name="*.txt" />
  30.                 <include name="*.jsp" />
  31.                 <include name="*.jpg" />
  32.            </fileset>
  33.        </copy>
  34.       <copy file="${root}/lib/${app.jar}" todir="${root}/deploy/lib" />
  35. </target>
  36. </project>

        在DOS下执行
        D:/>cd hello-ant
        D:/hello-ant> ant  -file  build.xml
        Buildfile: build.xml

        clearup:
              [mkdir] Created dir: K:/hello-ant/deploy/bin
              [mkdir] Created dir: K:/hello-ant/deploy/docs
              [mkdir] Created dir: K:/hello-ant/deploy/lib
              [delete] Deleting directory K:/hello-ant/build
              [mkdir] Created dir: K:/hello-ant/build/classes

         compile:
              [javac] Compiling 1 source file to K:/hello-ant/build/classes

         jar:
              [jar] Building jar: K:/hello-ant/lib/hello-ant.jar

        deploy:
              [copy] Copying 3 files to K:/hello-ant/deploy/docs
              [copy] Copying 1 file to K:/hello-ant/deploy/lib

       BUILD SUCCESSFUL
       Total time: 1 second

       这样工程就发布完成了。

      target的depends属性表示这个target在执行时所要依靠的其他target。如果当前target需要依靠多个其他的target则在depends属性中用“,”间隔。这样ant会在执行此target的时候按照从左到右的顺序优先执行depends属性中指定的target。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值