ant 01——编写ant:build.xml的方法

ant 是apache的java子项目"jakarta"的子项目.你可以选择当前的版本,,window版

解压后ant_home用来方便访问。并确保你也设置了java_home 。
set ant_home=D:\java\kit\ant\jakarta-ant-1.5.1 这是我的目录

hello ant

我们要开发一个java类:其内容只有一句,输出"hello ant"字符串。并使用ant完成编译和运行工作,这个例子只是为了跑通ant,不附加多余的东西。

下面是:“hello.ant.HelloAnt.java”文件。 
[java]  view plain copy
  1. package hello.ant;  
  2. public class HelloAnt{  
  3. <span style="white-space:pre">  </span>public static void main(String[] args){  
  4. <span style="white-space:pre">      </span>System.out.println("hello ant,ant 的第一次接触,好棒!");  
  5. <span style="white-space:pre">  </span>}   
  6. }   

在项目根目录(hello-ant\)写1个文件:ant执行配置文件build.xml

“build.xml”文件

[html]  view plain copy
  1. <?xml version="1.0" encoding="GB2312" ?>  
  2. <!-- 一个项目,可包含很多任务组(target) -->  
  3. <project default="main" basedir=".">  
  4. <span style="white-space:pre">  </span><!-- 项目中的一个任务组,可包含很多任务(task:javac,java...) -->  
  5. <span style="white-space:pre">  </span><target name="main">  
  6. <span style="white-space:pre">      </span><!--编译-->  
  7. <span style="white-space:pre">      </span><javac srcdir="src\main\hello\ant" destdir="build\classes"/>  
  8. <span style="white-space:pre">      </span><!--运行-->  
  9. <span style="white-space:pre">      </span><java classname="hello.ant.HelloAnt">  
  10. <span style="white-space:pre">          </span><classpath>  
  11. <span style="white-space:pre">              </span><pathelement path="build\classes"/>  
  12. <span style="white-space:pre">          </span></classpath>  
  13. <span style="white-space:pre">      </span></java>  
  14. <span style="white-space:pre">  </span></target>  
  15. </project>   

ok,一切大功告成,哦,不,还没有运行它。

dos下进入hello-ant的目录,即build.xml所在的目录,我们要用ant工具执行它 ,

执行: %ant_home%/bin/ant -file build.xml 用ant工具执行当前目录下的配置文件build.xml

或 :ant -file build.xml 你如果设置%ant_home%/bin到path中

这次ok了,这是答案:

命令提示符窗口

[plain]  view plain copy
  1. D:\temp\hello-ant>ant -file build.xml  
  2. Build build.xml  
  3. main:  
  4. [javac] Compiling 1 source file to D:\temp\hello-ant\build\classes  
  5. [java] hello ant,ant 的第一次接触,好棒!  
  6. BUILD SUCCESSFUL  
  7. Total time: 2 seconds  
  8. D:\temp\hello-ant>   

检查一下build/classes目录,哦,看到编译过的文件就在这里:
build/classes/hello/ant/HelloAnt.class.

hello ant 进级

我们要改进build.xml,让它做更多的事情:

定义全局变量 
初始化,主要是建立目录 
编译 (已有) 
打包为jar 
建立API documentation 
生成distribution产品 
凡事都讲究平衡,你要ant给你做更多事,当然要累一点点,不过只用累一次,以后的代码修改后的构建都是"一键式"完成,我们制作一个hello的简单例子,你可以自己做j2ee的练习。

我们要扩充目录结构,使它更像回事:

[plain]  view plain copy
  1. :\src,\docs,\lib是自己组织的文件结构,\build,\dist是ant动态生成的成品。  
  2. \src 源文件:java源,源,jsp源,xml配置.....  
  3. \src\main java源  
  4. \src\ window,unix,liunx的执行,我们的简单只有一个:  
  5. run.bat: java hello.ant.HelloAnt  
  6. \docs 手写说明文档  
  7. \lib 程序所需类库的jar,比如j2ee.jar,mail,jar...  
  8. \build 用ant动态生成的构建目录  
  9. \build\classes 编译的类文件  
  10. \build\docs copy "\docs"的手写说明文档,和ant生成的api文档  
  11. \build\lib 放置我们自己的HelloAnt.class打包成品hello-ant.jar  
  12. \dist\bin copy "\src\" 得执行文件  
  13. \dist\docs copy "\build\docs" 的文档  
  14. \dist\lib 除了copy "\build\lib"下的hello-ant.jar外,  
  15. 还应copy "\lib"的程序所需jar,这里我们没有。  

