打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Apache Ant 简介

Apache Ant 简介

    Apache Ant是目前事实上的Java应用的标准build脚本工具。使它大受欢迎的一个主要愿意是它的和灵活,允许程序员创建自己的Task来对Ant进行扩展。
    本文主要内容有:
  • 对Ant的简介
  • 介绍常用的Ant脚本

Ant的安装

    Apache Ant是Apache基金会下的一个项目,可以在http://ant.apache.org下载到。根据不同的平台下载不同的压缩包,直接解压到安装目录就可以了,不需要安装。解压后,在PATH环境变量中添加Ant的安装目录。

Ant简介

    我想大家都听说过很多build工具,如make,maven等。那么为什么我们选择Ant呢?简单说,Ant有以下这两个优点。
  • 使用java开发,并用xml存储build信息,因此是跨平台的。
  • 程序员可以自己扩展Ant。程序员可以自己写java程序来扩展Ant,创建自己的tasks。

    make这一类的工具是基于操作系统shell的,因此移植性不好。并且Ant也可以通过<exec>标签来实现调用shell,但这样会是以可移植性为代价的。
    Ant使用XML来存储build信息,在xml文件里有很多task的定义,默认使用的文件是build.xml。下图为Ant build文件的一般结构示意图:

FirstBuild.xml

    通过实例来说明Ant的build.xml文件的结构会更清晰一些,这里使用的build文件是FirstBuild.xml,它实现了创建一个文件夹并拷贝一个文件进入这个文件夹。
首先,要有<project>元素:
  1. <!--  
  2. A simple build script that creates a directory (dir.name), and copies  
  3. a file (file.name) to it.  
  4. -->  
  5. <project name=”MyFirstAntProject” basedir=”.” default=”copyfile”>  
 
<project>中的name属性标识工程名,basedir指示根目录,default标识默认执行的target。如果运行ant时不指定这些属性,Ant将执行这个target。
下一步,定义这个工程所使用的properties:
  1. <property name=”dir.name” value=”${basedir}/mydir”/>  
  2. <property name=”file.name” value=”file1.txt”/>  
 
这里定义了两个全局属性,分别是dir.name和file.name。这些属性是可选的,但使用属性会更方便,尤其是便于维护。一种更有效的方式是将这些属性放到一个专门文件里,从而使这个xml文件更加灵活,易于重用。
因为这个build文件很简单,所以没有task和path的定义。
最后,定义所要执行的targets。
  1. <target name=”makedirectory” description=”Create directory mydir”>  
  2. <mkdir dir=”${dir.name}”/>  
  3. </target>  
  4. <target name=”copyfile” depends=”makedirectory” description=”Copy files”>  
  5. <copy file=”${file.name}” todir=”${dir.name}”/>  
  6. </target>  
  7. <target name=”clean” description=”Clean up task”>  
  8. <delete dir=”${dir.name}”/>  
  9. </target  
  10. </project>  
 
注意copyfile中的depends,denpends属性意味着在执行copyfile之前,makedirectorytarget一定要先执行。
Ant的语法结构一般是:
  1. ant -buildfile <filename> <target-name>  
 
如果没有使用-fuildfile参数,那么Ant将默认使用build.xml,如果没有build.xml,那么Ant将报错,如下:
  1. $ ant  
  2. Buildfile: build.xml does not exist!  
  3. Build failed  
 
因为我们的build文件名不是build.xml,因此需要添加-buildfile这个参数。下面是使用-buildfile参数的结果:
  1. $ ant -buildfile FirstBuild.xml  
  2. Buildfile: FirstBuild.xml  
  3. makedirectory:  
  4. [mkdir] Created dir: /home/tomcat/AppendixB/mydir  
  5. copyfile:  
  6. [copy] Copying 1 file to /home/tomcat/AppendixB/mydir  
  7. BUILD SUCCESSFUL  
  8. Total time: 1 second  
 
在文件中定义的property可以被Ant的参数所覆盖,如下面使用yourdir取代mydir:
  1. $ ant -buildfile FirstBuild.xml -Ddir.name=yourdir  
  2. Buildfile: FirstBuild.xml  
  3. makedirectory:  
  4. [mkdir] Created dir: /home/tomcat/AppendixB/yourdir  
  5. copyfile:  
  6. [copy] Copying 1 file to /home/tomcat/AppendixB/yourdir  
  7. BUILD SUCCESSFUL  
  8. Total time: 1 second  
 
