首页 > 代码库 > 一个build.xml实例
一个build.xml实例
<?xml version="1.0"?>
<project name="ssh" basedir="." default="usage">
<property name="name" value="http://www.mamicode.com/ssh"/>
<property name="war.dir" value="http://www.mamicode.com/war"/>
<property name="src.dir" value="http://www.mamicode.com/src"/>
<property name="client.dir" value="http://www.mamicode.com/client"/>
<property name="build.dir" value="http://www.mamicode.com/.classes"/>
<property name="webcontent.dir" value="http://www.mamicode.com/WebContent"/>
<property name="prjlib.dir" value="http://www.mamicode.com/lib"/>
<property name="webcontentlib.dir" value="http://www.mamicode.com/${webcontent.dir}/WEB-INF/lib"/>
<property name="weblib.dir" value="http://www.mamicode.com/${war.dir}/WEB-INF/lib"/>
<property name="dist.dir" value="http://www.mamicode.com/dist"/>
<property environment="env"/>
<property name="tomcat.home" value="http://www.mamicode.com/${env.CATALINA_HOME}"/>
<property name="webapp.dist" value="http://www.mamicode.com/${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>
Ant是Java平台下很棒的批处理命令运行程序,能很方便地自己主动完毕编译,測试,打包,部署等等一系列任务,大大提高开发效率。假设你如今还没有開始使用Ant,那就要赶快開始学习使用,使自己的开发水平上一个新台阶。
Eclipse中已经集成了Ant,我们能够直接在Eclipse中运行Ant。
曾经面建立的Helloproject为例,创建下面文件夹结构:
新建一个build.xml,放在project根文件夹下。build.xml定义了Ant要运行的批处理命令。尽管Ant也能够使用其他文件名称,可是遵循标准能更使开发更规范,同一时候易于与别人交流。
通常,src存放Java源文件,classes存放编译后的class文件,lib存放编译和执行用到的全部jar文件,web存放JSP等web文件,dist存放打包后的jar文件,doc存放API文档。
然后在根文件夹下创建build.xml文件,输入下面内容:
<?xml version="1.0"?>
<project name="Hello world" default="doc"><!-- properies -->
<property name="src.dir" value="http://www.mamicode.com/src" />
<property name="report.dir" value="http://www.mamicode.com/report" />
<property name="classes.dir" value="http://www.mamicode.com/classes" />
<property name="lib.dir" value="http://www.mamicode.com/lib" />
<property name="dist.dir" value="http://www.mamicode.com/dist" />
<property name="doc.dir" value="http://www.mamicode.com/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>
以上xml依次定义了init(初始化),compile(编译),test(測试),doc(生成文档),pack(打包)任务,能够作为模板。
选中Helloproject,然后选择“Project”,“Properties”,“Builders”,“New…”,选择“Ant Build”:
填入Name:Ant_Builder;Buildfile:build.xml;Base Directory:${workspace_loc:/Hello}(按“Browse Workspace”选择project根文件夹),因为用到了junit.jar包,搜索Eclipse文件夹,找到junit.jar,把它拷贝到Hello/lib文件夹下,并加入到Ant的Classpath中:
然后在Builder面板中钩上Ant_Build,去掉Java Builder:
再次编译,就可以在控制台看到Ant的输出:
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/classestest:
[mkdir] Created dir: F:/eclipse-projects/Hello/report
[junit] Running example.HelloTest
[junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.02 secpack:
[mkdir] Created dir: F:/eclipse-projects/Hello/dist
[jar] Building jar: F:/eclipse-projects/Hello/dist/hello.jardoc:
[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 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
BUILD SUCCESSFUL
Total time: 11 seconds
Ant依次运行初始化,编译,測试,打包,生成API文档一系列任务,极大地提高了开发效率。将来开发J2EE项目时,还可增加部署等任务。而且,即使脱离了Eclipse环境,仅仅要正确安装了Ant,配置好环境变量ANT_HOME=<Ant解压文件夹>,Path=…;%ANT_HOME%/bin,在命令行提示符下切换到Hello文件夹,简单地键入ant就可以。
一个build.xml实例