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”文件。- packagehello.ant;
- publicclassHelloAnt{
- <spanstyle="white-space:pre"></span>publicstaticvoidmain(String[]args){
- <spanstyle="white-space:pre"></span>System.out.println("helloant,ant的第一次接触,好棒!");
- <spanstyle="white-space:pre"></span>}
- }
在项目根目录(hello-ant\)写1个文件:ant执行配置文件build.xml
“build.xml”文件- <?xmlversion="1.0"encoding="GB2312"?>
- <!--一个项目,可包含很多任务组(target)-->
- <projectdefault="main"basedir=".">
- <spanstyle="white-space:pre"></span><!--项目中的一个任务组,可包含很多任务(task:javac,java...)-->
- <spanstyle="white-space:pre"></span><targetname="main">
- <spanstyle="white-space:pre"></span><!--编译-->
- <spanstyle="white-space:pre"></span><javacsrcdir="src\main\hello\ant"destdir="build\classes"/>
- <spanstyle="white-space:pre"></span><!--运行-->
- <spanstyle="white-space:pre"></span><javaclassname="hello.ant.HelloAnt">
- <spanstyle="white-space:pre"></span><classpath>
- <spanstyle="white-space:pre"></span><pathelementpath="build\classes"/>
- <spanstyle="white-space:pre"></span></classpath>
- <spanstyle="white-space:pre"></span></java>
- <spanstyle="white-space:pre"></span></target>
- </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了,这是答案:
命令提示符窗口
- D:\temp\hello-ant>ant-filebuild.xml
- Buildbuild.xml
- main:
- [javac]Compiling1sourcefiletoD:\temp\hello-ant\build\classes
- [java]helloant,ant的第一次接触,好棒!
- BUILDSUCCESSFUL
- Totaltime:2seconds
- D:\temp\hello-ant>
检查一下build/classes目录,哦,看到编译过的文件就在这里:
build/classes/hello/ant/HelloAnt.class.
hello ant 进级
我们要改进build.xml,让它做更多的事情:
定义全局变量
初始化,主要是建立目录
编译 (已有)
打包为jar
建立API documentation
生成distribution产品
凡事都讲究平衡,你要ant给你做更多事,当然要累一点点,不过只用累一次,以后的代码修改后的构建都是"一键式"完成,我们制作一个hello的简单例子,你可以自己做j2ee的练习。
我们要扩充目录结构,使它更像回事:
- :\src,\docs,\lib是自己组织的文件结构,\build,\dist是ant动态生成的成品。
- \src源文件:java源,源,jsp源,xml配置.....
- \src\mainjava源
- \src\window,unix,liunx的执行,我们的简单只有一个:
- run.bat:javahello.ant.HelloAnt
- \docs手写说明文档
- \lib程序所需类库的jar,比如j2ee.jar,mail,jar...
- \build用ant动态生成的构建目录
- \build\classes编译的类文件
- \build\docscopy"\docs"的手写说明文档,和ant生成的api文档
- \build\lib放置我们自己的HelloAnt.class打包成品hello-ant.jar
- \dist\bincopy"\src\"得执行文件
- \dist\docscopy"\build\docs"的文档
- \dist\lib除了copy"\build\lib"下的hello-ant.jar外,
- 还应copy"\lib"的程序所需jar,这里我们没有。
以上是我学老外的文件组织,大家可以按照自己的爱好组织
我们编写必要的文件:
hello.ant. HelloAnt.java
src\.bat
- @echooff
- echo========================================================
- echo请先设置Environment
- echo.
- echoJAVA_HOME:%JAVA_HOME%
- echo======================================================
- %java_home%\bin\java-classpath..\lib\hello-ant.jarhello.ant.HelloAnt
- pause
\docs\index.html 随便写一个手写的文档
hello ant 软件项目手册docs
--------------------------------------------------------------------------------
\build.xml 配置文件
- <?xmlversion="1.0"encoding="GB2312"?>
- <!--
- =======================================================================
- hello-ant项目,学习ant工具的第2个buildfile.
- 参照ant的jakarta-ant-1.6alpha的build.xml
- Copyright(c)2002TheNeusoftSoftwareFoundation.Allrights
- reserved.
- =======================================================================
- -->
- <!--
- 文档结构为:
- <project>
- <property/>全局变量的定义
- <property/>...
- <targetname="1">任务组(tasks)
- <javac></javac>一项javac任务
- ...
- <oneTask></ontTask>一项其它任务
- </target>
- <targetname="2">
- <javac></javac>
- ...
- <oneTask></ontTask>
- </target>
- </project>
- project代表一个项目,
- default:运行到名称为"dist"的target(任务组)
- basedir:基准路径。
- -->
- <projectdefault="dist"basedir=".">
- <!--
- ===================================================================
- 定义属性(propertytasks)
- 最好把用到的路径呀,名称呀都在这里定义成全局变量
- 例:定义
- <propertyname="a"="hello"/>
- 以后就可以这样用它:
- <propertyname="b"="${a}/b"/>
- 现在:b=="hello/b"
- ===================================================================
- -->
- <!--主要的系统环境属性-->
- <propertyenvironment="env"/><!--取window,unix...的环境变量-->
- <propertyname="java.home"="${env.JAVA_HOME}"/>
- <propertyname="ant.home"="${env.ANT_HOME}"/>
- <!--主要的app环境属性-->
- <propertyname="app.name"="hello-ant"/>
- <propertyname="app.jar"="${app.name}.jar"/>
- <propertyname="app.copyright"="Copyright(c)2002TheNeusoftSoftwareFoundation.Allrightsreserved."/>
- <!--app中src的属性-->
- <propertyname="src.dir"="src"/>
- <propertyname="src.main"="${src.dir}/main"/>
- <propertyname="src."="${src.dir}/"/>
- <!--app用到的lib-->
- <propertyname="lib.dir"="lib"/>
- <!--app的build目录中-->
- <propertyname="build.dir"="build"/>
- <propertyname="build.classes"="${build.dir}/classes"/>
- <propertyname="build.docs"="${build.dir}/docs"/>
- <propertyname="build.docs.api"="${build.docs}/api"/>
- <propertyname="build.lib"="${build.dir}/lib"/>
- <!--app的dist(distribution)目录中-->
- <propertyname="dist.dir"="dist"/>
- <propertyname="dist.bin"="${dist.dir}/bin"/>
- <propertyname="dist.docs"="${dist.dir}/docs"/>
- <propertyname="dist.lib"="${dist.dir}/lib"/>
- <!--app的docs目录中-->
- <propertyname="docs.dir"="docs"/>
- <!--
- 定义一组路径以后可以通过id重用这组路径,例:
- <javacsrcdir="src/main"destdir="build/classes">
- <classpathrefid="classpath"/>
- </javac>
- -->
- <pathid="classpath">
- <!--本项目只有一个java,用不上classpath,这里只是做个例子-->
- <pathelementlocation="${build.classes}"/>
- <pathelementpath="${java.home}/lib/tools.jar"/>
- </path>
- <!--
- ===================================================================
- init准备目录(FileTasks)
- 主要的目录结构通常是不会变的,一起生成他们
- ===================================================================
- -->
- <targetname="init">
- <!--清除以前目录-->
- <deletedir="${build.dir}"fail="false"/>
- <deletedir="${dist.dir}"fail="false"/>
- <!--准备目录-->
- <mkdirdir="${build.dir}"/>
- <mkdirdir="${build.classes}"/>
- <mkdirdir="${build.docs}"/>
- <mkdirdir="${build.docs.api}"/>
- <mkdirdir="${build.lib}"/>
- <mkdirdir="${dist.dir}"/>
- <mkdirdir="${dist.bin}"/>
- <mkdirdir="${dist.lib}"/>
- </target>
- <!--
- ===================================================================
- Buildthecode(CompileTasks,FileTasks)
- ===================================================================
- -->
- <targetname="build"depends="init">
- <!--编译-->
- <javacsrcdir="${src.main}"destdir="${build.classes}">
- <classpathrefid="classpath"/>
- </javac>
- </target>
- <!--
- ===================================================================
- 打包文档(ArchiveTasks)
- Createtheprojectjars:xxx1.jarandxxx2.jar
- ===================================================================
- -->
- <targetname="jars"depends="build">
- <jarbasedir="${build.classes}"jarfile="${build.lib}/${app.jar}"/>
- </target>
- <!--
- ===================================================================
- CreatestheAPIdocumentation
- ===================================================================
- -->
- <targetname="javadocs"
- depends="jars"
- deion="-->createstheAPIdocumentation">
- <!--copydocs手册...-->
- <copytodir="${build.docs}">
- <filesetdir="${docs.dir}"/>
- </copy>
- <javadocpackagenames="hello.ant.*"
- sourcepath="${src.main}"
- defaultexcludes="yes"
- destdir="${build.docs.api}"
- author="true"
- version="true"
- use="true"
- windowtitle="DocsAPI">
- <doctitle><![CDATA[<h1>helloantDocsAPI</h1>]]></doctitle>
- <bottom><![CDATA[<i>${app.copyright}</i>]]></bottom>
- <tagname="todo"scope="all"deion="Todo:"/>
- </javadoc>
- </target>
- <!--
- ===================================================================
- Createthedistributionthatcanrun(ArchiveTasks)
- 主要是从各目录中把该copy的copy上
- ===================================================================
- -->
- <targetname="dist"depends="javadocs">
- <!--copybin执行文件-->
- <copytodir="${dist.bin}">
- <filesetdir="${src.}/"/>
- </copy>
- <copytodir="${dist.docs}">
- <filesetdir="${build.docs}/"/>
- </copy>
- <!--copylib文件-->
- <copytodir="${dist.lib}">
- <filesetdir="${build.lib}/"/>
- </copy>
- </target>
- <!--
- ===================================================================
- Cleanseverything(FileTasks)
- 例如可以删除build中的文件,留给你发挥吧
- ===================================================================
- -->
- </project>
build.xml多了些,但其实很简单:(注释比较详细可以参照,这里再简单说一下)
一个build.xml包含一个工程的自动化处理的完整xml说明,并且基本由3种东东组成:
<project >
1.全局变量的定义
<property/>
2.任务组
<target>
3.许多单项任务... 像copy,delete,javac,jar...
<task1/>
<task2/>
<task3/>
</target>
</project>