以上是我学老外的文件组织,大家可以按照自己的爱好组织

我们编写必要的文件:

hello.ant. HelloAnt.java

src\.bat 

[plain]  view plain copy
  1. @echo off  
  2. echo ========================================================  
  3. echo 请先设置 Environment  
  4. echo .  
  5. echo JAVA_HOME: %JAVA_HOME%  
  6. echo ======================================================  
  7. %java_home%\bin\java -classpath ..\lib\hello-ant.jar hello.ant.HelloAnt  
  8. pause   

\docs\index.html 随便写一个手写的文档 
hello ant 软件项目手册docs
--------------------------------------------------------------------------------

访问api文档
\build.xml 配置文件 

[html]  view plain copy
  1. <?xml version="1.0" encoding="GB2312" ?>  
  2. <!--  
  3. =======================================================================  
  4. hello-ant 项目 ,学习ant工具的第2个build file.  
  5. 参照ant的jakarta-ant-1.6alpha的build.xml  
  6. Copyright (c) 2002 The Neusoft Software Foundation. All rights  
  7. reserved.  
  8. =======================================================================  
  9. -->  
  10. <!--  
  11. 文档结构为:  
  12. <project>  
  13. <property/> 全局变量的定义  
  14. <property/>...  
  15. <target name="1"> 任务组(tasks)  
  16. <javac></javac> 一项javac任务  
  17. ...  
  18. <oneTask></ontTask> 一项其它任务  
  19. </target>  
  20. <target name="2">  
  21. <javac></javac>  
  22. ...  
  23. <oneTask></ontTask>  
  24. </target>  
  25. </project>  
  26. project代表一个项目,  
  27. default:运行到名称为"dist"的target(任务组)  
  28. basedir:基准路径。  
  29. -->  
  30. <project default="dist" basedir=".">  
  31. <!--  
  32. ===================================================================  
  33. 定义属性(property tasks)  
  34. 最好把用到的路径呀,名称呀都在这里定义成全局变量  
  35. 例:定义  
  36. <property name="a" ="hello"/>  
  37. 以后就可以这样用它:  
  38. <property name="b" ="${a}/b"/>  
  39. 现在:b=="hello/b"  
  40. ===================================================================  
  41. -->  
  42. <!--主要的系统环境属性-->  
  43. <property environment="env"/><!--取window,unix...的环境变量-->  
  44. <property name="java.home" ="${env.JAVA_HOME}"/>  
  45. <property name="ant.home" ="${env.ANT_HOME}"/>  
  46. <!--主要的app环境属性-->  
  47. <property name="app.name" ="hello-ant"/>  
  48. <property name="app.jar" ="${app.name}.jar"/>  
  49. <property name="app.copyright" =" Copyright (c) 2002 The Neusoft Software Foundation. All rights reserved."/>  
  50.   
  51. <!--app中src的属性-->  
  52. <property name="src.dir" ="src" />  
  53. <property name="src.main" ="${src.dir}/main"/>  
  54. <property name="src." ="${src.dir}/"/>  
  55. <!--app用到的lib-->  
  56. <property name="lib.dir" ="lib"/>  
  57. <!--app的build目录中-->  
  58. <property name="build.dir" ="build" />  
  59. <property name="build.classes" ="${build.dir}/classes"/>  
  60. <property name="build.docs" ="${build.dir}/docs"/>  
  61. <property name="build.docs.api" ="${build.docs}/api"/>  
  62. <property name="build.lib" ="${build.dir}/lib"/>  
  63. <!--app的dist (distribution) 目录中-->  
  64. <property name="dist.dir" ="dist"/>  
  65. <property name="dist.bin" ="${dist.dir}/bin"/>  
  66. <property name="dist.docs" ="${dist.dir}/docs"/>  
  67. <property name="dist.lib" ="${dist.dir}/lib"/>  
  68. <!--app的docs目录中-->  
  69. <property name="docs.dir" ="docs"/>  
  70. <!--  
  71. 定义一组路径以后可以通过id重用这组路径 ,例:  
  72. <javac srcdir="src/main" destdir="build/classes">  
  73. <classpath refid="classpath"/>  
  74. </javac>  
  75. -->  
  76. <path id="classpath">  
  77. <!--本项目只有一个java,用不上classpath,这里只是做个例子-->  
  78. <pathelement location="${build.classes}"/>  
  79. <pathelement path="${java.home}/lib/tools.jar"/>  
  80. </path>  
  81. <!--  
  82. ===================================================================  
  83. init 准备目录(File Tasks)  
  84. 主要的目录结构通常是不会变的,一起生成他们  
  85. ===================================================================  
  86. -->  
  87. <target name="init">  
  88. <!--清除以前目录-->  
  89. <delete dir="${build.dir}" fail="false" />  
  90. <delete dir="${dist.dir}" fail="false"/>  
  91. <!--准备目录-->  
  92. <mkdir dir="${build.dir}"/>  
  93. <mkdir dir="${build.classes}"/>  
  94. <mkdir dir="${build.docs}"/>  
  95. <mkdir dir="${build.docs.api}"/>  
  96. <mkdir dir="${build.lib}"/>  
  97. <mkdir dir="${dist.dir}"/>  
  98. <mkdir dir="${dist.bin}"/>  
  99. <mkdir dir="${dist.lib}"/>  
  100. </target>  
  101. <!--  
  102. ===================================================================  
  103. Build the code (Compile Tasks,File Tasks)  
  104. ===================================================================  
  105. -->  
  106. <target name="build" depends="init">  
  107. <!--编译-->  
  108. <javac srcdir="${src.main}" destdir="${build.classes}">  
  109. <classpath refid="classpath"/>  
  110. </javac>  
  111. </target>  
  112. <!--  
  113. ===================================================================  
  114. 打包文档(Archive Tasks)  
  115. Create the project jars: xxx1.jar and xxx2.jar  
  116. ===================================================================  
  117. -->  
  118. <target name="jars" depends="build">  
  119. <jar basedir="${build.classes}" jarfile="${build.lib}/${app.jar}"/>  
  120. </target>  
  121. <!--  
  122. ===================================================================  
  123. Creates the API documentation  
  124. ===================================================================  
  125. -->  
  126. <target name="javadocs"  
  127. depends="jars"  
  128. deion="--> creates the API documentation">  
  129. <!--copy docs 手册... -->  
  130. <copy todir="${build.docs}">  
  131. <fileset dir="${docs.dir}"/>  
  132. </copy>  
  133. <javadoc packagenames="hello.ant.*"  
  134. sourcepath="${src.main}"  
  135. defaultexcludes="yes"  
  136. destdir="${build.docs.api}"  
  137. author="true"  
  138. version="true"  
  139. use="true"  
  140. windowtitle="Docs API">  
  141. <doctitle><![CDATA[<h1>hello ant Docs API</h1>]]></doctitle>  
  142. <bottom><![CDATA[<i>${app.copyright}</i>]]></bottom>  
  143. <tag name="todo" scope="all" deion="To do:" />  
  144. </javadoc>  
  145. </target>  
  146. <!--  
  147. ===================================================================  
  148. Create the distribution that can run (Archive Tasks)  
  149. 主要是从各目录中把该copy的copy上  
  150. ===================================================================  
  151. -->  
  152. <target name="dist" depends="javadocs">  
  153. <!--copy bin 执行文件 -->  
  154. <copy todir="${dist.bin}">  
  155. <fileset dir="${src.}/"/>  
  156. </copy>  
  157. <copy todir="${dist.docs}">  
  158. <fileset dir="${build.docs}/"/>  
  159. </copy>  
  160. <!-- copy lib 文件 -->  
  161. <copy todir="${dist.lib}">  
  162. <fileset dir="${build.lib}/"/>  
  163. </copy>  
  164. </target>  
  165. <!--  
  166. ===================================================================  
  167. Cleans everything(File Tasks)  
  168. 例如可以删除build中的文件,留给你发挥吧  
  169. ===================================================================  
  170. -->  
  171. </project>   

build.xml多了些,但其实很简单:(注释比较详细可以参照,这里再简单说一下)

一个build.xml包含一个工程的自动化处理的完整xml说明,并且基本由3种东东组成:

<project >

1.全局变量的定义
<property/>

2.任务组
<target>
3.许多单项任务... 像copy,delete,javac,jar...
<task1/>
<task2/>
<task3/>
</target>

</project>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值