set ant_home=D:/java/kit/ant/jakarta-ant-1.5.1 这是我的目录
我们要开发一个java类:其内容只有一句,输出"hello ant"字符串。并使用ant完成编译和运行工作,这个例子只是为了跑通ant,不附加多余的东西。
下面是:“hello.ant.HelloAnt.java”文件。
package hello.ant;
public class HelloAnt{
public static void main(String[] args){
System.out.println("hello ant,ant 的第一次接触,好棒!");
}
}
在项目根目录(hello-ant/)写1个文件:ant执行配置文件build.xml
“build.xml”文件
<?xml version="1.0" encoding="GB2312" ?>
<!-- 一个项目,可包含很多任务组(target) -->
<project default="main" basedir=".">
<!-- 项目中的一个任务组,可包含很多任务(task:javac,java...) -->
<target name="main">
<!--编译-->
<javac srcdir="src/main/hello/ant" destdir="build/classes"/>
<!--运行-->
<java classname="hello.ant.HelloAnt">
<classpath>
<pathelement path="build/classes"/>
</classpath>
</java>
</target>
</project>
ok,一切大功告成,哦,不,还没有运行它。
dos下进入hello-ant的目录,即build.xml所在的目录,我们要用ant工具执行它 ,
执行: %ant_home%/bin/ant -file build.xml 用ant工具执行当前目录下的配置文件build.xml
或 :ant -file build.xml 你如果设置%ant_home%/bin到path中
这次ok了,这是答案:
命令提示符窗口
D:/temp/hello-ant>ant -file build.xml
Build build.xml
main:
[javac] Compiling 1 source file to D:/temp/hello-ant/build/classes
[java] hello ant,ant 的第一次接触,好棒!
BUILD SUCCESSFUL
Total time: 2 seconds
D:/temp/hello-ant>
检查一下build/classes目录,哦,看到编译过的文件就在这里:
build/classes/hello/ant/HelloAnt.class.
hello ant 进级
我们要改进build.xml,让它做更多的事情:
定义全局变量
初始化,主要是建立目录
编译 (已有)
打包为jar
建立API documentation
生成distribution产品
凡事都讲究平衡,你要ant给你做更多事,当然要累一点点,不过只用累一次,以后的代码修改后的构建都是"一键式"完成,我们制作一个hello的简单例子,你可以自己做j2ee的练习。
我们要扩充目录结构,使它更像回事:
:/src,/docs,/lib是自己组织的文件结构,/build,/dist是ant动态生成的成品。
/src 源文件:java源,源,jsp源,xml配置.....
/src/main java源
/src/ window,unix,liunx的执行,我们的简单只有一个:
run.bat: java hello.ant.HelloAnt
/docs 手写说明文档
/lib 程序所需类库的jar,比如j2ee.jar,mail,jar...
/build 用ant动态生成的构建目录
/build/classes 编译的类文件
/build/docs copy "/docs"的手写说明文档,和ant生成的api文档
/build/lib 放置我们自己的HelloAnt.class打包成品hello-ant.jar
/dist/bin copy "/src/" 得执行文件
/dist/docs copy "/build/docs" 的文档
/dist/lib 除了copy "/build/lib"下的hello-ant.jar外,
还应copy "/lib"的程序所需jar,这里我们没有。
以上是我学老外的文件组织,大家可以按照自己的爱好组织
我们编写必要的文件:
hello.ant. HelloAnt.java
src/.bat
@echo off
echo ========================================================
echo 请先设置 Environment
echo .
echo JAVA_HOME: %JAVA_HOME%
echo ======================================================
%java_home%/bin/java -classpath ../lib/hello-ant.jar hello.ant.HelloAnt
/docs/index.html 随便写一个手写的文档
hello ant 软件项目手册docs
--------------------------------------------------------------------------------
/build.xml 配置文件
<?xml version="1.0" encoding="GB2312" ?>
<!--
=========================================================&