欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Ant实践过程初记

程序员文章站 2022-03-15 20:18:26
...

ant 学习很好得文档:
ANT十五大最佳实践 http://www.oreilly.com.cn/news/ant15toppractices.php?c=java
Tutorial:Hello World with Ant http://ant.apache.org/manual/tutorial-HelloWorldWithAnt.html


ANT:
解压,
设置两项环境变量:
ANT_HOME=D:\apache-ant-1.7.0
PATH中添加 %ANT_HOME%\bin;

输入 ant -projecthelp,可以看到带有描述的任务清单,比如,你可以这样定义任务:
<target name="compile" 
   description="Compiles code, output goes to the build dir.">
将会如下显示:
D:\svn_space\hello>ant -projecthelp
Buildfile: build.xml

Main targets:

 compile  Compiles code, output goes to the build dir.
 doc      create api doc
 pack     make .jar file
 test     run junit test
Default target: doc

我的实践:

目录结构:

 hello
   src
       com.hello.....
   dist
   doc
   report
   WebRoot
        WEB-INF
             lib
             web.xml
        index.jsp
 build.xml

build.xml:

 

<?xml version="1.0" encoding="UTF-8"?>
<!-- ====================================================================== 
     Apr 30, 2008 11:31:26 AM                                                        

     project    
     description
                                                                         
     ====================================================================== -->
<project name="Hello world" default="doc">

	<!-- properies -->
	<property name="src.dir" value="src" />
	<property name="report.dir" value="report" />
	<property name="classes.dir" value="build/classes" />
	<property name="lib.dir" value="WebRoot/WEB-INF/lib" />
	<property name="dist.dir" value="dist" />
	<property name="doc.dir" value="doc" />

	<!-- 定义classpath -->
	<path id="master-classpath">
		<fileset file="${lib.dir}/*.jar" />
		<pathelement path="${classes.dir}" />
	</path>

	<!-- 初始化任务 -->
	<target name="init">
		<echo message="***** target init "/>
	</target>

	<!-- 编译 -->
	<target name="compile" depends="init" description="compile the source files">
		<mkdir dir="${classes.dir}" />
		<echo message="****** compile method" />
		<javac srcdir="${src.dir}" destdir="${classes.dir}" target="1.5">
			<classpath refid="master-classpath" />
		</javac>
	</target>

	<!-- 测试 -->
	<target name="test" depends="compile" description="run junit test">
		<mkdir dir="${report.dir}" />
		<junit printsummary="on" haltonfailure="false" failureproperty="tests.failed" showoutput="true">
			<classpath refid="master-classpath" />
			<formatter type="plain" />
			<batchtest todir="${report.dir}">
				<fileset dir="${classes.dir}">
					<include name="**/*Test.*" />
				</fileset>
			</batchtest>
		</junit>
		<fail if="tests.failed">
            ***********************************************************
            ****  One or more tests failed!  Check the output ...  ****
            ***********************************************************
            </fail>
	</target>

	<!-- 打包成jar -->
	<target name="pack" depends="test" description="make .jar file">
		<mkdir dir="${dist.dir}" />
		<jar destfile="${dist.dir}/hello.jar" basedir="${classes.dir}">
			<exclude name="**/*Test.*" />
			<exclude name="**/Test*.*" />
		</jar>
	</target>

	<!-- 输出api文档 -->
	<target name="doc" depends="pack" description="create api doc">
		<mkdir dir="${doc.dir}" />
		<javadoc destdir="${doc.dir}" author="true" version="true" use="true" windowtitle="Test API">
			<packageset dir="${src.dir}" defaultexcludes="yes">
				<include name="com/**" />
			</packageset>
			<doctitle>
				<![CDATA[<h1>Hello, test</h1>]]></doctitle>
			<bottom>
				<![CDATA[<i>All Rights Reserved.</i>]]></bottom>
			<tag name="todo" scope="all" description="To do:" />
		</javadoc>
	</target>
</project>

 

执行ant结果:

Buildfile: D:\svn_space\hello\build.xml

init:
        [echo] ***** target init

compile:
       [mkdir] Created dir: D:\svn_space\hello\build\classes
        [echo] ****** compile method
       [javac] Compiling 1 source file to D:\svn_space\hello\build\classes

test:

pack:
         [jar] Building jar: D:\svn_space\hello\dist\hello.jar

doc:
     [javadoc] Generating Javadoc
     [javadoc] Javadoc execution
     [javadoc] 正在装入软件包 com.hello 的源文件...
     [javadoc] 正在构造 Javadoc 信息...
     [javadoc] 标准 Doclet 版本 1.5.0_06
     [javadoc] 正在构建所有软件包和类的树...
     [javadoc] 正在生成 D:\svn_space\hello\doc\com/hello/\HelloOK.html...
     [javadoc] D:\svn_space\hello\src\com\hello\HelloOK.java:10: 警告 - @date 是未知标记。
     [javadoc] 正在生成 D:\svn_space\hello\doc\com/hello/\package-frame.html...
     [javadoc] 正在生成 D:\svn_space\hello\doc\com/hello/\package-summary.html...
     [javadoc] 正在生成 D:\svn_space\hello\doc\com/hello/\package-tree.html...
     [javadoc] 正在生成 D:\svn_space\hello\doc\constant-values.html...
     [javadoc] 正在生成 D:\svn_space\hello\doc\com/hello/\class-use\HelloOK.html...
     [javadoc] 正在生成 D:\svn_space\hello\doc\com/hello/\package-use.html...
     [javadoc] 正在构建所有软件包和类的索引...
     [javadoc] 正在生成 D:\svn_space\hello\doc\overview-tree.html...
     [javadoc] 正在生成 D:\svn_space\hello\doc\index-all.html...
     [javadoc] 正在生成 D:\svn_space\hello\doc\deprecated-list.html...
     [javadoc] 正在构建所有类的索引...
     [javadoc] 正在生成 D:\svn_space\hello\doc\allclasses-frame.html...
     [javadoc] 正在生成 D:\svn_space\hello\doc\allclasses-noframe.html...
     [javadoc] 正在生成 D:\svn_space\hello\doc\index.html...
     [javadoc] 正在生成 D:\svn_space\hello\doc\help-doc.html...
     [javadoc] 正在生成 D:\svn_space\hello\doc\stylesheet.css...
     [javadoc] 注意:可能覆盖将来的标准标记的自定义标记: @todo。为了避免出现可能的覆盖,请在自定义标记名称中至少使用一个句点字符 (.)。
     [javadoc] 注意:未找到的自定义标记:  @todo
     [javadoc] 1 警告
BUILD SUCCESSFUL
Total time: 5 seconds