一般在build.xml中都会有clean这个target,其作用有些像卸载软件程序:
  1. $ ant -buildfile FirstBuild.xml clean  
  2. Buildfile: FirstBuild.xml  
  3. clean:  
  4. [delete] Deleting directory /home/tomcat/AppendixB/mydir  
  5. BUILD SUCCESSFUL  
  6. Total time: 0 seconds  
 

使用Ant创建web应用程序

    前面的那个Ant任务比较简单,下面介绍一个有些复杂又十分常用的Ant文件:我们要用Ant来构建一个web应用程序。web应用程序一般的目录结构是这样的:
src放置java文件,web放jsp等页面文件和配置文件,dist用来存放生成的war文件,build用来放编译好的servlet,lib用来放库文,doc用来放生成的javadoc,以及最主要的,build.xml。其中,build,dist和doc是使用Ant脚本来生成的。

build.xml

build.xml的第一部分:
  1. <!-- Ant build file for a sample web application -->  
  2. <project name=”SampleWebApplication” default=”compile” basedir=”.”>  
 
这一部分没什么好说的,前面都说了。
下一部分还是全局可用的properties。
  1. <property name=”tomcat.home” value=”/usr/tomcat/apache-tomcat-6 “/>  
  2. <property name=”app.name” value=”sampleWebapp”/>  
  3. <property name=”context.path” value=”/${app.name}”/>  
  4. <property name=”src.home” value=”${basedir}/src”/>  
  5. <property name=”web.home” value=”${basedir}/web”/>  
  6. <property name=”conf.home” value=”${basedir}/conf”/>  
  7. <property name=”lib.home” value=”${basedir}/lib”/>  
  8. <property name=”docs.home” value=”${basedir}/docs”/>  
  9. <property name=”build.home” value=”${basedir}/build”/>  
  10. <property name=”dist.home” value=”${basedir}/dist”/>  
  11. <property name=”war.file” value=”${dist.home}/${app.name}.war”/>  
  12. <!-- Configure properties to access the Tomcat Manager application -->  
  13. <property name=”tomcat.manager.url” value=”http://localhost:8080/manager”/>  
  14. <property name=”tomcat.username” value=”tomcat”/>  
  15. <property name=”tomcat.password” value=”tomcat”/>  
 
clean target,用于删除build和dist目录及其所有子目录。
  1. <!-- ====== Clean Target ====== -->  
  2. <target name=”clean”  
  3. description=”Cleanup- deletes everything generated by the ant script”>  
  4. <delete dir=”${build.home}”/>  
  5. <delete dir=”${dist.home}”/>  
  6. <delete dir=”${docs.home}”/>  
  7. </target>  
 
init target用于完成初始化工作,包括创建目录结构,初始化Java CLASSPATH。CLASSPATH中包含这个web应用程序使用的所有库文件。下面的代码将servlet-api.jar包进来。
  1. <!-- ====== All initializations: Classpath, directory structure ====== -->  
  2. <target name=”init”>  
  3. <mkdir dir=”${build.home}”/>  
  4. <mkdir dir=”${docs.home}”/>  
  5. <mkdir dir=”${dist.home}”/>  
  6. <!-- Classpath for compiling web application, generating javadocs -->  
  7. <path id=”classpath”>  
  8. <fileset dir=”${lib.home}”>  
  9. <include name=”*.jar”/>  
  10. </fileset>  
  11. <fileset dir=”${tomcat.home}/lib”>  
  12. <include name=”servlet-api.jar”/>  
  13. </fileset>  
  14. </path>  
  15. <property name=”classpath” refid=”classpath”/>  
  16. </target>  
 
compile target将编译src目录中所有的java文件,生成的class文件将被存放到destdir属性所指的目录中,compile必需是在init完成之后才能执行。如果使用compile命令,则将自动先执行init。
  1. <!-- ====== Compilation ====== -->  
  2. <target name=”compile” depends=”init”>  
  3. <echo message=”Classpath set to ${classpath}”/>  
  4. <javac srcdir=”${src.home}”  
  5. destdir=”${build.home}”  
  6. debug=”true”  
  7. classpath=”${classpath}”  
  8. deprecation=”true”>  
  9. </javac>  
  10. <!-- Copy all property files -->  
  11. <copy todir=”${build.home}”>  
  12. <fileset dir=”${conf.home}”/>  
  13. </copy>  
  14. </target>  
 
