ant教程

  1. 写一个buildfile文件

示例


 
    <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节点:定义项目的基本信息 主要属性 name,basedir,default

 

  • target 节点:定义要执行的一些操作,比如 编译(compile),运行(run)等 , depends表示依赖的target。

Parameters

AttributeDescriptionRequired
namethe name of the property to set.No
valuethe value of the property. One of these or nested text, when using the name attribute
locationSets the property to the absolute filename of the given file. If the value of this attribute is an absolute path, it is left unchanged (with / and \ characters converted to the current platforms conventions). Otherwise it is taken as a path relative to the project's basedir and expanded.
refidReference to an object defined elsewhere. Only yields reasonable results for references to PATH like structures or properties.
resourcethe name of the classpath resource containing properties settings in properties file format. One of these, whennot using the name attribute
filethe location of the properties file to load.
urla url containing properties-format settings.
environmentthe prefix to use when retrieving environment variables. Thus if you specify environment="myenv" you will be able to access OS-specific environment variables via property names "myenv.PATH" or "myenv.TERM". Note that if you supply a property name with a final "." it will not be doubled; i.e. environment="myenv." will still allow access of environment variables through "myenv.PATH" and "myenv.TERM". This functionality is currently only implemented on select platforms. Feel free to send patches to increase the number of platforms on which this functionality is supported ;).Note also that properties are case-sensitive, even if the environment variables on your operating system are not; e.g. Windows 2000's system path variable is set to an Ant property named "env.Path" rather than "env.PATH".
classpaththe classpath to use when looking up a resource.No
classpathrefthe classpath to use when looking up a resource, given as reference to a <path> defined elsewhere..No
prefixPrefix to apply to properties loaded using fileresource, or url. A "." is appended to the prefix if not specified.No
prefixValuesWhether to apply the prefix when expanding the right hand side of properties loaded using fileresource, or urlSince Ant 1.8.2No (default=false)
relativeIf set to true the relative path to basedir is set. Since Ant 1.8.0No (default=false)
basedirThe basedir to calculate the relative path from. Since Ant 1.8.0No (default=${basedir})

  • 内置属性 (built-in properties)

Ant provides access to all system properties as if they had been defined using a <property> task. For example, ${os.name}expands to the name of the oper atingsystem.

For a list of system properties see the Javadoc of System.getProperties.

In addition, Ant has some built-in properties:

basedir             the absolute path of the project's basedir (as set
                    with the basedir attribute of <project>).
ant.file            the absolute path of the buildfile.
ant.version         the version of Ant
ant.project.name    the name of the project that is currently executing;
                    it is set in the name attribute of <project>.
ant.project.default-target
                    the name of the currently executing project's
                    default target;  it is set via the default
                    attribute of <project>.
ant.project.invoked-targets
                    a comma separated list of the targets that have
                    been specified on the command line (the IDE,
                    an <ant> task ...) when invoking the current
                    project.
ant.java.version    the JVM version Ant detected; currently it can hold
                    the values "1.2", "1.3",
                    "1.4",  "1.5" and "1.6".
ant.core.lib        the absolute path of the ant.jar file.

There is also another property, but this is set by the launcher script and therefore maybe not set inside IDEs:

ant.home            home directory of Ant

The following property is only set if Ant is started via the Launcher class (which means it may not be set inside IDEs either):

ant.library.dir     the directory that has been used to load Ant's
                    jars from.  In most cases this is ANT_HOME/lib.



  • ant任务 (ant tasks)

    Ant的常用任务

在Ant工具中每一个任务封装了具体要执行的功能,是Ant工具的基本执行单位。在本小节中,主要引导读者来看下Ant的常用任务及其使用举例。

1.       copy任务

该任务主要用来对文件和目录的复制功能。举例如下:

Eg1.复制单个文件:<copy file="file.txt" tofile="copy.txt"/>

Eg2.对文件目录进行复制:

   <copy todir="../newdir/dest_dir">

            <fileset dir="src_dir"/>

 </copy>

Eg3. 将文件复制到另外的目录:

 <copy file="file.txt" todir="../other/dir"/>

2.       delete任务

对文件或目录进行删除,举例如下:

Eg1. 删除某个文件:<delete file="photo/amigo.jpg"/>

Eg2. 删除某个目录:<delete dir="photo"/>

Eg3. 删除所有的备份目录或空目录:

        <delete includeEmptyDirs="true">

               <fileset dir="." includes="**/*.bak"/>

        </delete>

3.       mkdir任务

创建目录。eg:<mkdir dir="build"/>

4.       move任务

移动文件或目录,举例如下:

Eg1. 移动单个文件:<move file="fromfile" tofile=”tofile”/>

Eg2. 移动单个文件到另一个目录:<move file="fromfile" todir=”movedir”/>

Eg3. 移动某个目录到另一个目录:

        <move todir="newdir">

               <fileset dir="olddir"/>

        </move>

5.       echo任务

该任务的作用是根据日志或监控器的级别输出信息。它包括message、file、append和level四个属性,举例如下:

