Java Ant build.xml详解

1,什么是ant
ant是构建工具
2,什么是构建
概念到处可查到,形象来说,你要把代码从某个地方拿来,编译,再拷贝到某个地方去等等操作,当然不仅与此,但是主要用来干这个
3,ant的好处
跨平台   –因为ant是使用java实现的,所以它跨平台
使用简单–与ant的兄弟make比起来
语法清晰–同样是和make相比
功能强大–ant能做的事情很多,可能你用了很久,你仍然不知道它能有多少功能。当你自己开发一些ant插件的时候,你会发现它更多的功能。
4,ant的兄弟make
ant做的很多事情,大部分是曾经有一个叫make的所做的,不过对象不同,make更多应用于c/c++ ,ant更多应用于Java。当然这不是一定的,但大部分人如此。
一,构建ant环境
要使用ant首先要构建一个ant环境,步骤很简单:
1),安装jdk,设置JAVA_HOME ,PATH ,CLASS_PATH(这些应该是看这篇文章的人应该知道的)
2),下载ant 地址http://www.apache.org/找一个你喜欢的版本,或者干脆最新的版本
3),解压ant 你得到的是一个压缩包,解压缩它,并把它放在一个尽量简单的目录,例如D:\ant-1.6虽然你不一 定要这么做,但这么做是有好处的。
4),设置ANT_HOME, PATH中添加ANT_HOME目录下的bin目录(我设置的:ANT_HOME:D:\apache-ant-1.8.2,PATH:%ANT_HOME%\bin)
5),测试一下你的设置,开始–>运行–>cmd进入命令行–>键入 ant 回车,如果看到
Buildfile: build.xml does not exist!
Build failed
那么恭喜你你已经完成ant的设置
二,体验ant
就像每个语言都有HelloWorld一样,一个最简单的应用能让人感受一下Ant
1,首先你要知道你要干什么,我现在想做的事情是:
编写一些程序
编译它们
把它打包成jar包
把他们放在应该放置的地方
运行它们
这里为了简单起见只写一个程序,就是HelloWorld.java程序代码如下:
package test.ant;
public class HelloWorld{
public static void main(String[] args){
System.out.println("Hello world1");
}
};

2,为了达到上边的目的,你可以手动的用javac 、copy 、jar、java来完成,但是考虑一下如果你有成百上千个类,在多次调试,部署的时候,一次次的javac 、copy、jar、
java那将是一份辛苦的工作。现在看看ant怎么优雅的完成它们。
要运行ant需要有一个build.xml虽然不一定要叫这个名字,但是建议你这么做
下边就是一个完整的build.xml,然后我们来详细的解释每一句
<?xml version="1.0" encoding="UTF-8" ?>
<project name="HelloWorld" default="run" basedir=".">
<property name="src" value="src"/>
<property name="dest" value="classes"/>
<property name="hello_jar" value="hello1.jar"/>
<target name="init">
<mkdir dir="${dest}"/>
</target>
<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${dest}"/>
</target>
<target name="build" depends="compile">
<jar jarfile="${hello_jar}" basedir="${dest}"/>
</target>
<target name="run" depends="build">
<java classname="test.ant.HelloWorld" classpath="${hello_jar}"/>
</target>
<target name="clean">
<delete dir="${dest}" />
<delete file="${hello_jar}" />
</target>
<target name="rerun" depends="clean,run">
<ant target="clean" />
<ant target="run" />
</target>
</project>

解释:
<?xml version="1.0" encoding="UTF-8" ?>
build.xml中的第一句话,没有实际的意义
<project name="HelloWorld" default="run" basedir=".">
</project>

ant的所有内容必须包含在这个里边,name是你给它取的名字,basedir故名思意就是工作的根目录 .代表当前目录。default代表默认要做的事情。
<property name=”src” value=”src”/>
类似程序中的变量,为什么这么做想一下变量的作用
<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${dest}"/>
</target>

