Apache Ant入门

来源:互联网 发布:sql服务器拒绝访问 编辑:程序博客网 时间:2024/05/16 13:53

apache ant的安装和介绍在官方的用户手册上说明很详细,下面开始从Ant的一些概念开始

基本概念

project

Ant的构建文件(buildfiles)是XML格式的,每个构建文件都包含一个project标签,代表这个构建文件所要构建的项目。project标签有三个属性,分别是name、default、basedir,这三个属性的作用如下表所示:

属性描述是否必须namethe name of the project.Nodefaultthe 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.basedirthe 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

一个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

一个task是一个可以运行的代码片段。一个target可以包含多个task。

每个task可以有多个属性(或者值),其中属性的值可以直接指定,也可以是一个对property的引用,这个引用将在task执行之前解析。

task的结构如下:

<name attribute1="value1" attribute2="value2" ... />

name就是这个task的名,attribute1,attribute2是名为name的task的属性,属性可以有多个,如名为mkdir的task作用是创建一个目录,它的用法如下:

<mkdir dir="${dist}"/>

这个task的是创建一个目录,dir是其属性,用于指定要创建的目录的路径,目录的路径由一个属性值${dist}指定,这个属性dist的值是在property中指定的。

Apache Ant内置了大量的task,具体可以参考http://ant.apache.org/manual/tasklist.html

property

property用于在project中通过name或value属性设置属性或者通过资源文件设置属性,其中property是大小写敏感的。如

<property name="foo.dist" value="dist"/>

这个property的作用是设置属性foo.dist的值为dist,除了通过name和value指定属性值,property还支持指定文件和资源来设置属性,如

<property file="foo.properties"/>

就表示从名为foo.properties的文件中读取属性和属性值,还可以通过url读取网络上的一个资源,如:

<property url="http://www.mysite.com/bla/props/foo.properties"/>

表示从web上读取url为http://www.mysite.com/bla/props/foo.properties的属性。关于property的其他属性可以参考http://ant.apache.org/manual/Tasks/property.html

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");}}

这个Java文件所在的项目名为AntStart,build.xml文件放在AntStart项目的根目录下,其内容为:

<?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>

在AntStart项目的根目录下执行ant命令就会创建build目录和dist目录,在dist目录中有个lib目录,这个目录中存有构建的jar包,注意最后一个名为clean的target并未执行,因为project的默认target是dist,如果将project的默认target改为clean,则最后会删除build目录和dist目录。

Reference

http://ant.apache.org/manual/index.html


EOF

0 0
原创粉丝点击