Ant 的build.xml(二)-----实例
趁热打铁,接着上面的再发一个实例:
<?xml version="1.0"?>
<project name="ssh" basedir="." default="usage">
<property name="name" value="ssh"/>
<property name="war.dir" value="war"/>
<property name="src.dir" value="src"/>
<property name="client.dir" value="client"/>
<property name="build.dir" value=".classes"/>
<property name="webcontent.dir" value="WebContent"/>
<property name="prjlib.dir" value="lib"/>
<property name="webcontentlib.dir" value="${webcontent.dir}/WEB-INF/lib"/>
<property name="weblib.dir" value="${war.dir}/WEB-INF/lib"/>
<property name="dist.dir" value="dist"/>
<property environment="env"/>
<property name="tomcat.home" value="${env.CATALINA_HOME}"/>
<property name="webapp.dist" value="${dist.dir}/webapps"/>
<path id="master-classpath">
<fileset dir="${webcontentlib.dir}">
<include name="hibernate3.jar"/>
<include name="spring.jar"/>
<include name="struts.jar"/>
<include name="struts-el.jar"/>
<include name="struts-menu-2.4.2.jar"/>
<include name="acegi-security-1.0.2.jar"/>
<include name="activation.jar"/>
<include name="antlr.jar"/>
<include name="antlr-2.7.6.jar"/>
<include name="asm.jar"/>
<include name="aspectjweaver-1.5.2.jar"/>
<include name="cglib-2.1.3.jar"/>
<include name="commons-beanutils.jar"/>
<include name="commons-codec-1.3.jar"/>
<include name="commons-collections.jar"/>
<include name="commons-dbcp.jar"/>
<include name="commons-digester.jar"/>
<include name="commons-fileupload.jar"/>
<include name="commons-io.jar"/>
<include name="commons-lang.jar"/>
<include name="commons-logging-1.1.jar"/>
<include name="commons-pool.jar"/>
<include name="commons-validator.jar"/>
<include name="displaytag-1.1.jar"/>
<include name="dom4j-1.6.1.jar"/>
<include name="dwr.jar"/>
<include name="ehcache-1.2.3.jar"/>
<include name="itext-1.4.jar"/>
<include name="jakarta-oro.jar"/>
<include name="jstl.jar"/>
<include name="jta.jar"/>
<include name="log4j-1.2.11.jar"/>
<include name="mail.jar"/>
<include name="oscache-2.3.2.jar"/>
<include name="mysql-connector-java-5.0.3-bin.jar"/>
<include name="sitemesh-2.2.1.jar"/>
<include name="standard.jar"/>
<include name="urlrewrite-3.0-beta.jar"/>
<include name="velocity-1.4.jar"/>
<include name="velocity-tools-view-1.1.jar"/>
</fileset>
<fileset dir="${prjlib.dir}/servletapi-2.3">
<include name="servletapi-2.3.jar"/>
</fileset>
</path>
<target name="usage">
<echo message=""/>
<echo message="Spring JPetStore build file"/>
<echo message="------------------------------------------------------"/>
<echo message=""/>
<echo message="Available targets are:"/>
<echo message=""/>
<echo message="clean --> Clean output dirs"/>
<echo message="build --> Compile main Java sources and copy libraries"/>
<echo message="warfile --> Build the web application archive"/>
<echo message="all --> Clean, build, warfile"/>
<echo message=""/>
</target>
<target name="clean" description="Clean output dirs (build, weblib, dist)">
<delete dir="${build.dir}"/>
<delete dir="${weblib.dir}"/>
<delete dir="${war.dir}"/>
<delete dir="${dist.dir}"/>
<delete file="client/${name}.jar"/>
</target>
<target name="build" description="Compile main source tree java files into class files, generate jar files">
<mkdir dir="${build.dir}"/>
<mkdir dir="${war.dir}"/>
<javac destdir="${build.dir}" source="1.3" target="1.3" debug="true"
deprecation="false" optimize="false" failonerror="true">
<src path="${src.dir}"/>
<classpath refid="master-classpath"/>
</javac>
<mkdir dir="${weblib.dir}"/>
<mkdir dir="${war.dir}/WEB-INF/classes"/>
<jar jarfile="${weblib.dir}/${name}.jar" compress="true" basedir="${build.dir}"/>
<copy todir="${war.dir}" preservelastmodified="true">
<fileset dir="${webcontent.dir}">
<include name="**/**"/>
<include name="**.**"/>
</fileset>
</copy>
<copy todir="${war.dir}/WEB-INF/classes" preservelastmodified="true">
<fileset dir="${src.dir}">
<include name="*.xml"/>
<include name="**/*.properties"/>
<include name="**/*.vm"/>
<exclude name="**/.*"/>
</fileset>
</copy>
<copy file="${weblib.dir}/${name}.jar" tofile="${client.dir}/${name}.jar"/>
</target>
<target name="dist" depends="warfile">
<!--
Delegate to warfile target by depending on it. dist is just to offer
a generic target name across all Spring sample apps that may be used
for autobuilds testing.
-->
</target>
<target name="warfile" depends="build" description="Build the web application archive">
<mkdir dir="${dist.dir}"/>
<war warfile="${dist.dir}/${name}.war" basedir="${war.dir}" webxml="${webcontent.dir}/WEB-INF/web.xml">
<include name="*"/>
<include name="images/**"/>
<include name="common/**"/>
<include name="decorators/**"/>
<include name="scripts/**"/>
<include name="styles/**"/>
<include name="WEB-INF/*.*"/>
<include name="WEB-INF/lib/**"/>
<include name="WEB-INF/pages/**"/>
<include name="WEB-INF/classes/**"/>
<exclude name="WEB-INF/web.xml"/>
</war>
</target>
<target name="all" depends="clean,build,warfile" description="Clean,build,warfile"/>
</project>
在Eclipse中使用Ant Ant是Java平台下非常棒的批处理命令执行程序,能非常方便地自动完成编译,测试,打包,部署等等一系列任务,大大提高开发效率。如果你现在还没有开 始使用Ant,那就要赶快开始学习使用,使自己的开发水平上一个新台阶。
Eclipse中已经集成了Ant,我们可以直接在 Eclipse中运行Ant。
以前面建立的Hello工程为例,创建以下目录结构:
新建一个build.xml,放在工程根目录下。 build.xml定义了Ant要执行的批处理命令。虽然Ant也可以使用其它文件名,但是遵循标准能更使开发更规范,同时易于与别人交流。
通 常,src存放Java源文件,classes存放编译后的class文件,lib存放编译和运行用到的所有jar文件,web存放JSP等web文 件,dist存放打包后的jar文件,doc存放API文档。
然后在根目录下创建build.xml文件,输入以下内容:
<?xml version="1.0"?>以 上xml依次定义了init(初始化),compile(编译),test(测试),doc(生成文档),pack(打包)任务,可以作为模板。
<project name="Hello world" default="doc">
<!-- properies -->
<property name="src.dir" value="src" />
<property name="report.dir" value="report" />
<property name="classes.dir" value="classes" />
<property name="lib.dir" value="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">
</target>
<!-- 编译 -->
<target name="compile" depends="init" description="compile the source files">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}" target="1.4">
<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="example/**" />
</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>
Buildfile: F:\eclipse-projects\Hello\build.xml
init:
compile:
[mkdir] Created dir: F:\eclipse-projects\Hello\classes
[javac] Compiling 2 source files to F:\eclipse-projects\Hello\classes
test:
[mkdir] Created dir: F:\eclipse-projects\Hello\report
[junit] Running example.HelloTest
[junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.02 sec
pack:
[mkdir] Created dir: F:\eclipse-projects\Hello\dist
[jar] Building jar: F:\eclipse-projects\Hello\dist\hello.jar
doc:
[mkdir] Created dir: F:\eclipse-projects\Hello\doc
[javadoc] Generating Javadoc
[javadoc] Javadoc execution
[javadoc] Loading source files for package example...
[javadoc] Constructing Javadoc information...
[javadoc] Standard Doclet version 1.4.2_04
[javadoc] Building tree for all the packages and classes...
[javadoc] Building index for all the packages and classes...
[javadoc] Building index for all classes...
[javadoc] Generating F:\eclipse-projects\Hello\doc\stylesheet.css...
[javadoc] Note: Custom tags that could override future standardtags: @todo. To avoid potential overrides, use at least one periodcharacter (.) in custom tag names.
[javadoc] Note: Custom tags that were not seen: @todo
BUILD SUCCESSFUL
Total time: 11 seconds
Ant依次执行初始化,编译,测试,打包,生成API文档一系列任务,极 大地提高了开发效率。将来开发J2EE项目时,还可加入部署等任务。并且,即使脱离了Eclipse环境,只要正确安装了Ant,配置好环境变量 ANT_HOME=<Ant解压目录>, Path=…;%ANT_HOME%\bin,在命令行提示符下切换到Hello目录,简单地键入ant即可。
转自---http://blog.csdn.net/angelseyes/archive/2008/10/13/3070562.aspx