把你想做的每一件事情写成一个target ,它有一个名字,depends是它所依赖的target,在执行这个target 例如这里的compile之前ant会先检查init是否曾经被执行过,如果执行
过则直接直接执行compile,如果没有则会先执行它依赖的target例如这里的init,然后在执行这个target
如我们的计划
编译:
<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${dest}"/>
</target>

做jar包:
<target name="build" depends="compile">
<jar jarfile="${hello_jar}" basedir="${dest}"/>
</target>

运行:
<target name="run" depends="build">
<java classname="test.ant.HelloWorld" classpath="${hello_jar}"/>
</target>

为了不用拷贝,我们可以在最开始定义好目标文件夹,这样ant直接把结果就放在目标文件夹中了
新建文件夹:
<target name="init">
<mkdir dir="${dest}"/>
</target>

为了更多一点的功能体现,又加入了两个target
删除生成的文件
<target name="clean">
<delete dir="${dest}" />
<delete file="${hello_jar}" />
</target>

再次运行,这里显示了如何在一个target里边调用其他的target
<target name="rerun" depends="clean,run">
<ant target="clean" />
<ant target="run" />
</target>

好了,解释完成了,下边检验一下你的ant吧
新建一个src的文件夹,然后把HelloWorld.java按照包目录放进去
做好build.xml文件,最好将这些放到一个文件夹中,在cmd中进入该文件夹,
在命令行下键入ant ,你会发现一个个任务都完成了。每次更改完代码只需要再次键入ant
有的时候我们可能并不想运行程序,只想执行这些步骤中的某一两个步骤,例如我只想重新部署而不想运行,键入
ant build
ant中的每一个任务都可以这样调用ant + target name
好了,这样一个简单的ant任务完成了。

一,什么时候使用ant
也许你听到别人说起ant,一时冲动准备学习一下ant,当你看完了上边的第一个实例,也许你感觉ant真好,也许你感觉ant不过如此,得出这些结论都不能说错,虽然ant很好用,
但并不是在任何情况下都是最好的选择,例如windows上有更多更简单,更容易使用的工具,比如eclipse+myeclipse eclipse+wtp等等,无论是编译,部署,运行使用起来比ant更
容易,方便但有些情况则是ant发挥的好地方:
1,服务器上部署的时候
当你的程序开发完成,部署人员要部署在服务器上的时候,总不能因为因为安装一个程序就配置一个eclipse+myeclipse吧,ant在这个时候是个很好的选择,因为它小巧,容易配
置,你带着你写好的build.xml到任何一台服务器上,只需要做简单的修改(一些设定,例如目录),然后一两个命令完成,这难道不是一件美好的事情吗。
2,linux上,很多时候是这样的,程序开发是在windows下,但是程序要在linux或者unix上运行,在linux或者
在unix(特别是unix上)部署是个麻烦的事情,这个时候ant的特点又出来了,因为ant是跨平台的,你在build.xml可以在大多数操作系统上使用,基本不需要修改。
3,当服务器维护者不懂编程的时候
很多人都有过这样的经历,使用你们程序的人,并不懂得写程序。你得程序因为版本更新,因为修正bug需要一次又一次得重新部署。这个时候你会发现教一个人是如此得困难。但
是有ant后,你只需要告诉他,输入ant xxx等一两个命令,一切ok.
以上是我遇到得一些情况。
看完以上得情况,好好考虑一下,你是否需要使用ant,如果是继续。进一步学习一个稍微复杂一点点的ant
在实际的工作过程中可能会出现以下一些情况,一个项目分成很多个模块,每个小组或者部门负责一个模块,为了测试,他们自己写了一个build.xml,而你负责把这些模块组合到
一起使用,写一个build.xml
这个时候你有两种选择:
1,自己重新写一个build.xml ,这将是一个麻烦的事情
2,尽量利用他们已经写好的build.xml,减少自己的工作
举个例子:
假设你下边有三个小组,每个小组负责一个部分,他们分别有一个src 和一个写好的build.xml
这个时候你拿到他们的src,你需要做的是建立三个文件夹src1 ,src2, src3分别把他们的src和build.xml放进去,然后写一个build.xml
<?xml version="1.0" encoding="UTF-8" ?>
<project name="main" default="build" basedir=".">
<property name="bin" value="${basedir}\bin" />
<property name="src1" value="${basedir}\src1" />
<property name="src2" value="${basedir}\src2" />
<property name="src3" value="${basedir}\src3" />
<target name="init">
<mkdir dir="${bin}" />
</target>
<target name="run">
<ant dir="${src1}" target="run" />
<ant dir="${src2}" target="run" />
<ant dir="${src3}" target="run" />
</target>
<target name="clean">
<ant dir="${src1}" target="clean" />
<ant dir="${src2}" target="clean" />
<ant dir="${src3}" target="clean" />
</target>
<target name="build" depends="init,call">
<copy todir="${bin}">
<fileset dir="${src1}">
<include name="*.jar" />
</fileset>
<fileset dir="${src2}">
<include name="*.jar" />
</fileset>
<fileset dir="${src3}">
<include name="*.jar" />
</fileset>
</copy>
</target>
<target name="rebuild" depends="build,clean">
<ant target="clean" />
<ant target="build" />
</target>
</project>

