使用Apache Ant

来源:互联网 发布:看上期尾数知下期尾数 编辑:程序博客网 时间:2024/04/30 06:45

要快速的学会使用一个新的工具,library,基本上有这么几个步骤吧:

  • 了解其基本概念:这是工具是干什么的,大概长的什么样,有什么特点与局限? 此时对其的认识还是比较抽象的
  • 下载安装其可用版本:保证其能运行,使用
  • 动手实践:根据教程,或者example,自己动手做个东西出来,这样就有一个非常直观的感受了

这些资料,一般在官方网站能够找全,如果没有,相信搜也能搜到。

废话讲完了,下面上ant。

 

基本概念



Apache Ant是java世界中的make(make是微软世界中的VC project, btw),是一个主要用来管理软件编译的工具。和make一样,ant也有target,dependency和rule的概念,只不过格式不一样: 
make:

target:dependencies
rules

ant

<target name="target3" depends="target1, target2" description="demo target" > 
<echo message="rules: everything in this section are rules"/>
</target>


那么,已经有了make,为什么还需要ant呢? apache的理由是: 

1. make的格式比较不标准,tab啊space啊容易出错,ant采用xml,感觉是要标准很多 
2. make多调用shell命令、程序,这对跨平台的java来讲是不能忍受的,ant采用java class实现的"task",和java跨一样的平台。

这个理由还是蛮靠谱的。

 

下载安装



如果只是使用ant的话,没必要自己没事找事下载source code编译,直接下binary版本:http://ant.apache.org/bindownload.cgi

  • .zip archive: apache-ant-1.8.2-bin.zip
  • .tar.gz archive: apache-ant-1.8.2-bin.tar.gz
  • .tar.bz2 archive: apache-ant-1.8.2-bin.tar.bz2

这三个包的内容是一样的,只不过适用于不同的平台,.zip是Windows下常用的zip压缩包;.tar.gz是tar打包,gzip压缩;.tar.bz2是tar打包,bzip2压缩。

安装很简单,就是解压缩:

  ant   +--- README, LICENSE, fetch.xml, other text files. //basic information   +--- bin  // contains launcher scripts   |   +--- lib  // contains Ant jars plus necessary dependencies   |   +--- docs // contains documentation   |      |   |      +--- images  // various logos for html documentation   |      |   |      +--- manual  // Ant documentation (a must read ;-)   |   +--- etc // contains xsl goodies to:            //   - create an enhanced report from xml output of various tasks.            //   - migrate your build files and get rid of 'deprecated' warning            //   - ... and more ;-)

注意ant需要用到jdk,而不仅仅是jre,你需要设置相关的环境变量: 

set ANT_HOME=D:\Source\Tools\apache-ant-1.8.2 
set JAVA_HOME=C:\Program Files\Java\jdk1.7.0
set PATH=%PATH%;%ANT_HOME%\bin

Ant其实是一个java程序,一般通过shell或者batch封装的script运行,比如windows下就是ant.bat,做一些初始化的工作,然后调用java ant-launcher.jar。

好,现在验证一下是否安装成功:

D:\Source\TestingArena\Java\Ant 
$ ant -version
Apache Ant(TM) version 1.8.2 compiled on December 20 2010

 

动手实践



ant manual的内容很多,但是要最快的学会一样东西,就是过一遍他的教程:Manual -> Tutorials,这些教程写的非常简单明了,而且提供了可以直接使用的代码与build.xml,我花了大概1个小时过了一遍前两个教程:HelloWorld和Write Tasks,就感觉对用ant写java程序,测试java程序有了很好的理解。

几个tips:

  • ant -diagnostics, ant -verbose, ant -debug对调试ant非常有用
  • 跑junit,你需要自行下载junit(https://github.com/KentBeck/junit/downloads), 命名成junit.jar并放到ant/lib目录中
  • 只要实现了public void execute()函数的java类就可以是一个ant task,ant会使用反射找到并执行它,当然,需要通过taskdef引入:
     
    <taskdef name="helloworld"
    classname="HelloWorld"
    classpath="helloworld.jar"/>
  • ant-contrib是一个比较常用的第三方task库,提供了不少辅助性质的task,如if,switch等控制语句. 他们往往提供了一个属性文件供你导入:
     
    <taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
    该文件内容是task名字与jar包中类名的对应:
    复制代码
    if=net.sf.antcontrib.logic.IfTask
    foreach=net.sf.antcontrib.logic.ForEach
    throw=net.sf.antcontrib.logic.Throw
    trycatch=net.sf.antcontrib.logic.TryCatchTask
    switch=net.sf.antcontrib.logic.Switch
    outofdate=net.sf.antcontrib.logic.OutOfDate
    复制代码
  • build.xml中的property类似于变量,它也可以在其中文件中定义并导入:
    复制代码
    <!-- build.properties: define the properties -->
    helloworld.property1=Hello
    helloworld.property2=World

    <!-- build.xml: import and use -->
    <property file="build.properties" />
    <target name="tryproperty" description="Use property file" >
    <echo message="${helloworld.property1} ${helloworld.property2}"/>
    </target>
    复制代码
0 0
原创粉丝点击