一、构建Ant环境
①免安装构建:如果你已经配置好Eclipse环境(3.0以上),那Ant环境就已经构建好了;
②从零开始手工配置:
A:配置好Java环境;
安装JDK,然后设置好环境变量,如:
Java 环境配置:
Java安装目录:E:\Java\jdk1.5.0_12
在“系统”-》“高级”-》“环境变量”-》“系统参数”中
1、新增JAVA_HOME:
变量名:JAVA_HOME
变量值:E:"Java\jdk1.5.0_12
2、新增CLASSPATH:
变量名:CLASSPATH
变量值:.;%JAVA_HOME%\lib\tool.jar;%JAVA_HOME%\lib\dt.jar;
3、在Path变量中增加Java的bin目录
已存在的变量名:Path
在最前面新增的值:.;%JAVA_HOME%\bin;
Java安装目录:E:\Java\jdk1.5.0_12
在“系统”-》“高级”-》“环境变量”-》“系统参数”中
1、新增JAVA_HOME:
变量名:JAVA_HOME
变量值:E:"Java\jdk1.5.0_12
2、新增CLASSPATH:
变量名:CLASSPATH
变量值:.;%JAVA_HOME%\lib\tool.jar;%JAVA_HOME%\lib\dt.jar;
3、在Path变量中增加Java的bin目录
已存在的变量名:Path
在最前面新增的值:.;%JAVA_HOME%\bin;
B、下载Ant包:
地址:http://ant.apache.org/bindownload.cgi
配置Ant环境变量:
Ant 环境配置:
Ant安装目录:D:\ant
在“系统”-》“高级”-》“环境变量”-》“系统参数”中
1、新增ANT_HOME:
变量名:ANT_HOME
变量值:D:\ant
2、在Path变量中增加Ant的bin目录
已存在的变量名:Path
在最前面新增的值:.;%ANT_HOME%"bin;
Ant安装目录:D:\ant
在“系统”-》“高级”-》“环境变量”-》“系统参数”中
1、新增ANT_HOME:
变量名:ANT_HOME
变量值:D:\ant
2、在Path变量中增加Ant的bin目录
已存在的变量名:Path
在最前面新增的值:.;%ANT_HOME%"bin;
测试:run-》cmd,输入ant,如果出现下面的画面,测表明Ant环境已经配置好了。
二、简单上手:
build.xml
<?
xml version="1.0"
?>
< project default ="main" basedir ="." >
< target name ="main" >
< javac srcdir ="src\main\hello\ant" destdir ="bin" />
< java classname ="main.hello.ant.HelloAnt" >
< classpath >
< pathelement path ="bin" />
</ classpath >
</ java >
</ target >
</ project >
< project default ="main" basedir ="." >
< target name ="main" >
< javac srcdir ="src\main\hello\ant" destdir ="bin" />
< java classname ="main.hello.ant.HelloAnt" >
< classpath >
< pathelement path ="bin" />
</ classpath >
</ java >
</ target >
</ project >
HelloAnt.java
package
main.hello.ant;
public class HelloAnt {
public static void main(String[] args) {
System.out.println( " Hello World From HelloAnt! " );
}
}
public class HelloAnt {
public static void main(String[] args) {
System.out.println( " Hello World From HelloAnt! " );
}
}
目录结构:
![](https://i-blog.csdnimg.cn/blog_migrate/7a3faed52b7a612cfcca0cb6d6782f48.jpeg)
运行结果:
![](https://i-blog.csdnimg.cn/blog_migrate/0e527a51560c23b6b3b4b29eb0752e3e.jpeg)
三、Ant提高
改进build.xml,让它做更多的事情:
定义全局变量
初始化,主要是建立目录
编译(已有)
打包为jar
建立API documentation
生成 发布(distribution) 产品
build.xml
<?
xml version="1.0" encoding="UTF-8"
?>
< project default ="dist" basedir ="." >
<!-- 主要的系统环境属性 -->
<!-- 取Window,Unix
的环境变量
-->
< property environment ="env" />
< property name ="java.home" value ="${env.JAVA_HOME}" />
< property name ="ant.home" value ="${env.ANT_HOME}" />
<!-- 主要的app环境属性 -->
< property name ="app.name" value ="hello-ant" />
< property name ="app.jar" value ="${app.name}.jar" />
< property name ="app.copyright" value =" Copyright(c) 2007 CoderDream's Studio All rights reserved." />
<!-- app中的src属性 -->
< property name ="src.dir" value ="src" />
< property name ="src.main" value ="${src.dir}/main" />
< property name ="src.script" value ="${src.dir}/script" />
<!-- app用到的lib -->
< property name ="lib.dir" value ="lib" />
<!-- app的build目录中 -->
< property name ="build.dir" value ="build" />
< property name ="build.classes" value ="${build.dir}/classes" />
< property name ="build.docs" value ="${build.dir}/doc" />
< property name ="build.docs.api" value ="${build.docs}/api" />
< property name ="build.lib" value ="${build.dir}/lib" />
<!-- app的dist(distribution发布)目录中 -->
< property name ="dist.dir" value ="dist" />
< property name ="dist.bin" value ="${dist.dir}/bin" />
< property name ="dist.docs" value ="${dist.dir}/docs" />
< property name ="dist.lib" value ="${dist.dir}/lib" />
<!-- app的docs目录中 -->
< property name ="docs.dir" value ="docs" />
< property name ="report" value ="report" />
< path id ="classpath" >
< pathelement location ="${build.classes}" />
< pathelement path ="${java.home}/lib/tools.jar" />
</ path >
< target name ="init" >
<!-- 清除以前的目录 -->
< delete dir ="${build.dir}" failonerror ="false" />
< delete dir ="${dist.dir}" failonerror ="false" />
<!-- 准备目录 -->
< mkdir dir ="${build.dir}" />
< mkdir dir ="${build.classes}" />
< mkdir dir ="${build.docs}" />
< mkdir dir ="${build.docs.api}" />
< mkdir dir ="${build.lib}" />
< mkdir dir ="${dist.dir}" />
< mkdir dir ="${dist.bin}" />
< mkdir dir ="${dist.lib}" />
</ target >
< target name ="build" depends ="init" >
< javac srcdir ="${src.dir}" destdir ="${build.classes}" >
< classpath refid ="classpath" />
</ javac >
</ target >
< target name ="jars" depends ="build" >
< jar basedir ="${build.classes}" jarfile ="${build.lib}/${app.jar}" />
</ target >
< target name ="javadocs" depends ="jars" description ="-> creates the API documentation" >
<!-- copy docs 手册 -->
< copy todir ="${build.docs}" >
< fileset dir ="${docs.dir}" />
</ copy >
< javadoc packagenames ="main.hello.ant.*"
sourcepath ="${src.dir}"
defaultexcludes ="yes"
destdir ="${build.docs.api}"
author ="true"
version ="true"
use ="true"
windowtitle ="Docs API" >
< doctitle >
<![CDATA[ <h1>hello and Docs API</h1> ]]>
</ doctitle >
< bottom >
<![CDATA[ <i>${app.copyright}</i> ]]>
</ bottom >
< tag name ="todo" scope ="all" description ="To do:" />
</ javadoc >
</ target >
< target name ="dist" depends ="javadocs" >
<!-- copy bin 执行文件 -->
< copy todir ="${dist.bin}" >
< fileset dir ="${src.script}/" />
</ copy >
<!-- copy doc 执行文件 -->
< copy todir ="${dist.docs}" >
< fileset dir ="${build.docs}/" />
</ copy >
<!-- copy lib 执行文件 -->
< copy todir ="${dist.lib}" >
< fileset dir ="${build.lib}/" />
</ copy >
</ target >
< target name ="junitreport" depends ="build" >
< junit printsummary ="true" failureproperty ="tests.failed" >
< test name ="main.hello.ant.TestHelloAnt" />
< classpath >
< pathelement location ="${build.classes}" />
</ classpath >
</ junit >
< junitreport todir ="${report}" >
< fileset dir ="${report}" >
< include name ="TEST-*.xml" />
</ fileset >
< report format ="frames" todir ="${report}" />
</ junitreport >
< fail if ="tests.failed" >
--fail!--
</ fail >
</ target >
</ project >
< project default ="dist" basedir ="." >
<!-- 主要的系统环境属性 -->
<!-- 取Window,Unix
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
< property environment ="env" />
< property name ="java.home" value ="${env.JAVA_HOME}" />
< property name ="ant.home" value ="${env.ANT_HOME}" />
<!-- 主要的app环境属性 -->
< property name ="app.name" value ="hello-ant" />
< property name ="app.jar" value ="${app.name}.jar" />
< property name ="app.copyright" value =" Copyright(c) 2007 CoderDream's Studio All rights reserved." />
<!-- app中的src属性 -->
< property name ="src.dir" value ="src" />
< property name ="src.main" value ="${src.dir}/main" />
< property name ="src.script" value ="${src.dir}/script" />
<!-- app用到的lib -->
< property name ="lib.dir" value ="lib" />
<!-- app的build目录中 -->
< property name ="build.dir" value ="build" />
< property name ="build.classes" value ="${build.dir}/classes" />
< property name ="build.docs" value ="${build.dir}/doc" />
< property name ="build.docs.api" value ="${build.docs}/api" />
< property name ="build.lib" value ="${build.dir}/lib" />
<!-- app的dist(distribution发布)目录中 -->
< property name ="dist.dir" value ="dist" />
< property name ="dist.bin" value ="${dist.dir}/bin" />
< property name ="dist.docs" value ="${dist.dir}/docs" />
< property name ="dist.lib" value ="${dist.dir}/lib" />
<!-- app的docs目录中 -->
< property name ="docs.dir" value ="docs" />
< property name ="report" value ="report" />
< path id ="classpath" >
< pathelement location ="${build.classes}" />
< pathelement path ="${java.home}/lib/tools.jar" />
</ path >
< target name ="init" >
<!-- 清除以前的目录 -->
< delete dir ="${build.dir}" failonerror ="false" />
< delete dir ="${dist.dir}" failonerror ="false" />
<!-- 准备目录 -->
< mkdir dir ="${build.dir}" />
< mkdir dir ="${build.classes}" />
< mkdir dir ="${build.docs}" />
< mkdir dir ="${build.docs.api}" />
< mkdir dir ="${build.lib}" />
< mkdir dir ="${dist.dir}" />
< mkdir dir ="${dist.bin}" />
< mkdir dir ="${dist.lib}" />
</ target >
< target name ="build" depends ="init" >
< javac srcdir ="${src.dir}" destdir ="${build.classes}" >
< classpath refid ="classpath" />
</ javac >
</ target >
< target name ="jars" depends ="build" >
< jar basedir ="${build.classes}" jarfile ="${build.lib}/${app.jar}" />
</ target >
< target name ="javadocs" depends ="jars" description ="-> creates the API documentation" >
<!-- copy docs 手册 -->
< copy todir ="${build.docs}" >
< fileset dir ="${docs.dir}" />
</ copy >
< javadoc packagenames ="main.hello.ant.*"
sourcepath ="${src.dir}"
defaultexcludes ="yes"
destdir ="${build.docs.api}"
author ="true"
version ="true"
use ="true"
windowtitle ="Docs API" >
< doctitle >
<![CDATA[ <h1>hello and Docs API</h1> ]]>
</ doctitle >
< bottom >
<![CDATA[ <i>${app.copyright}</i> ]]>
</ bottom >
< tag name ="todo" scope ="all" description ="To do:" />
</ javadoc >
</ target >
< target name ="dist" depends ="javadocs" >
<!-- copy bin 执行文件 -->
< copy todir ="${dist.bin}" >
< fileset dir ="${src.script}/" />
</ copy >
<!-- copy doc 执行文件 -->
< copy todir ="${dist.docs}" >
< fileset dir ="${build.docs}/" />
</ copy >
<!-- copy lib 执行文件 -->
< copy todir ="${dist.lib}" >
< fileset dir ="${build.lib}/" />
</ copy >
</ target >
< target name ="junitreport" depends ="build" >
< junit printsummary ="true" failureproperty ="tests.failed" >
< test name ="main.hello.ant.TestHelloAnt" />
< classpath >
< pathelement location ="${build.classes}" />
</ classpath >
</ junit >
< junitreport todir ="${report}" >
< fileset dir ="${report}" >
< include name ="TEST-*.xml" />
</ fileset >
< report format ="frames" todir ="${report}" />
</ junitreport >
< fail if ="tests.failed" >
--fail!--
</ fail >
</ target >
</ project >
执行结果:
1、Console信息:
Buildfile: E:\XL\workspace\Ant03\build.xml
init:
[ delete ] Deleting directory E:\XL\workspace\Ant03\build
[ delete ] Deleting directory E:\XL\workspace\Ant03\dist
[ mkdir ] Created dir: E:\XL\workspace\Ant03\build
[ mkdir ] Created dir: E:\XL\workspace\Ant03\build\classes
[ mkdir ] Created dir: E:\XL\workspace\Ant03\build\doc
[ mkdir ] Created dir: E:\XL\workspace\Ant03\build\doc\api
[ mkdir ] Created dir: E:\XL\workspace\Ant03\build\lib
[ mkdir ] Created dir: E:\XL\workspace\Ant03\dist
[ mkdir ] Created dir: E:\XL\workspace\Ant03\dist\bin
[ mkdir ] Created dir: E:\XL\workspace\Ant03\dist\lib
build:
[ javac ] Compiling 1 source file to E:\XL\workspace\Ant03\build\classes
jars:
[ jar ] Building jar: E:\XL\workspace\Ant03\build\lib\hello - ant.jar
javadocs:
[ javadoc ] Generating Javadoc
[ javadoc ] Javadoc execution
[ javadoc ] Loading source files for package main.hello.ant![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
[ javadoc ] Constructing Javadoc information![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
[ javadoc ] Standard Doclet version 1.5 .0_ 12
[ javadoc ] Building tree for all the packages and classes![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
[ javadoc ] Building index for all the packages and classes![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
[ javadoc ] Building index for all classes![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
[ javadoc ] Generating E:\XL\workspace\Ant03\build\doc\api\stylesheet.css![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
[ javadoc ] Note: Custom tags that could override future standard tags: @todo .
To avoid potential overrides, use at least one period character (.) in custom tag names.
[ javadoc ] Note: Custom tags that were not seen: @todo
dist:
[ copy ] Copying 17 files to E:\XL\workspace\Ant03\dist\docs
[ copy ] Copying 1 file to E:\XL\workspace\Ant03\dist\lib
BUILD SUCCESSFUL
Total time: 4 seconds
init:
[ delete ] Deleting directory E:\XL\workspace\Ant03\build
[ delete ] Deleting directory E:\XL\workspace\Ant03\dist
[ mkdir ] Created dir: E:\XL\workspace\Ant03\build
[ mkdir ] Created dir: E:\XL\workspace\Ant03\build\classes
[ mkdir ] Created dir: E:\XL\workspace\Ant03\build\doc
[ mkdir ] Created dir: E:\XL\workspace\Ant03\build\doc\api
[ mkdir ] Created dir: E:\XL\workspace\Ant03\build\lib
[ mkdir ] Created dir: E:\XL\workspace\Ant03\dist
[ mkdir ] Created dir: E:\XL\workspace\Ant03\dist\bin
[ mkdir ] Created dir: E:\XL\workspace\Ant03\dist\lib
build:
[ javac ] Compiling 1 source file to E:\XL\workspace\Ant03\build\classes
jars:
[ jar ] Building jar: E:\XL\workspace\Ant03\build\lib\hello - ant.jar
javadocs:
[ javadoc ] Generating Javadoc
[ javadoc ] Javadoc execution
[ javadoc ] Loading source files for package main.hello.ant
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
[ javadoc ] Constructing Javadoc information
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
[ javadoc ] Standard Doclet version 1.5 .0_ 12
[ javadoc ] Building tree for all the packages and classes
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
[ javadoc ] Building index for all the packages and classes
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
[ javadoc ] Building index for all classes
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
[ javadoc ] Generating E:\XL\workspace\Ant03\build\doc\api\stylesheet.css
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
[ javadoc ] Note: Custom tags that could override future standard tags: @todo .
To avoid potential overrides, use at least one period character (.) in custom tag names.
[ javadoc ] Note: Custom tags that were not seen: @todo
dist:
[ copy ] Copying 17 files to E:\XL\workspace\Ant03\dist\docs
[ copy ] Copying 1 file to E:\XL\workspace\Ant03\dist\lib
BUILD SUCCESSFUL
Total time: 4 seconds
2、生成jar文件和docs API
![](https://i-blog.csdnimg.cn/blog_migrate/0e1b2639c972a390ccae037b70f9c80b.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/59bc30839ce5a12c87acee8d3f802fff.jpeg)
原文地址: http://www.blogjava.net/coderdream/archive/2007/09/18/146075.html