Ant简介 java
Ant是基于java的构建工具。理论上来讲它相似与make工具,可是却克服了make的一些固有的缺陷. linux
传统的Make是基于操做系统shell的构建工具,虽然也能够基于工做的os对make工具进行扩展,但却难以实现跨平台构建.Ant基于java扩展功能,而且经过在xml中的target中定义的task进行构建任务的定义.其中每个任务都是实现特定任务接口的类.同时Ant也提供了exec任务容许调用不一样的操做系统的shell命令.shell
Ant主要元素介绍windows
Ant使用xml文件定义构建过程,每个构建文件都会包括一个project和至少一个target.Target会包含task元素。每个task元素能够经过id属性被引用.eclipse
1.Projectside
Project元素有三个属性:name、default、basedir.同时其能够包含一个description元素对项目进行描述.工具
name:project的名称,不是必须属性;学习
default:执行构建的时候不指定要执行的target,则默认执行此设置的target;ui
basedir:设置基准目录,则后续全部的相对位置则以此设置为基准路径;若是不设置则以包含构建文件的目录为基准目录;this
<?xml version="1.0" encoding="UTF-8"?> <project name="ProjectExample" default="default" basedir="."> <description> 学习project的用法 </description> <target name="default" > <echo>${basedir}</echo> </target> <target name="echoBasedir"> <echo>${basedir}</echo> </target> </project>
2.Targets
每一个Project会定义一个或者多个target.Target是待执行的task的集合.当启动执行ant的时候能够指定要执行的target,若是没有指定则执行project设置的默认的target/一个target能够经过depends来定义其依赖的其余的target,好比打包的target依赖编译的target.
<?xml version="1.0" encoding="UTF-8"?> <!-- ====================================================================== 2015年6月1日 下午9:35:09 targetExample description hou ====================================================================== --> <project name="targetExample" default="default"> <description> description </description> <!-- ================================= target: default ================================= --> <target name="default" depends="depends" description="description"> <echo>excuting default target</echo> </target> <!-- - - - - - - - - - - - - - - - - - target: depends - - - - - - - - - - - - - - - - - --> <target name="depends"> <echo>excuting depends target</echo> </target> </project>
3.Tasks
Task是一段能够被执行的代码.一个task能够有多个属性(attribute).这些属性(attribute)的值能够引用属性(property),被引用的属性会在任务执行以前被解析成具体的值.task的通用结构以下
<name attribute1=”value1” attribute2=”value2”.../>
Ant既内置了一些经常使用的task,同时也提供了便利的扩展机制.
<?xml version="1.0" encoding="UTF-8"?> <!-- ====================================================================== 2015年6月1日 下午9:59:23 taskExample description hou ====================================================================== --> <project name="taskExample" default="default"> <description> description </description> <property name="dirName" value="taskdir "></property> <!-- ================================= target: default ================================= --> <target name="default" description="description"> <echo>list current directory </echo> <exec executable="ls"></exec> <echo>make [${dirName}] directory </echo> <mkdir dir="${dirName}"/> <echo>list current directory </echo> <exec executable="ls"></exec> <delete dir="${dirName}"></delete> </target> </project>
4.Properties
属性是经过设置一个能够在构建过程当中重复使用的字符串来达到自定义构建过程的重要方式.一般状况下是在构建文件中直接定义属性,固然也能够在外部定义属性.属性的名字是大小写敏感的.属性的引用是经过${属性名}来实现的.Ant中的属性相似java里的字段,可是属性一经设置就不可更改.
Ant 中有许多预约义的属性.首先, Java 环境设置用于运行 Ant 的全部系统属性, 都可做为 Ant 属性使用,好比 ${user.home} .除了这些属性以外,Ant 还定义了它本身的一小组属性,包括${ant.version},这个属性包含 Ant 的版本;以及 ${basedir},这个属性是项目目录的绝对路径(由包含生成文件的目录所定义,或者由 project 元素的可选 basedir 属性所定义). 属性常常用于引用文件系统上的文件或目录,可是对于使用不一样路径分隔符(例如,/ 与 \)的平台来讲,这样可能在跨越不一样平台时致使问题.Ant 的 location 属性专门设计用于以平台无关的方式包含文件系统路径.
<project name="propertiesExample" default="dist" basedir="../"> <description> simple propertiesExample build file </description> <!-- set global properties for this build --> <property name="src" location="src"/> <property name="build" location="build"/> <property name="dist" location="dist"/> <target name="init"> <!-- Create the time stamp --> <tstamp/> <!-- Create the build directory structure used by compile --> <mkdir dir="${build}"/> </target> <target name="compile" depends="init" description="compile the source " > <!-- Compile the java code from ${src} into ${build} --> <javac srcdir="${src}" destdir="${build}"/> </target> <target name="dist" depends="compile" description="generate the distribution" > <!-- Create the distribution directory --> <mkdir dir="${dist}/lib"/> <!-- Put everything in ${build} into the wufengtinghai.alm-${DSTAMP}.jar file --> <jar jarfile="${dist}/lib/wufengtinghai.alm-${DSTAMP}.jar" basedir="${build}"/> </target> <target name="clean" description="clean up" > <!-- Delete the ${build} and ${dist} directory trees --> <delete dir="${build}"/> <delete dir="${dist}"/> </target> </project>
运行Ant
Ant做为命令行工做,天然能够经过unix/linux的shell和windows的cmd进行条用.同时能够经过java的IDE进行调用,好比eclipse.
从命令行直接调用ant,这样ant会从当前目录寻找默认的build.xml,并使用默认的target执行生成过程.同时也能够经过ant -f buildfilename targetname来指定构建文件和target名称.
Eclipse对ant提供了强大的支持.首先支持语法高亮/智能提示/大纲视图.
在eclipse中选中构建文件,右键选择”Run As”-”Ant Build”,并执行便可.