<?xml version="1.0" encoding="GBK" ?> <project name="demo" default="usage" xmlns:catalina="antlib:org.apache.catalina.ant"> <!--属性配置--> <!--目录结构--> <property name="src.dir" location="src" /> <property name="tests.dir" location="test" /> <property name="web.dir" location="web" /> <property name="build.dir" location="build" /> <property name="dist.dir" location="dist" /> <property name="lib" location="lib" /> <property name="build.classes.dir" location="${build.dir}/classes" /> <property name="test.classes.dir" location="${build.dir}/test-classes" /> <property name="reports.dir" location="reports" /> <property name="reports.javadoc" location="${reports.dir}/javadoc" /> <property name="reports.data.dir" location="${reports.dir}/xml" /> <property name="reports.html.dir" location="${reports.dir}/html" /> <!--项目编译信息--> <property name="project.name" value="${ant.project.name}" /> <property name="project.version" value="1.0" /> <property name="debug" value="on" /><!--on/off--> <property name="src.encoding" value="UTF-8" /> <property name="source.version" value="1.6" /> <property name="target.version" value="1.6" /> <!--Tomcat信息--> <property name="tomcat.install.dir" location="C:/Program Files/Apache Software Foundation/Tomcat 6.0" /> <property name="tomcat.manager.url" value="http://localhost:8080/manager" /> <property name="tomcat.manager.username" value="admin" /> <property name="tomcat.manager.password" value="admin" /> <property name="context-path" value="/${project.name}" /> <!--classpath配置--> <path id="compile.classpath" > <pathelement location="${build.classes.dir}" /> <fileset dir="${lib}" includes="*.jar" /> <fileset dir="${tomcat.install.dir}/lib" includes="*.jar" /> </path> <path id="test.compile.classpath" > <path refid="compile.classpath" /> <pathelement location="${build.classes.dir}" /> </path> <path id="test.classpath" > <path refid="test.compile.classpath" /> <pathelement path="${test.classes.dir}" /> </path> <!--使用说明--> <target name="usage"> <echo message="${project.name} - ${project.version}"/> <echo message="" /> <echo message="create -- 创建项目目录结构" /> <echo message="compile -- 编译项目" /> <echo message="compile-tests -- 编译单元测试" /> <echo message="test -- 运行单元测试" /> <echo message="test.report -- 运行单元测试并生成测试报告" /> <echo message="javadoc -- 生成JavaDoc" /> <echo message="jar -- 打包所有class为jar" /> <echo message="war -- 打包web项目" /> <echo message="tomcat.start -- 启动Tomcat" /> <echo message="tomcat.stop -- 关闭Tomcat" /> <echo message="tomcat.start -- 启动Tomcat到Debug模式" /> <echo message="deploy -- 部署到Tomcat下" /> <echo message="undeploy -- 取消部署到Tomcat下" /> <echo message="clean -- 清理dist,build" /> <echo message="clean-all -- 清理dist,build,repots" /> </target> <!--创建项目目录结构--> <target name="create" > <mkdir dir="${src.dir}" /> <mkdir dir="${tests.dir}" /> <mkdir dir="${lib}" /> <mkdir dir="${web.dir}" /> </target> <!--初始化--> <target name="init" > <mkdir dir="${build.classes.dir}" /> <mkdir dir="${test.classes.dir}" /> <mkdir dir="${dist.dir}" /> <mkdir dir="${reports.data.dir}" /> <mkdir dir="${reports.html.dir}" /> <mkdir dir="${reports.javadoc}" /> </target> <!--编译--> <target name="compile" depends="init" description="Compile Java code" > <javac srcdir="${src.dir}" destdir="${build.classes.dir}" classpathref="compile.classpath" encoding="${src.encoding}" debug="${debug}" source="${source.version}" target="${target.version}"/> <copy todir="${build.classes.dir}"> <fileset dir="${src.dir}"> <exclude name="**/*.java"/> </fileset> </copy> </target> <!--编译测试--> <target name="compile-tests" depends="compile" description="Compile Unit Tests" > <javac srcdir="${tests.dir}" destdir="${test.classes.dir}" encoding="${src.encoding}" debug="${debug}"> <classpath refid="test.compile.classpath" /> </javac> <copy todir="${test.classes.dir}"> <fileset dir="${tests.dir}"> <exclude name="**/*.java"/> </fileset> </copy> </target> <!--运行单元测试--> <target name="test" depends="compile-tests" description="Run unit tests" > <junit printsummary="true" haltonfailure="false" failureproperty="test.failures" fork="true" > <classpath refid="test.classpath" /> <formatter type="xml" /> <formatter type="plain" /> <batchtest todir="${reports.data.dir}" > <!--reports保存的目录--> <fileset dir="${test.classes.dir}" includes="**/*Test.class" /> </batchtest> </junit> </target> <!--单元测试报告--> <target name="test.report" depends="test" description="Generate HTML unit test reports" > <junitreport todir="${reports.data.dir}" > <fileset dir="${reports.data.dir}" > <include name="TEST-*.xml" /> </fileset> <report format="noframes" todir="${reports.html.dir}" /> </junitreport> <fail if="test.failures" message="There were test failures." /> </target> <!--生成文档--> <target name="javadoc" depends="compile,init" description="Generate JavaDocs." > <javadoc sourcepath="${src.dir}" destdir="${reports.javadoc}" author="true" version ="true" use="true" access="private" linksource="true" windowtitle="${ant.project.name} API" source="${source.version}" Encoding="${src.encoding}" charset="${src.encoding}"> <classpath> <path refid="compile.classpath" /> <pathelement path="${build.classes.dir}" /> </classpath> <doctitle> <![CDATA[<h1>${ant.project.name}</h1>]]> </doctitle> <bottom> <![CDATA[<i>Copyright & # 169; 2007 All Rights Reserved.</i>]]> </bottom> </javadoc> </target> <!--打包jar,向MANIFEST. MF添加一些关于项目的信息,如时间,作者,版本--> <target name="jar" depends="compile" description="Generate JAR file" > <tstamp> <format property="build.date" pattern="EEEE, d MMMM yyyy" /> <format property="build.time" pattern="hh:mm a" /> </tstamp> <jar destfile="${dist.dir}/${project.name}-${project.version}.jar" basedir="${build.classes.dir}" > <manifest> <attribute name="Built-By" value="${user.name}" /> <attribute name="Specification-Title" value="${project.name}" /> <attribute name="Specification-Version" value="${project.version}" /> <attribute name="Specification-Vendor" value="ACME Incorporated" /> <attribute name="Implementation-Title" value="common" /> <attribute name="Implementation-Version" value="${project.version} - built at ${build.time} on ${build.date} " /> <attribute name="Implementation-Vendor" value="ACME Incorporated" /> </manifest> </jar> </target> <!--打包war--> <target name="war" depends="compile" description="Generate WAR file" > <war destfile="${dist.dir}/${project.name}.war" webxml="${web.dir}/WEB-INF/web.xml" > <fileset dir="${web.dir}" /> <classes dir="${build.classes.dir}" /> <lib dir="${lib}" > <include name="*.jar" /> </lib> <manifest> <attribute name="Built-By" value="${user.name}" /> <attribute name="Specification-Title" value="${project.name}" /> <attribute name="Specification-Version" value="${project.version}" /> <attribute name="Specification-Vendor" value="ACME Incorporated" /> <attribute name="Implementation-Title" value="common" /> <attribute name="Implementation-Version" value="${project.version} - built at ${build.time} on ${build.date} " /> <attribute name="Implementation-Vendor" value="ACME Incorporated" /> </manifest> </war> </target> <!--启动tomcat--> <target name="tomcat.start"> <java jar="${tomcat.install.dir}/bin/bootstrap.jar" fork="true" spawn="true"> <jvmarg value="-Dcatalina.home=${tomcat.install.dir}"/> </java> </target> <!--关闭tomcat--> <target name="tomcat.stop"> <java jar="${tomcat.install.dir}/bin/bootstrap.jar" fork="true" spawn="true"> <jvmarg value="-Dcatalina.home=${tomcat.install.dir}"/> <arg line="stop"/> </java> </target> <!--启动tomcat到debug模式--> <target name="tomcat.debug"> <java jar="${tomcat.install.dir}/bin/bootstrap.jar" fork="true" spawn="true"> <jvmarg value="-Dcatalina.home=${tomcat.install.dir}"/> <jvmarg value="-Xdebug"/> <jvmarg value="-Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n"/> </java> </target> <!--部署到tomcat,tomcat需要启动--> <target name="deploy" depends="war" description="Deploy to Tomcat instance" > <catalina:deploy url="${tomcat.manager.url}" username="${tomcat.manager.username}" password="${tomcat.manager.password}" path="${context-path}" localWar="file:${dist.dir}/${project.name}.war" /> </target> <!--取消部署到tomcat,tomcat需要启动--> <target name="undeploy" description="Remove application in Tomcat"> <catalina:undeploy url="${tomcat.manager.url}" username="${tomcat.manager.username}" password="${tomcat.manager.password}" path="${context-path}" /> </target> <!--reload,tomcat需要启动--> <target name="reload" description="Reload application in Tomcat"> <catalina:reload url="${tomcat.manager.url}" username="${tomcat.manager.username}" password="${tomcat.manager.password}" path="${context-path}" /> </target> <!--Clean--> <target name="clean"> <delete dir="${dist.dir}"/> <delete dir="${build.dir}"/> </target> <!--Clean All--> <target name="clean-all"> <delete dir="${dist.dir}"/> <delete dir="${build.dir}"/> <delete dir="${reports.dir}"/> </target> </project>