<echo message="Hello,Amigo" file="logs/system.log" append="true">

 

Ant的数据类型  
在构建文件中为了标识文件或文件组,经常需要使用数据类型。数据类型包含在 
org.apache.tool.ant.types包中。下面简单介绍构建文件中一些常用的数据类型。 

1. argument 类型  
由Ant构建文件调用的程序,可以通过<arg>元素向其传递命令行参数,如apply,exec和java任务均可接受嵌套<arg>元素,可以为各自的过程调用指定参数。以下是<arg>的所有属性。 
(1).values 是一个命令参数。如果参数种有空格,但又想将它作为单独一个值,则使用此属性。 
(2).file表示一个参数的文件名。在构建文件中,此文件名相对于当前的工作目录。 
(3).line表示用空格分隔的多个参数列表。 
(4).path表示路径。 

2.ervironment 类型  
由Ant构建文件调用的外部命令或程序,<env>元素制定了哪些环境变量要传递给正在执行的系统命令,<env>元素可以接受以下属性。 
(1).file表示环境变量值得文件名。此文件名要被转换位一个绝对路径。 
(2).path表示环境变量的路径。Ant会将它转换为一个本地约定。 
(3).value 表示环境变量的一个直接变量。 
(4).key 表示环境变量名。 
注意  file path 或 value只能取一个。 

3.filelist类型 Filelist 是一个支持命名的文件列表的数据类型,包含在一个filelist类型中的文件不一定是存在的文件。以下是其所有的属性。 
(1).dir是用于计算绝对文件名的目录。 
(2).files 是用逗号分隔的文件名列表。 
(3).refid 是对某处定义的一个<filelist>的引用。 
注意  dir 和 files 都是必要的,除非指定了refid(这种情况下,dir和files都不允许使用)。 

4.fileset类型  
Fileset 数据类型定义了一组文件,并通常表示为<fileset>元素。不过,许多ant任务构建成了隐式的fileset,这说明他们支持所有的fileset属性和嵌套元素。以下为fileset 的属性列表。 
(1).dir表示fileset 的基目录。 
(2).casesensitive的值如果为false,那么匹配文件名时,fileset不是区分大小写的,其默认值为true。 
(3).defaultexcludes 用来确定是否使用默认的排除模式,默认为true。 
(4).excludes 是用逗号分隔的需要派出的文件模式列表。 
(5).excludesfile 表示每行包含一个排除模式的文件的文件名。 
(6).includes 是用逗号分隔的,需要包含的文件模式列表。 
(7).includesfile 表示每行包括一个包含模式的文件名。 

5.patternset 类型  
Fileset 是对文件的分组,而patternset是对模式的分组,他们是紧密相关的概念。<patternset>支持4个属性:includes excludex includexfile 和 excludesfile,与fileset相同。Patternset 还允许以下嵌套元素:include,exclude,includefile 和 excludesfile。 

6.filterset 类型  
Filterset定义了一组过滤器,这些过滤器将在文件移动或复制时完成文件的文本替换。 
主要属性如下: 
(1).begintoken 表示嵌套过滤器所搜索的记号,这是标识其开始的字符串。 
(2).endtoken表示嵌套过滤器所搜索的记号这是标识其结束的字符串。 
(3).id是过滤器的唯一标志符。 
(4).refid是对构建文件中某处定义一个过滤器的引用。 

7.Path类型  
Path元素用来表示一个类路径,不过它还可以用于表示其他的路径。在用作揖个属性时,路经中的各项用分号或冒号隔开。在构建的时候,此分隔符将代替当前平台中所有的路径分隔符,其拥有的属性如下。 
(1).location 表示一个文件或目录。Ant在内部将此扩展为一个绝对路径。 
(2).refid 是对当前构建文件中某处定义的一个path的引用。 
(3).path表示一个文件或路径名列表。 

8.mapper类型  
Mapper类型定义了一组输入文件和一组输出文件间的关系,其属性如下。 
(1).classname 表示实现mapper类的类名。当内置mapper不满足要求时,用于创建定制mapper。 
(2).classpath表示查找一个定制mapper时所用的类型路径。 
(3).classpathref是对某处定义的一个类路径的引用。 
(4).from属性的含义取决于所用的mapper。 
(5).to属性的含义取决于所用的mapper。 
(6).type属性的取值为identity,flatten glob merge  regexp  其中之一,它定义了要是用的内置mapper的类型。 


类型举例

<fileset dir="${server.src}" casesensitive="yes">
  <include name="**/*.java"/>
  <exclude name="**/*Test*"/>
</fileset>
 
 
 
<fileset dir="${server.src}" casesensitive="yes">
  <patternset id="non.test.sources">
    <include name="**/*.java"/>
    <exclude name="**/*Test*"/>
  </patternset>
</fileset>

Groups the same files as the above example, but also establishes a PatternSet that can be referenced in other <fileset>elements, rooted at a different directory.

<fileset dir="${client.src}" >
  <patternset refid="non.test.sources"/>
</fileset>
<fileset dir="src" includes="main/" />


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值