ok你的任务完成了。ok,上边你完成了任务,但是你是否有些感触呢,在那些build.xml中,大多数是重复的,而且更改一次目录需要更改不少东西。是否能让工作做的更好一点呢,答案是肯定的。
引入两个东西:
1,propery
2,xml include
这两个东西都有一个功能,就是能把build.xml中<propery />中的内容分离出来,共同使用
除此之外它们各有特点:
propery的特点是维护简单,只需要简单的键值对,因为并不是所有人都喜欢xml的格式
xml include的特点是不单可以提取出属性来,连target也可以。
还是以前的例子:
例如我们想把src1 src2 src3这三个属性从xml中提出来,可以新建一个文件叫all.properties
里边的内容
src1=D:\\study\\ant\\src1
src2=D:\\study\\ant\\src2
src3=D:\\study\\ant\\src3

然后你的build.xml文件可以这样写,别人只需要更改配置文件,而不许要更改你的build.xml文件了
<?xml version="1.0" encoding="UTF-8" ?>
<project name="main" default="build" basedir=".">
<property file="all.properties" />
<property name="bin" value="${basedir}\bin" />
<target name="init">
<mkdir dir="${bin}" />
</target>
<target name="run">
<ant dir="${src1}" target="run" />
<ant dir="${src2}" target="run" />
<ant dir="${src3}" target="run" />
</target>
<target name="clean">
<ant dir="${src1}" target="clean" />
<ant dir="${src2}" target="clean" />
<ant dir="${src3}" target="clean" />
</target>
<target name="build" depends="init,call">
<copy todir="${bin}">
<fileset dir="${src1}">
<include name="*.jar" />
</fileset>
<fileset dir="${src2}">
<include name="*.jar" />
</fileset>
<fileset dir="${src3}">
<include name="*.jar" />
</fileset>
</copy>
</target>
<target name="rebuild" depends="build,clean">
<ant target="clean" />
<ant target="build" />
</target>
<target name="test">
<ant dir="${src1}" target="test" />
<ant dir="${src2}" target="test" />
<ant dir="${src3}" target="test" />
</target>
</project>

如果你自己看的话你会看到这样一个target
<target name="test">
<ant dir="${src1}" target="test" />
<ant dir="${src2}" target="test" />
<ant dir="${src3}" target="test" />
</target>

有的时候你想给每个小组的build.xml加入几个target,一种做法是每个里边写,然后在这里调用
但是有一种更好的方法。
你可以写一个include.xml文件,内容如下
<?xml version="1.0" encoding="UTF-8" ?>
<property name="src" value="src"/>
<property name="dest" value="classes"/>
<target name="test" >
<ant target="run" />
</target>

