Apache Ant 简介
来源:互联网 发布:文化部网络歌曲黑名单 编辑:程序博客网 时间:2024/06/05 23:12
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>元素:
- <!--
- 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:
- <property name=”dir.name” value=”${basedir}/mydir”/>
- <property name=”file.name” value=”file1.txt”/>
这里定义了两个全局属性,分别是dir.name和file.name。这些属性是可选的,但使用属性会更方便,尤其是便于维护。一种更有效的方式是将这些属性放到一个专门文件里,从而使这个xml文件更加灵活,易于重用。
因为这个build文件很简单,所以没有task和path的定义。
最后,定义所要执行的targets。
- <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的语法结构一般是:
- ant -buildfile <filename> <target-name>
如果没有使用-fuildfile参数,那么Ant将默认使用build.xml,如果没有build.xml,那么Ant将报错,如下:
- $ ant
- Buildfile: build.xml does not exist!
- Build failed
因为我们的build文件名不是build.xml,因此需要添加-buildfile这个参数。下面是使用-buildfile参数的结果:
- $ 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:
- $ 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,其作用有些像卸载软件程序:
- $ ant -buildfile FirstBuild.xml clean
- Buildfile: FirstBuild.xml
- clean:
- [delete] Deleting directory /home/tomcat/AppendixB/mydir
- BUILD SUCCESSFUL
- Total time: 0 seconds
使用Ant创建web应用程序
src放置java文件,web放jsp等页面文件和配置文件,dist用来存放生成的war文件,build用来放编译好的servlet,lib用来放库文,doc用来放生成的javadoc,以及最主要的,build.xml。其中,build,dist和doc是使用Ant脚本来生成的。
build.xml
build.xml的第一部分:
- <!-- Ant build file for a sample web application -->
- <project name=”SampleWebApplication” default=”compile” basedir=”.”>
这一部分没什么好说的,前面都说了。
下一部分还是全局可用的properties。
- <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 ====== -->
- <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包进来。
- <!-- ====== 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。
- <!-- ====== 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文件。
- <!-- ====== 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文档。
- <!-- ====== 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中的顺序组织起来。
- <!-- ====== All Target ====== -->
- <target name=”all”
- depends=”clean, prepare, compile, dist”
- description=”Builds the web application and war file”/>
使用Ant管理web应用程序
继续上面的那个build.xml文件,在其中添加下面的这些代码,就可以通过Ant管理这个web应用程序了。
- <!-- 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>
0 0
- Apache Ant 简介
- Apache Ant 简介
- Apache Ant简介
- Apache Ant
- Apache Ant
- ant apache
- Apache Ant
- ant.apache
- Apache Ant
- Apache-Ant
- apache--ANT
- Apache-Ant
- Apache-ant
- [ANT] -------Ant简介
- Ant 简介
- Ant 简介
- Ant简介
- Ant简介
- LRU Cache
- Android根据所设置的textSize获取字体高度和设置行高
- Python 获取新浪微博的最新公共微博
- 简单开发过程介绍(C语言)
- "error while loading shared libraries: xxx.so.x" 错误的原因和解决办法
- Apache Ant 简介
- httphelp
- picture
- centos 编译安装 heartbeat
- hdu2099
- Linux系统目录结构
- atitit.插件体系设计总结o73.doc
- nefu115
- java基础--->管理权限public protected private default