dist target将创建一个war文件。
  1. <!-- ====== Create a distributable WAR file ====== -->  
  2. <target name=”dist” depends=”compile”  
  3. description=”Creates the deployable WAR file”>  
  4. <war destfile=”${war.file}”  
  5. webxml=”${web.home}/WEB-INF/web.xml”>  
  6. <fileset dir=”${web.home}” excludes=”**/web.xml” />  
  7. <lib dir=”${lib.home}”/>  
  8. <classes dir=”${build.home}”/>  
  9. </war>  
  10. </target>  
 
javadoc target将为源码创建javadoc文档。
  1. <!-- ====== Create documentation (javadocs) ====== -->  
  2. <target name=”javadoc” depends=”init” description=”Creates the Javadocs for the  
  3. project”>  
  4. <javadoc sourcepath=”${src.home}”  
  5. packagenames=”com.foobar.*”  
  6. classpath=”${classpath}”  
  7. destdir=”${docs.home}”  
  8. windowtitle=”Javadoc for the Sample Web Application (TM)”>  
  9. </javadoc>  
 
all target将上面这些任务按在depends中的顺序组织起来。
  1. <!-- ====== All Target ====== -->  
  2. <target name=”all”  
  3. depends=”clean, prepare, compile, dist”  
  4. description=”Builds the web application and war file”/>  
 

使用Ant管理web应用程序

继续上面的那个build.xml文件,在其中添加下面的这些代码,就可以通过Ant管理这个web应用程序了。
  1. <!-- Configure properties to access the Tomcat Manager application -->  
  2. <property name=”tomcat.manager.url” value=”http://localhost:8080/manager”/>  
  3. <property name=”tomcat.username” value=”tomcat”/>  
  4. <property name=”tomcat.password” value=”tomcat”/>  
  5. <!-- Classpath for Tomcat ant tasks -->  
  6. <path id=”deployer.classpath”>  
  7. <fileset dir=”${tomcat.home}/lib”>  
  8. <include name=”*.jar”/>  
  9. </fileset>  
  10. </path  
  11. ...  
  12. <!-- ====== Manage web application ====== -->  
  13. <target name=”deploy” depends=”dist” description=”Deploy web application”>  
  14. <deploy url=”${tomcat.manager.url}” username=”${tomcat.username}”  
  15. password=”${tomcat.password}” path=”${context.path}”  
  16. war=”${war.file}” update=”true” />  
  17. </target>  
  18. <target name=”undeploy” description=”Undeploy web application”>  
  19. <undeploy url=”${tomcat.manager.url}” username=”${tomcat.username}”  
  20. password=”${tomcat.password}” path=”${context.path}”/>  
  21. </target>  
  22. <target name=”start” description=”Start web application”>  
  23. <start url=”${tomcat.manager.url}” username=”${tomcat.username}”  
  24. password=”${tomcat.password}” path=”${context.path}”/>  
  25. </target>  
  26. <target name=”reload” description=”Reload web application”>  
  27. <reload url=”${tomcat.manager.url}” username=”${tomcat.username}”  
  28. password=”${tomcat.password}” path=”${context.path}”/>  
  29. </target>  
  30. <target name=”stop” description=”Stop web application”>  
  31. <stop url=”${tomcat.manager.url}” username=”${tomcat.username}”  
  32. password=”${tomcat.password}” path=”${context.path}”/>  
  33. </target>  
  34. <target name=”list” description=”List all web applications on server”>  
  35. <list url=”${tomcat.manager.url}” username=”${tomcat.username}”  
  36. password=”${tomcat.password}”/>  
  37. </target>  
 
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
cruisecontrol、ant、svn持续集成
无所不能的“蚂蚁”--Ant(一)(二)(三)(四)
Apache Ant使用进阶
带有Selenium的Apache ANT:完整教程
ant 中 if else - 学习笔记
Ant学习指南
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服