打开APP
未登录
开通VIP,畅享免费电子书等14项超值服
开通VIP
首页
好书
留言交流
下载APP
联系客服
Apache Ant 简介
馆天下ccf
>《ANT》
2015.07.05
关注
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>元素:
[xhtml]
view plain
copy
print
?
<!--
A simple build script that creates a directory (dir.name), and copies
a file (file.name) to it.
-->
<project name=”MyFirstAntProject” basedir=”.” default=”copyfile”>
<project>中的name属性标识工程名,basedir指示根目录,default标识默认执行的target。如果运行ant时不指定这些属性,Ant将执行这个target。
下一步,定义这个工程所使用的properties:
[xhtml]
view plain
copy
print
?
<property name=”dir.name” value=”${basedir}/mydir”/>
<property name=”file.name” value=”file1.txt”/>
这里定义了两个全局属性,分别是dir.name和file.name。这些属性是可选的,但使用属性会更方便,尤其是便于维护。一种更有效的方式是将这些属性放到一个专门文件里,从而使这个xml文件更加灵活,易于重用。
因为这个build文件很简单,所以没有task和path的定义。
最后,定义所要执行的targets。
[xhtml]
view plain
copy
print
?
<target name=”makedirectory” description=”Create directory mydir”>
<mkdir dir=”${dir.name}”/>
</target>
<target name=”copyfile” depends=”makedirectory” description=”Copy files”>
<copy file=”${file.name}” todir=”${dir.name}”/>
</target>
<target name=”clean” description=”Clean up task”>
<delete dir=”${dir.name}”/>
</target
</project>
注意copyfile中的depends,denpends属性意味着在执行copyfile之前,makedirectorytarget一定要先执行。
Ant的语法结构一般是:
[c-sharp]
view plain
copy
print
?
ant -buildfile <filename> <target-name>
如果没有使用-fuildfile参数,那么Ant将默认使用build.xml,如果没有build.xml,那么Ant将报错,如下:
[c-sharp]
view plain
copy
print
?
$ ant
Buildfile: build.xml does not exist!
Build failed
因为我们的build文件名不是build.xml,因此需要添加-buildfile这个参数。下面是使用-buildfile参数的结果:
[c-sharp]
view plain
copy
print
?
$ ant -buildfile FirstBuild.xml
Buildfile: FirstBuild.xml
makedirectory:
[mkdir] Created dir: /home/tomcat/AppendixB/mydir
copyfile:
[copy] Copying 1 file to /home/tomcat/AppendixB/mydir
BUILD SUCCESSFUL
Total time: 1 second
在文件中定义的property可以被Ant的参数所覆盖,如下面使用yourdir取代mydir:
[c-sharp]
view plain
copy
print
?
$ ant -buildfile FirstBuild.xml -Ddir.name=yourdir
Buildfile: FirstBuild.xml
makedirectory:
[mkdir] Created dir: /home/tomcat/AppendixB/yourdir
copyfile:
[copy] Copying 1 file to /home/tomcat/AppendixB/yourdir
BUILD SUCCESSFUL
Total time: 1 second
一般在build.xml中都会有clean这个target,其作用有些像卸载软件程序:
[c-sharp]
view plain
copy
print
?
$ ant -buildfile FirstBuild.xml clean
Buildfile: FirstBuild.xml
clean:
[delete] Deleting directory /home/tomcat/AppendixB/mydir
BUILD SUCCESSFUL
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的第一部分:
[xhtml]
view plain
copy
print
?
<!-- Ant build file for a sample web application -->
<project name=”SampleWebApplication” default=”compile” basedir=”.”>
这一部分没什么好说的,前面都说了。
下一部分还是全局可用的properties。
[xhtml]
view plain
copy
print
?
<property name=”tomcat.home” value=”/usr/tomcat/apache-tomcat-6 “/>
<property name=”app.name” value=”sampleWebapp”/>
<property name=”context.path” value=”/${app.name}”/>
<property name=”src.home” value=”${basedir}/src”/>
<property name=”web.home” value=”${basedir}/web”/>
<property name=”conf.home” value=”${basedir}/conf”/>
<property name=”lib.home” value=”${basedir}/lib”/>
<property name=”docs.home” value=”${basedir}/docs”/>
<property name=”build.home” value=”${basedir}/build”/>
<property name=”dist.home” value=”${basedir}/dist”/>
<property name=”war.file” value=”${dist.home}/${app.name}.war”/>
<!-- Configure properties to access the Tomcat Manager application -->
<property name=”tomcat.manager.url” value=”http://localhost:8080/manager”/>
<property name=”tomcat.username” value=”tomcat”/>
<property name=”tomcat.password” value=”tomcat”/>
clean target,用于删除build和dist目录及其所有子目录。
[xhtml]
view plain
copy
print
?
<!-- ====== Clean Target ====== -->
<target name=”clean”
description=”Cleanup- deletes everything generated by the ant script”>
<delete dir=”${build.home}”/>
<delete dir=”${dist.home}”/>
<delete dir=”${docs.home}”/>
</target>
init target用于完成初始化工作,包括创建目录结构,初始化Java CLASSPATH。CLASSPATH中包含这个web应用程序使用的所有库文件。下面的代码将servlet-api.jar包进来。
[xhtml]
view plain
copy
print
?
<!-- ====== All initializations: Classpath, directory structure ====== -->
<target name=”init”>
<mkdir dir=”${build.home}”/>
<mkdir dir=”${docs.home}”/>
<mkdir dir=”${dist.home}”/>
<!-- Classpath for compiling web application, generating javadocs -->
<path id=”classpath”>
<fileset dir=”${lib.home}”>
<include name=”*.jar”/>
</fileset>
<fileset dir=”${tomcat.home}/lib”>
<include name=”servlet-api.jar”/>
</fileset>
</path>
<property name=”classpath” refid=”classpath”/>
</target>
compile target将编译src目录中所有的java文件,生成的class文件将被存放到destdir属性所指的目录中,compile必需是在init完成之后才能执行。如果使用compile命令,则将自动先执行init。
[xhtml]
view plain
copy
print
?
<!-- ====== Compilation ====== -->
<target name=”compile” depends=”init”>
<echo message=”Classpath set to ${classpath}”/>
<javac srcdir=”${src.home}”
destdir=”${build.home}”
debug=”true”
classpath=”${classpath}”
deprecation=”true”>
</javac>
<!-- Copy all property files -->
<copy todir=”${build.home}”>
<fileset dir=”${conf.home}”/>
</copy>
</target>
dist target将创建一个war文件。
[xhtml]
view plain
copy
print
?
<!-- ====== Create a distributable WAR file ====== -->
<target name=”dist” depends=”compile”
description=”Creates the deployable WAR file”>
<war destfile=”${war.file}”
webxml=”${web.home}/WEB-INF/web.xml”>
<fileset dir=”${web.home}” excludes=”**/web.xml” />
<lib dir=”${lib.home}”/>
<classes dir=”${build.home}”/>
</war>
</target>
javadoc target将为源码创建javadoc文档。
[xhtml]
view plain
copy
print
?
<!-- ====== Create documentation (javadocs) ====== -->
<target name=”javadoc” depends=”init” description=”Creates the Javadocs for the
project”>
<javadoc sourcepath=”${src.home}”
packagenames=”com.foobar.*”
classpath=”${classpath}”
destdir=”${docs.home}”
windowtitle=”Javadoc for the Sample Web Application (TM)”>
</javadoc>
all target将上面这些任务按在depends中的顺序组织起来。
[xhtml]
view plain
copy
print
?
<!-- ====== All Target ====== -->
<target name=”all”
depends=”clean, prepare, compile, dist”
description=”Builds the web application and war file”/>
使用Ant管理web应用程序
继续上面的那个build.xml文件,在其中添加下面的这些代码,就可以通过Ant管理这个web应用程序了。
[xhtml]
view plain
copy
print
?
<!-- Configure properties to access the Tomcat Manager application -->
<property name=”tomcat.manager.url” value=”http://localhost:8080/manager”/>
<property name=”tomcat.username” value=”tomcat”/>
<property name=”tomcat.password” value=”tomcat”/>
<!-- Classpath for Tomcat ant tasks -->
<path id=”deployer.classpath”>
<fileset dir=”${tomcat.home}/lib”>
<include name=”*.jar”/>
</fileset>
</path
...
<!-- ====== Manage web application ====== -->
<target name=”deploy” depends=”dist” description=”Deploy web application”>
<deploy url=”${tomcat.manager.url}” username=”${tomcat.username}”
password=”${tomcat.password}” path=”${context.path}”
war=”${war.file}” update=”true” />
</target>
<target name=”undeploy” description=”Undeploy web application”>
<undeploy url=”${tomcat.manager.url}” username=”${tomcat.username}”
password=”${tomcat.password}” path=”${context.path}”/>
</target>
<target name=”start” description=”Start web application”>
<start url=”${tomcat.manager.url}” username=”${tomcat.username}”
password=”${tomcat.password}” path=”${context.path}”/>
</target>
<target name=”reload” description=”Reload web application”>
<reload url=”${tomcat.manager.url}” username=”${tomcat.username}”
password=”${tomcat.password}” path=”${context.path}”/>
</target>
<target name=”stop” description=”Stop web application”>
<stop url=”${tomcat.manager.url}” username=”${tomcat.username}”
password=”${tomcat.password}” path=”${context.path}”/>
</target>
<target name=”list” description=”List all web applications on server”>
<list url=”${tomcat.manager.url}” username=”${tomcat.username}”
password=”${tomcat.password}”/>
</target>
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报
。
打开APP,阅读全文并永久保存
查看更多类似文章
猜你喜欢
类似文章
【热】
打开小程序,算一算2024你的财运
cruisecontrol、ant、svn持续集成
无所不能的“蚂蚁”--Ant(一)(二)(三)(四)
Apache Ant使用进阶
带有Selenium的Apache ANT:完整教程
ant 中 if else - 学习笔记
Ant学习指南
更多类似文章 >>
生活服务
热点新闻
留言交流
回顶部
联系我们
分享
收藏
点击这里,查看已保存的文章
导长图
关注
一键复制
下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!
联系客服
微信登录中...
请勿关闭此页面
先别划走!
送你5元优惠券,购买VIP限时立减!
5
元
优惠券
优惠券还有
10:00
过期
马上使用
×