Ant学习笔记

Ant学习笔记

Ant 是一个 Apache 基金会下的跨平台的基于 Java 语言开发的构件工具,上一篇博客记录了通过使用javac编译java项目,但是直接使用java来构建项目是比较复杂的,不易于构建大型的项目,构建c/c++有make,构架java项目则有ant和maven。

Ant配置文件

一般来说ant会默认读取当前目录下面的build.xml,但是也可以指定对应的配置文件。
使用默认的配置

ant

指定配置文件

ant -f myconfig.xml

ant的构建文件是使用xml。每一个构建文件都包含有一个project元素和至少一个target元素,通过property元素设置一些变量,供后面调用。一个基本的build.xml如下:

<project  name="MyProject"  default="dist"  basedir=".">
	<description>
       simple example 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 MyProject-${DSTAMP}.jar file -->
        <jar  jarfile="${dist}/lib/MyProject-${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>

project

project元素有三个属性,如下:

Attribute Description Required
name 项目的名词 No
default 默认的target No
basedir 表示当该属性没有指定时,使用 Ant 的构件文件的附目录作为基准目录 No

target

执行的任务,比如执行清除生成项目工程,编译项目,部署项目。

Attribute Description Required
name the name of the target. Yes
depends a comma-separated list of names of targets on which this target depends. No
if the name of the property that must be set in order for this target to execute, or something evaluating to true. No
unless the name of the property that must not be set in order for this target to execute, or something evaluating to false. No
description a short description of this target's function. No
extensionOf Adds the current target to the depends list of the named extension-pointsince Ant 1.8.0. No
onMissingExtensionPoint What to do if this target tries to extend a missing extension-point. ("fail", "warn", "ignore"). since Ant 1.8.2. No. Not allowed unless extensionOfis present. Defaults to fail.

property

Attribute Description Required
name 属性名字
value 属性值
file 属性文件的名字

Task

执行的具体任务,内建的task有EchoJavacMkdir等。

参考

Using Apache Ant Writing a Simple Buildfile

原文地址:https://www.cnblogs.com/ZiYangZhou/p/8542707.html