apache ant的安装和介绍在官方的用户手册上说明很详细,下面开始从Ant的一些概念开始
Ant的构建文件(buildfiles)是XML格式的,每个构建文件都包含一个project标签,代表这个构建文件所要构建的项目。project标签有三个属性,分别是name、default、basedir,这三个属性的作用如下表所示:
属性 | 描述 | 是否必须 |
name | the name of the project. | No |
default | the default target to use when no target is supplied. | No; however, since Ant 1.6.0, every project includes an implicit target that contains any and all top-level tasks and/or types. This target will always be executed as part of the project‘s initialization, even when Ant is run with the -projecthelp option. |
basedir | the base directory from which all path calculations are done. This attribute might be overridden by setting the "basedir" property beforehand. When this is done, it must be omitted in the project tag. If neither the attribute nor the property
have been set, the parent directory of the buildfile will be used. A relative path is resolved relative to the directory containing the build file. |
No |
此外,可以通过<description>标签为project加入描述,这个description是projet的子元素,例如:
<project name="MyProject" default="dist" basedir="."> <description> simple example build file </description> </project>
一个target表示将要执行任务的集合。在project中至少包含一个target,每个target包含一个或多个任务。当启动Ant时,可以指定一个默认的target,如果没有指定则默认执行project标签中default属性所指定的target。
一个project中可能会有多个target,一个target可以依赖于另一个target,例如在一个project中定一个了一个名为compiling的target和一个名为distributable的target,但是只有compiling运行完了才能执行distributable,那么distributable就是依赖于compiling的,Ant可以处理这样的依赖。但是Ant的依赖只是处理target运行的顺序,它们之间的执行与否并不影响,例如,如果compiling执行失败,distributable还是会执行只是会执行失败。
一个task是一个可以运行的代码片段。一个target可以包含多个task。
每个task可以有多个属性(或者值),其中属性的值可以直接指定,也可以是一个对property的引用,这个引用将在task执行之前解析。
task的结构如下:
<name attribute1="value1" attribute2="value2" ... />
<mkdir dir="${dist}"/>
Apache Ant内置了大量的task,具体可以参考http://ant.apache.org/manual/tasklist.html
property用于在project中通过name或value属性设置属性或者通过资源文件设置属性,其中property是大小写敏感的。如
<property name="foo.dist" value="dist"/>
<property file="foo.properties"/>
<property url="http://www.mysite.com/bla/props/foo.properties"/>
property本质上还是一个task。
学习了上面关于Apache Ant的基本内容,现在就来写一个最基本的build.xml文件,这个build.xml文件的作用就是编译一个Java项目,然后将这个项目制作为jar包,首先写一个Java类文件
package com.anttest; public class AntTest { public static void main(String[] args) { System.out.println("Hello Ant"); } }
<?xml version="1.0" encoding="UTF-8"?> <project name="AntStart" default="dist" basedir="."> <description> build文件的例子 </description> <!-- 为这个build文件设置全局属性 --> <property name="src" location="src"/> <property name="build" location="build"/> <property name="dist" location="dist"/> <target name="init"> <!-- 创建一个时间戳 --> <tstamp/> <!-- 创建文件夹用于存放编译和打包文件 --> <mkdir dir="${build}"/> </target> <target name="compile" depends="init" description="compile the source " > <!-- 从 ${src} 编译Java源代码到 ${build} --> <javac srcdir="${src}" destdir="${build}"/> </target> <target name="dist" depends="compile" description="generate the distribution" > <!-- 创建存放jar包的目录 --> <mkdir dir="${dist}/lib"/> <!-- 创建jar文件 --> <jar jarfile="${dist}/lib/AntStart-${DSTAMP}.jar" basedir="${build}"/> </target> <target name="clean" description="clean up" > <!-- 删除目录 --> <delete dir="${build}"/> <delete dir="${dist}"/> </target> </project>
http://ant.apache.org/manual/index.html
EOF
原文:http://blog.csdn.net/workformywork/article/details/19194229