然后更改你三个小组的build.xml文件,每个里边加入如下内容
<!--include a xml file ,it can be common propery ,can be also a target   -->
<!DOCTYPE project [
<!ENTITY share-variable SYSTEM "file:../include.xml">
]>
&share-variable;

变成如下的样子
这个时候,你只要在include.xml添加propery , 添加target,三个build.xml会同时添加这些propery和target
而且不会让三个组的build.xml变得更复杂。
<?xml version="1.0" encoding="UTF-8" ?>
<!--include a xml file ,it can be common propery ,can be also a target   -->
<!DOCTYPE project [
<!ENTITY share-variable SYSTEM "file:../include.xml">
]>
<project name="HelloWorld" default="run" basedir=".">
<!--use the include   -->
&share-variable;
<!--defined the property-->
<!--via include
<property name="src" value="src"/>
<property name="dest" value="classes"/>
-->
<property name="hello_jar" value="hello1.jar"/>
<!--define the op-->
<target name="init">
<mkdir dir="${dest}"/>
</target>
<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${dest}"/>
</target>
<target name="build" depends="compile">
<jar jarfile="${hello_jar}" basedir="${dest}"/>
</target>
<target name="run" depends="build">
<java classname="test.ant.HelloWorld" classpath="${hello_jar}"/>
</target>
<target name="clean">
<delete dir="${dest}" />
<delete file="${hello_jar}" />
</target>
<target name="rerun" depends="clean,run">
<ant target="clean" />
<ant target="run" />
</target>
</project>

掌握了上边的那些内容之后,你就知道如何去写一个好的ant,但是你会发现当你真的想去做的时候,你不能马上作出好的build.xml,因为你知道太少的ant的默认提供的命令.这
个时候如果你想完成任务,并提高自己,有很多办法:
1,很多开源的程序都带有build.xml,看看它们如何写的
2,ant的document,里边详细列写了ant的各种默认命令,及其丰富
3,google,永远不要忘记它
ok,在这之后随着你写的ant build越来越多,你知道的命令就越多,ant在你的手里也就越来越强大了。
这个是一个慢慢积累的过程。ant的例子很好找,各种开源框架都会带有一个build.xml仔细看看,会有很大收获
另外一个经常会用到的,但是在开源框架的build.xml一般没有的是cvs
如果使用的是远程的cvs,可以这样使用
<xml version="1.0" encoding="utf-8"?>
<project>
<property name="cvsroot" value=":pserver:wang:@192.168.1.2:/cvsroot"/>
<property name="basedir" value="/tmp/testant/"/>
<property name="cvs.password" value="wang"/>
<property name="cvs.passfile" value="${basedir}/ant.cvspass"/>
<target name="initpass">
<cvspass cvsroot="${cvsroot}" password="${cvs.password}" passfile="${cvs.passfile}"/>
</target>
<target name="checkout" depends="initpass">
<cvs cvsroot="${cvsroot}" command="checkout" cvsrsh="ssh" package="myproject" dest="${basedir}"
passfile="${cvs.passfile}"/>
</target>

</project>在eclipse里边先天支持ant,所以你可以在eclipse里边直接写build.xml
因为eclipse提供了提示功能,自动补充功能,它能让你事半功倍。
使用方法,只需要建立一个工程,然后建立一个叫build.xml的文件。然后就可以在里边写你的ant build了
但是时刻记住http://www.apache.org/永远能找到你需要的东西

