Basics of Apache Ant

来源:互联网 发布:上瘾网络剧新闻发布会 编辑:程序博客网 时间:2024/06/02 16:26

what is Ant? (Ant是什么)

Apache Ant is a Java-based build tool whose mission is to drive processes described in build files as targets and extension points dependentupon each other.(Ant uses XML to describe the build process and its dependencies. By default the XML file is named build.xml.

note:Ant是基于java的构建工具,运行靠构建脚本,默认的名称为build.xml,可直接运行ant命令。如果是其他名称,如:mybuild.xml,命令行则应为:ant -f mybuild.xml。


The main known usage of Ant is the build of Java applications. Ant supplies anumber of built-in tasks allowing to compile, assemble, test and run Javaapplications. Ant can also be used effectively to build non Java applications,for instance C or C++ applications. More generally, Ant can be used to pilotany type of process which can be described in terms of targets and tasks.

noteAnt内置task,允许编译,汇编,测试和运行的Java应用程序。Ant也可用来有效地建立非Java应用程序,如C或C + +的应用程序。



Ant is written in Java. Users ofAnt can develop their own "antlibs" containing Ant tasks and types,and are offered a large number of ready-made commercial or open-source"antlibs".

note:Ant基于Java。用户可以开发自己的包含task和type的 antlibs。同时也提供了大量现成的商用或是开源的antlibs。


Ant is extremely flexible and doesnot impose coding conventions or directory layouts to the Java projects which adopt it as a build tool.


Ant is an Apache project. It is open source software, and isreleased under the Apache Software License. 


Installing Apache Ant

To get up and running with Ant quickly, follow these steps:
  1. Make sure you have a Java environment installed.( both JDK and Jre). Make sure you have setenvironmental variables JAVA_HOME to your Java environment. (确认机器安装了Java环境,设置了Java环境变量。检查JAVA_HOME、path是否正确)

    note: Ant 1.8.* works with jdk1.4 and higher,
            Ant 1.7.* works with jdk1.3 and higher,
    Ant 1.6.* works with jdk1.2 and higher,
    Ant 1.2 to Ant 1.5.* work with jdk1.1 and higher.

  2. Download Ant and umcompress the downloaded file into a directory. (下载Ant并解压,链接:http://ant.apache.org/)
  3. Set enviromental variable ANT_HOME to the directory you uncompressed Ant to. (设置ANT_HOME环境变量,指向ant的解压根目录)
  4. Add  ${ANT_HOME}/bin (Unix系统)   or%ANT_HOME%/bin (Windows系统) to your path. (在path环境变量中,添加${ANT_HOME}/bin (Unix系统)   或者 %ANT_HOME%/bin (Windows系统))

  5. Check installation.
    You can check the basic installation with opening a new shell and typing  ant. You shoud get a message like this:



    so Ant works. This message is there because you need to write an individual builfile for your project. 
    (如果能够运行ant(忽略这些异常:Buildfile:build.xml does not exist!),而不是出现诸如命令无法解释的错误,那么你的ant就安装成功了)

    With a ant - version you should get an output like:(命令窗口输入 ant –version 你可以获得当前的ant版本)



    If this does not work ensure your environment variables are set right.


Sample build.xml file

 

Ant uses XML to describe the build process and its dependencies.Below is listed a sample build.xml file. It defines a target-test, which hasan associated description.

 

<?xml version="1.0"encoding="GBK"?><project name="test" default="copyfile" basedir="."><target name="copyfile"><copy file="d:/a.txt" todir="c:/temp" overwrite="true"/></target></project>

Each buildfile contains one project and at least one (default) target. Targets contain task elements. Each task element of the buildfile can have an id attribute and can later be referred to by the value supplied to this. The value has to be unique.

note:如上述例子中的copy,是比较常用的命令。把上述内容放到build.xml文件中,运行ant命令,Ant将执行拷贝文件的任务。


从上述的简单构建脚本中,我们可以了解脚本的基本编写方法:

  1. buildfile的根元素:<project>。 <project>标签有三个属性:name、default、basedir
  2. 一个buildfile中至少有一个target。所有的任务,必须通过<target>标签包围。一个<target>标签,可以包含多个任务。
  3. <copy>标签描述了一个任务(task),这些任务必须放到一个<target>内部。
  4. <project>中可以包含多个<target>,如果不指定执行哪个任务,则将使用<project>标签中指定的default属性的target。







原创粉丝点击