前言
Apache Ant 是一个软件自动化构建工具,构建过程包括编译、测试和部署等。它和 Make 工具相似,但由 Java 实现,所以要求 Java 运行环境,非常适合构建 Java 程序。
Ant 和 Make 明显不同之处在于 Ant 使用 XML 来表述构建过程与依赖关系,而 Make 使用 Makefile 格式文件。Ant 默认的构建文件名为 build.xml。每一个 build.xml 文件包含一个 和至少一个默认的 , 中包含许多 task elements。每个 task element 有一个作为引用的唯一 ID。
Ant 是 Apache 开源项目,以 Apache License 发行。
历史
Ant(意为 Another Neat Tool,另一个好用的工具。)最初的构想来自于 James Duncan Davidson,当时他正在开发 Sun 公司的 JSP/Servlet 引擎,也就是后来的 Apache Tomcat。当时 Solaris 平台有一个专属版本的 make 可以用来构建 JSP/Servlet,但是在开源世界不能对使用那个平台来构建 Tomcat 做出控制,因此 Ant 被开发出来,作为一个与平台无关的工具来构建 Tomcat,并使用 XML 文件作为其构建文件。Ant 1.1 在 2000 年 7 月 19 日作为独立产品公开发行。
有一些提案被用于 Ant 2,例如 James Duncan Davidson 的 AntEater, Peter Donald 的 Myrmidon,Conor MacNeill 的 Mutant。但它们都没有被开发者社区广泛接受。
$ ant
Buildfile: /home/xavier/Exploration/000_build_java_application_with_ant/AntHelloWorld2/build.xml
clean:
[delete] Deleting directory /home/xavier/Exploration/000_build_java_application_with_ant/AntHelloWorld2/build
compile:
[mkdir] Created dir: /home/xavier/Exploration/000_build_java_application_with_ant/AntHelloWorld2/build/classes
[javac] /home/xavier/Exploration/000_build_java_application_with_ant/AntHelloWorld2/build.xml:19: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 1 source file to /home/xavier/Exploration/000_build_java_application_with_ant/AntHelloWorld2/build/classes
jar:
[mkdir] Created dir: /home/xavier/Exploration/000_build_java_application_with_ant/AntHelloWorld2/build/jar
[jar] Building jar: /home/xavier/Exploration/000_build_java_application_with_ant/AntHelloWorld2/build/jar/HelloWorld.jar
run:
[java] Hello World
main:
BUILD SUCCESSFUL
Total time: 5 seconds
View Code
节点表示复制所有的非 .java 后缀的资源文件到 build 目录,这样我们就可以启动 build 目录下的程序并且将这些资源文件包含到 jar 文件中。运行 ant:
$ ant
Buildfile: /home/xavier/exploration/000_build_java_application_with_ant/AntHelloWorld2/build.xml
clean:
[delete] Deleting directory /home/xavier/exploration/000_build_java_application_with_ant/AntHelloWorld2/build
compile:
[mkdir] Created dir: /home/xavier/exploration/000_build_java_application_with_ant/AntHelloWorld2/build/classes
[javac] /home/xavier/exploration/000_build_java_application_with_ant/AntHelloWorld2/build.xml:20: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 1 source file to /home/xavier/exploration/000_build_java_application_with_ant/AntHelloWorld2/build/classes
[copy] Copying 2 files to /home/xavier/exploration/000_build_java_application_with_ant/AntHelloWorld2/build/classes
jar:
[mkdir] Created dir: /home/xavier/exploration/000_build_java_application_with_ant/AntHelloWorld2/build/jar
[jar] Building jar: /home/xavier/exploration/000_build_java_application_with_ant/AntHelloWorld2/build/jar/HelloWorld.jar
run:
[java] Hello World
测试 Java 类
Ant 内置了 JUnit,你可以直接使用 JUnit 测试框架来测试你的代码。新建一个测试类 src/HelloWorldTest.java:
public class HelloWorldTest extends junit.framework.TestCase {
public void testNothing() {
}
public void testWillAlwaysFail() {
fail("An error message");
}
}
注:本文直接使用 ant 内置 JUnit 导致了 package junit.framework does not exist 的报错。尝试将 /usr/share/java/ant-junit4-1.8.2.jar 复制到 ./lib 目录依然无法解决报错。最后只能下载一个 junit.jar 到 ./lib 目录中才解决该问题。