25 Responses so far.

  1. quest bars says:
    Thank you for every other wonderful article.
    The place else may anyone get that kind of information in such an ideal means of writing?
    I have a presentation next week, and I am at the search for such
    information.
  2. quest bars says:
    Thanks for one’s marvelous posting! I actually enjoyed reading it, you’re a great author.I will be
    sure to bookmark your blog and may come back sometime soon. I want
    to encourage you continue your great posts, have
    a nice evening!
  3. Quest Bars says:
    I was extremely pleased to uncover this great site.
    I need to to thank you for ones time for this particularly fantastic
    read!! I definitely enjoyed every bit of it and i also have you bookmarked to look at new information on your blog.
  4. Thanks for the auspicious writeup. It if truth be told was once a enjoyment account it.

    Glance complex to far added agreeable from you!
    By the way, how could we communicate?

  5. Great post however , I was wanting to know if you could
    write a litte more on this subject? I’d be very grateful if you could elaborate a little bit
    further. Kudos!
  6. Hey! I know this is kinda off topic but I was wondering if you knew where I could find a captcha
    plugin for my comment form? I’m using the same blog platform as yours and I’m having
    trouble finding one? Thanks a lot!
  7. Amazing blog! Do you have any suggestions for aspiring writers?
    I’m planning to start my own blog soon but I’m a little
    lost on everything. Would you propose starting with a free platform like WordPress or go for a paid option? There are so many choices out there that I’m totally overwhelmed ..
    Any ideas? Appreciate it!
  8. Thanks designed for sharing such a fastidious thinking, paragraph is good, thats why i have read it
    entirely
  9. I think what you composed made a ton of sense. But, what
    about this? suppose you were to write a killer headline?
    I am not saying your content is not solid, but what if you added a
    title that grabbed people’s attention? I mean Java Ant build.xml详解 – Huang Shiyang is a
    little boring. You should look at Yahoo’s home page and note how they create post
    titles to get people to open the links. You might try adding a video or a related picture
    or two to get people excited about what you’ve got to say.

    Just my opinion, it could make your website a little bit more
    interesting.

  10. These are genuinely wonderful ideas in about blogging.
    You have touched some fastidious things here. Any way keep up wrinting.
  11. I’m not sure why but this site is loading very slow for me.
    Is anyone else having this problem or is it a problem on my end?
    I’ll check back later on and see if the problem still exists.
  12. Hello! This is kind of off topic but I need some advice from
    an established blog. Is it difficult to set up your own blog?
    I’m not very techincal but I can figure things out pretty fast.
    I’m thinking about creating my own but I’m not sure where to start.
    Do you have any ideas or suggestions? Thanks
  13. This page definitely has all of the information and facts I wanted about this subject and didn’t know who to ask.
  14. Hi there i am kavin, its my first occasion to commenting anyplace,
    when i read this article i thought i could also make comment
    due to this sensible piece of writing.
  15. hi!,I really like your writing very a lot!
    proportion we communicate extra about your post on AOL?
    I need a specialist on this space to unravel my problem.

    Maybe that is you! Looking ahead to look you.

  16. It’s actually a cool and useful piece of information. I am satisfied that you simply shared this helpful information with
    us. Please keep us informed like this. Thank you for sharing.
  17. Hey there! Quick question that’s completely off topic. Do you know how to make your site mobile
    friendly? My site looks weird when viewing from my iphone 4.
    I’m trying to find a template or plugin that might be able to correct this problem.
    If you have any recommendations, please share. Cheers!
  18. I think this is among the most vital info for me. And i am glad reading your article.
    But wanna remark on few general things, The web site style is wonderful, the articles is really nice : D.
    Good job, cheers
  19. Highly descriptive blog, I liked that bit. Will there be a part
    2?
  20. I was recommended this blog by means of my
    cousin. I’m now not certain whether this publish is
    written by him as nobody else understand
    such designated approximately my problem. You’re incredible!
    Thank you!
  21. Quality content is the main to invite the users to
    go to see the website, that’s what this web page is
    providing.
  22. I love what you guys tend to be up too. This sort of clever work and reporting!
    Keep up the great works guys I’ve included you guys to
    my personal blogroll.
  23. bookmarked!!, I like your site!
  24. These are really fantastic ideas in regarding
    blogging. You have touched some fastidious things here.
    Any way keep up wrinting.
  25. This design is steller! You most certainly know how to keep a reader amused. Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Fantastic job. I really loved what you had to say, and more than that, how you presented it. Too cool!

Leave a Reply to Tommye Murnock Cancel reply