build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project default="run" name="build-project-with-ant">
<!-- 最终编译后的jar包名称 -->
<property name="jar.prefix-name" value="cms"></property>
<property name="jar.version" value="1.0"></property>
<property name="tomcat.version" value="6.0.18" />
<property name="mailhost" value="smtp.qq.com"></property>
<property name="username" value="**********" />
<property name="password" value="**********"></property>
<property name="mailfrom" value="184675420@qq.com" />
<property name="mailto" value="184675420@qq.com"></property>
<property name="mailsubject" value="使用Ant自助构建项目测试打包文档"></property>
<!-- 项目构建时所需要的lib -->
<property name="project.lib.dir" value="${basedir}/WebRoot/WEB-INF/lib"></property>
<!-- 项目构建时tomcat的home目录 -->
<property name="tomcat.download.url" value="http://archive.apache.org/dist/tomcat/tomcat-6/v${tomcat.version}/bin/apache-tomcat-${tomcat.version}.zip"></property>
<!-- 项目构建时下载tomcat的存放目录 -->
<property name="tomcat.dir" value="${basedir}/dist/"></property>
<!-- 定义项目的当前目录 -->
<property name="basedir" value="."/>
<!-- 定义项目源码目录 -->
<property name="src.dir" value="${basedir}/src"></property>
<!-- 定义项目源码编译后的目录 -->
<property name="build.dir" value="${basedir}/classes"></property>
<!-- 定义项目最终编译生成的目录 -->
<property name="dist.dir" value="${basedir}/dist"></property>
<!-- 定义项目文档的目录 -->
<property name="doc.dir" value="${basedir}/doc"></property>
<!-- 定义项目文档api的目录 -->
<property name="doc.dir.api" value="${doc.dir}/api"></property>
<!-- 定义项目单元测试文档的目录 -->
<property name="junit.doc.dir" value="${basedir}/doc/api/junit"></property>
<!-- 定义项目单元测试源码的目录 -->
<property name="junit.src.dir" value="${basedir}/test"></property>
<!-- 定义项目单元测试编译的目录 -->
<property name="junit.dir" value="${basedir}/junit"></property>
<property name="junit.build.dir" value="${junit.dir}/classes"></property>
<!-- 定义项目单元测试测试报告的目录 -->
<property name="junit.report.dir" value="${junit.dir}/report"></property>
<target name="init" description="init">
<echo>项目初始化设置</echo>
<delete dir="${build.dir}"></delete>
<delete dir="${dist.dir}"></delete>
<delete dir="${doc.dir}"></delete>
<delete dir="${junit.dir}"></delete>
<mkdir dir="${build.dir}"/>
<mkdir dir="${dist.dir}"/>
<mkdir dir="${doc.dir}"/>
<mkdir dir="${junit.build.dir}"/>
<mkdir dir="${junit.report.dir}"/>
</target>
<target name="compile" depends="init" description="compile source">
<echo>编译源码</echo>
<javac srcdir="${src.dir}" includeantruntime="false" destdir="${build.dir}" verbose="true">
<classpath>
<fileset dir="${project.lib.dir}">
<include name="*.jar"/>
</fileset>
</classpath>
</javac>
</target>
<target name="api-doc" depends="compile" description="build javadoc">
<echo>生成api-doc帮助文档</echo>
<javadoc
destdir="${doc.dir.api}"
author="true"
version="true"
use="true"
windowtitle="${jar.prefix-name} API"
sourcepath="${src.dir}">
<doctitle><![CDATA[<h1>Test</h1>]]></doctitle>
<bottom><![CDATA[<i>Copyright © 2012 Dummy Corp. All Rights Reserved.</i>]]></bottom>
<classpath>
<fileset dir="${project.lib.dir}">
<include name="*.jar"/>
</fileset>
</classpath>
</javadoc>
</target>
<target name="junit-javac" depends="api-doc" description="junit test case compile">
<echo>编译junit</echo>
<javac destdir="${junit.build.dir}" srcdir="${junit.src.dir}" fork="false" includeantruntime="false">
<classpath>
<fileset dir="${project.lib.dir}">
<include name="*.jar"/>
</fileset>
</classpath>
</javac>
</target>
<target name="junit-run" depends="junit-javac" description="junit test case execute">
<echo>运行单元测试</echo>
<junit printsummary="yes" haltonfailure="false">
<classpath>
<pathelement location="${junit.build.dir}"/>
<fileset dir="${project.lib.dir}">
<include name="*.jar"/>
</fileset>
</classpath>
<formatter type="xml"/>
<batchtest todir="${junit.report.dir}">
<fileset dir="${junit.src.dir}">
<include name="**/*Test.java"/>
</fileset>
</batchtest>
</junit>
</target>
<target name="junit-report" depends="junit-run" description="junit test case report">
<echo>生成单元测试报告</echo>
<junitreport todir="${junit.report.dir}">
<fileset dir="${junit.report.dir}">
<include name="**/*.xml"/>
</fileset>
<report format="frames" todir="${junit.report.dir}/html"/>
</junitreport>
</target>
<target name="junit-doc" depends="junit-report" description="build javadoc">
<echo>生成junit-doc帮助文档</echo>
<javadoc
destdir="${junit.doc.dir}"
author="true"
version="true"
use="true"
windowtitle="Junit Test API"
sourcepath="${junit.src.dir}">
<classpath>
<fileset dir="${project.lib.dir}">
<include name="*.jar"/>
</fileset>
</classpath>
<doctitle><![CDATA[<h1>Test</h1>]]></doctitle>
<bottom><![CDATA[<i>Copyright © 2012 Dummy Corp. All Rights Reserved.</i>]]></bottom>
</javadoc>
</target>
<target name="package" depends="api-doc" description="package">
<echo>使用jar打包编译后的class文件</echo>
<jar destfile="${dist.dir}/${jar.prefix-name}-${jar.version}.jar" includes="**/*.class" basedir="${build.dir}">
</jar>
<echo>使用zip打包源码</echo>
<zip destfile="${dist.dir}/${jar.prefix-name}-source.zip" basedir="${src.dir}">
<exclude name="**/*.class"/>
</zip>
<echo>使用zip打包javadoc</echo>
<zip destfile="${dist.dir}/${jar.prefix-name}-doc.zip" basedir="${doc.dir.api}">
</zip>
<echo>使用war打war包</echo>
<war destfile="${dist.dir}/${jar.prefix-name}.war" webxml="${basedir}/WebRoot/WEB-INF/web.xml">
<fileset dir="${basedir}/WebRoot">
<include name="**/*.*"/>
</fileset>
<!--
<lib dir="${basedir}/WebRoot/WEB-INF/lib"></lib>
<classes dir="${basedir}/WebRoot/WEB-INF/classes"></classes>
-->
</war>
</target>
<target name="junit-package" depends="junit-doc" description="junit-package">
<echo>打包</echo>
<echo>使用jar打包编译后的class文件</echo>
<jar destfile="${dist.dir}/${jar.prefix-name}-${jar.version}.jar" includes="**/*.class" basedir="${build.dir}">
</jar>
<echo>使用zip打包源码</echo>
<zip destfile="${dist.dir}/${jar.prefix-name}-source.zip" basedir="${src.dir}">
<exclude name="**/*.class"/>
</zip>
<echo>使用zip打包javadoc</echo>
<zip destfile="${dist.dir}/${jar.prefix-name}-doc.zip" basedir="${doc.dir.api}">
</zip>
<echo>使用war打war包</echo>
<war destfile="${dist.dir}/${jar.prefix-name}.war" webxml="${basedir}/WebRoot/WEB-INF/web.xml">
<fileset dir="${basedir}/WebRoot">
<include name="**/*.*"/>
</fileset>
<!--
<lib dir="${basedir}/WebRoot/WEB-INF/lib"></lib>
<classes dir="${basedir}/WebRoot/WEB-INF/classes"></classes>
-->
</war>
<echo>使用jar打junit单元测试编译好的class文件</echo>
<jar destfile="${dist.dir}/${jar.prefix-name}-junit-${jar.version}.jar" includes="**/*.class" basedir="${junit.build.dir}">
</jar>
<echo>使用zip打junit单元测试源文件</echo>
<zip destfile="${dist.dir}/${jar.prefix-name}-junit-source.zip" basedir="${junit.src.dir}">
<exclude name="**/*.class"/>
</zip>
<echo>使用zip打junit单元测试测试报告</echo>
<zip destfile="${dist.dir}/junit-report.zip" basedir="${junit.report.dir}/html">
</zip>
</target>
<target name="clean">
<echo>清理项目</echo>
<delete dir="${build.dir}" includeemptydirs="true"></delete>
<delete dir="${doc.dir}" includeemptydirs="true"></delete>
<delete dir="${junit.dir}" includeemptydirs="true"></delete>
</target>
<target name="mail" description="use java mail send mail message" depends="clean">
<echo>将打包的文件email发送</echo>
<mail
mailhost="${mailhost}"
mailport="25"
subject="${mailsubject}"
charset="utf-8"
user="${username}"
password="${password}"
>
<from address="${mailfrom}"/>
<to address="${mailto}"/>
<message>使用Ant自助构建项目测试打包文档</message>
<attachments>
<fileset dir="${dist.dir}">
<include name="*.*"/>
</fileset>
</attachments>
</mail>
</target>
<target name="wget" depends="mail">
<echo>下载tomcat</echo>
<get dest="${tomcat.dir}" verbose="true">
<url url="${tomcat.download.url}"/>
</get>
</target>
<target name="unzip" depends="wget">
<echo>解压tomcat</echo>
<unzip src="${tomcat.dir}/apache-tomcat-${tomcat.version}.zip" dest="${tomcat.dir}">
</unzip>
</target>
<target name="copy" depends="unzip">
<echo>拷贝war包到tomcat中部署</echo>
<copy file="${dist.dir}/${jar.prefix-name}.war" tofile="${tomcat.dir}/apache-tomcat-${tomcat.version}/webapps/${jar.prefix-name}.war"/>
</target>
<target name="startup" depends="copy">
<echo>启动tomcat web容器</echo>
<exec executable="${tomcat.dir}/apache-tomcat-${tomcat.version}/bin/startup.bat">
<env key="CATALINA_HOME" path="${tomcat.dir}/apache-tomcat-${tomcat.version}/"/>
</exec>
</target>
<target name="run" depends="package,startup" description="不存在单元测试时执行的任务">
</target>
<target name="junitrun" depends="junit-package,startup" description="存在单元测试时执行的任务">
</target>
</project>
构建控制台日志
init:
[echo] 项目初始化设置
[delete] Deleting directory D:\workspace\Ant1\classes
[delete] Deleting directory D:\workspace\Ant1\dist
[delete] Deleting directory D:\workspace\Ant1\doc
[delete] Deleting directory D:\workspace\Ant1\junit
[mkdir] Created dir: D:\workspace\Ant1\classes
[mkdir] Created dir: D:\workspace\Ant1\dist
[mkdir] Created dir: D:\workspace\Ant1\doc
[mkdir] Created dir: D:\workspace\Ant1\junit\classes
[mkdir] Created dir: D:\workspace\Ant1\junit\report
compile:
[echo] 编译源码
[javac] Compiling 3 source files to D:\workspace\Ant1\classes
junit-javac:
[echo] 编译junit
[javac] Compiling 1 source file to D:\workspace\Ant1\junit\classes
junit-run:
[echo] 运行单元测试
[junit] Running com.junit.test.OtherTest
[junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 0.027 sec
junit-report:
[echo] 生成单元测试报告
[junitreport] Processing D:\workspace\Ant1\junit\report\TESTS-TestSuites.xml to C:\Users\scott\AppData\Local\Temp\null1056173236
[junitreport] Loading stylesheet jar:file:/E:/apache-ant-1.8.4/lib/ant-junit.jar!/org/apache/tools/ant/taskdefs/optional/junit/xsl/junit-frames.xsl
[junitreport] Transform time: 334ms
[junitreport] Deleting: C:\Users\scott\AppData\Local\Temp\null1056173236
doc:
[echo] 生成帮助文档
[javadoc] Generating Javadoc
[javadoc] Javadoc execution
[javadoc] 正在创建目标目录: "D:\workspace\Ant1\doc\api\"
[javadoc] 正在装入软件包 com.ant.lesson1 的源文件...
[javadoc] 正在构造 Javadoc 信息...
[javadoc] 标准 Doclet 版本 1.6.0_25
[javadoc] 正在构建所有软件包和类的树...
[javadoc] 正在生成 D:\workspace\Ant1\doc\api\com/ant/lesson1/\Main.html...
[javadoc] 正在生成 D:\workspace\Ant1\doc\api\com/ant/lesson1/\MyHttpServlet.html...
[javadoc] 正在生成 D:\workspace\Ant1\doc\api\com/ant/lesson1/\SimpleTask.html...
[javadoc] 正在生成 D:\workspace\Ant1\doc\api\com/ant/lesson1/\package-frame.html...
[javadoc] 正在生成 D:\workspace\Ant1\doc\api\com/ant/lesson1/\package-summary.html...
[javadoc] 正在生成 D:\workspace\Ant1\doc\api\com/ant/lesson1/\package-tree.html...
[javadoc] 正在生成 D:\workspace\Ant1\doc\api\constant-values.html...
[javadoc] 正在生成 D:\workspace\Ant1\doc\api\serialized-form.html...
[javadoc] 正在生成 D:\workspace\Ant1\doc\api\com/ant/lesson1/\class-use\SimpleTask.html...
[javadoc] 正在生成 D:\workspace\Ant1\doc\api\com/ant/lesson1/\class-use\MyHttpServlet.html...
[javadoc] 正在生成 D:\workspace\Ant1\doc\api\com/ant/lesson1/\class-use\Main.html...
[javadoc] 正在生成 D:\workspace\Ant1\doc\api\com/ant/lesson1/\package-use.html...
[javadoc] 正在构建所有软件包和类的索引...
[javadoc] 正在生成 D:\workspace\Ant1\doc\api\overview-tree.html...
[javadoc] 正在生成 D:\workspace\Ant1\doc\api\index-all.html...
[javadoc] 正在生成 D:\workspace\Ant1\doc\api\deprecated-list.html...
[javadoc] 正在构建所有类的索引...
[javadoc] 正在生成 D:\workspace\Ant1\doc\api\allclasses-frame.html...
[javadoc] 正在生成 D:\workspace\Ant1\doc\api\allclasses-noframe.html...
[javadoc] 正在生成 D:\workspace\Ant1\doc\api\index.html...
[javadoc] 正在生成 D:\workspace\Ant1\doc\api\help-doc.html...
[javadoc] 正在生成 D:\workspace\Ant1\doc\api\stylesheet.css...
[javadoc] Generating Javadoc
[javadoc] Javadoc execution
[javadoc] 正在创建目标目录: "D:\workspace\Ant1\doc\api\junit\"
[javadoc] 正在装入软件包 com.junit.test 的源文件...
[javadoc] 正在构造 Javadoc 信息...
[javadoc] 标准 Doclet 版本 1.6.0_25
[javadoc] 正在构建所有软件包和类的树...
[javadoc] 正在生成 D:\workspace\Ant1\doc\api\junit\com/junit/test/\OtherTest.html...
[javadoc] 正在生成 D:\workspace\Ant1\doc\api\junit\com/junit/test/\package-frame.html...
[javadoc] 正在生成 D:\workspace\Ant1\doc\api\junit\com/junit/test/\package-summary.html...
[javadoc] 正在生成 D:\workspace\Ant1\doc\api\junit\com/junit/test/\package-tree.html...
[javadoc] 正在生成 D:\workspace\Ant1\doc\api\junit\constant-values.html...
[javadoc] 正在生成 D:\workspace\Ant1\doc\api\junit\com/junit/test/\class-use\OtherTest.html...
[javadoc] 正在生成 D:\workspace\Ant1\doc\api\junit\com/junit/test/\package-use.html...
[javadoc] 正在构建所有软件包和类的索引...
[javadoc] 正在生成 D:\workspace\Ant1\doc\api\junit\overview-tree.html...
[javadoc] 正在生成 D:\workspace\Ant1\doc\api\junit\index-all.html...
[javadoc] 正在生成 D:\workspace\Ant1\doc\api\junit\deprecated-list.html...
[javadoc] 正在构建所有类的索引...
[javadoc] 正在生成 D:\workspace\Ant1\doc\api\junit\allclasses-frame.html...
[javadoc] 正在生成 D:\workspace\Ant1\doc\api\junit\allclasses-noframe.html...
[javadoc] 正在生成 D:\workspace\Ant1\doc\api\junit\index.html...
[javadoc] 正在生成 D:\workspace\Ant1\doc\api\junit\help-doc.html...
[javadoc] 正在生成 D:\workspace\Ant1\doc\api\junit\stylesheet.css...
package:
[echo] 打包
[jar] Building jar: D:\workspace\Ant1\dist\cms-1.0.jar
[zip] Building zip: D:\workspace\Ant1\dist\cms-source.zip
[zip] Building zip: D:\workspace\Ant1\dist\cms-doc.zip
[jar] Building jar: D:\workspace\Ant1\dist\cms-junit-1.0.jar
[zip] Building zip: D:\workspace\Ant1\dist\cms-junit-source.zip
[zip] Building zip: D:\workspace\Ant1\dist\junit-report.zip
[war] Building war: D:\workspace\Ant1\dist\cms.war
clean:
[echo] 清理项目
[delete] Deleting directory D:\workspace\Ant1\classes
[delete] Deleting directory D:\workspace\Ant1\doc
[delete] Deleting directory D:\workspace\Ant1\junit
mail:
[echo] 将打包的文件email发送
[mail] Sending email: Test build
最终编译后的目录为