Skip to content

Instantly share code, notes, and snippets.

@dylanninin
Last active December 28, 2015 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dylanninin/7425918 to your computer and use it in GitHub Desktop.
Save dylanninin/7425918 to your computer and use it in GitHub Desktop.
Ant build.xml for Java Web Application. Be free to use it as a template and modify at will. Reference http://ant.apache.org/
<?xml version="1.0" encoding="utf-8"?>
<!-- build.xml -->
<!-- 2013-11-12 dylanninin@gmail.com init -->
<project name="SSH" default="dist.deploy" basedir=".">
<!-- properties for packages and source -->
<property name="package.top" value="com/egolife/ssh" description="SSH项目顶层包结构" />
<property name="src.dir" value="src" description="src文件夹" />
<!-- properties for resource-->
<property name="resource.dir" value="resource" />
<property name="resource.ftl.dir" value="${resource.dir}/ftl" />
<property name="resource.icons.dir" value="${resource.dir}/icons" />
<property name="resource.jpdl.dir" value="${resource.dir}/jpdl" />
<property name="resource.spring.dir" value="${resource.dir}/spring" />
<property name="resource.struts.dir" value="${struts}/jpdl" />
<!-- properties for reports-->
<property name="reports.dir" value="reports" description="报告文件夹" />
<property name="reports.junit.dir" value="${reports.dir}/junit" />
<!-- properties for doc-->
<property name="doc.dir" value="doc" description="文档文件夹" />
<property name="doc.api.dir" value="${doc.dir}/api" />
<!-- properties for webapp and classes -->
<property name="webapp.dir" value="WebRoot" />
<property name="lib.dir" value="${webapp.dir}/WEB-INF/lib" />
<property name="classes.dir" value="${webapp.dir}/WEB-INF/classes" />
<property name="compressed.dir" value="${webapp.dir}/dist" />
<!-- properties for test-->
<property name="test.src.dir" value="test" description="test文件夹" />
<property name="test.lib.dir" value="lib" description="测试需要的jar库" />
<property name="test.classes.dir" value="${classes.dir}" />
<!-- properties for hbm -->
<property name="hbm.src.dir" value="${src.dir}/${package.top}/hbm" />
<property name="hbm.dist.dir" value="${classes.dir}/${package.top}/hbm" />
<property name="dist.dir" value="dist" />
<!-- properties for tomcat home-->
<property file="build.properties" description="CATALINA_HOME Properties"/>
<property name="tomcat.home" location="${catalina_home}" />
<property name="tomcat.conf" location="${tomcat.home}/conf" />
<property name="tomcat.conf.localhost" location="${tomcat.conf}/Catalina/localhost" />
<property name="tomcat.webapps.dir" location="${tomcat.home}/webapps" />
<property name="dir.lib.yuicompress" value="lib"/><!-- 存放 YUI Compress 二个 .jar 文件的目录 -->
<property name="dir.build.js" value="${compressed.dir}/js"/><!-- 存放压缩过的 JavaScript 文件目录 -->
<property name="dir.build.css" value="${compressed.dir}/css"/><!-- 存放压缩过的 CSS 文件目录 -->
<property name="dir.src.js" value="WebRoot/js"/><!-- JavaScript 源文件目录 -->
<property name="dir.src.css" value="WebRoot/css"/><!-- CSS 源文件目录 -->
<path id="path.build.classpath.yuicompress" description="js压缩需要jar路径">
<fileset dir="${dir.lib.yuicompress}">
<include name="yuicompressor-2.4.7.jar"/>
<include name="YUIAnt-zh_CN.jar"/>
</fileset>
</path>
<path id="classpath" description="项目发布运行时搜索路径">
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
</path>
<path id="test.classpath" description="测试时需要额外添加的搜索路径">
<fileset dir="${test.lib.dir}">
<include name="**/*.jar" />
</fileset>
</path>
<target name="demo" description="展示">
<echo>do nothing....</echo>
</target>
<!-- targets for initialization-->
<target name="init" description="初始化" depends="clean.all,copy.hbm,copy.resource">
</target>
<target name="copy.hbm" description="复制HBM文件到classes目录下">
<mkdir dir="${hbm.dist.dir}" taskname="make *.hbm.xml dist dir" />
<copy todir="${hbm.dist.dir}" taskname="copy *.hbm.xml from src to classes" overwrite="true">
<fileset dir="${hbm.src.dir}">
<include name="**/*.hbm.xml" />
</fileset>
</copy>
</target>
<target name="copy.resource" depends="jpdl.deploy" description="复制resource资源到classes目录下">
<copy todir="${classes.dir}" taskname="copy *.properties or *.xml from resource to classes" overwrite="true">
<fileset dir="${resource.dir}">
<exclude name="jpdl/**/*" />
</fileset>
</copy>
</target>
<!-- targets for clean-->
<target name="clean.resource" description="清空resource">
<delete includeemptydirs="true" taskname="delete resource like *.properties or *.xml except com/**/* from classes">
<fileset dir="${classes.dir}">
<exclude name="com/**/*" />
</fileset>
</delete>
</target>
<target name="clean.classes" description="仅清空编译后的类">
<delete includeemptydirs="true" taskname="delete com/**/* from classes">
<fileset dir="${classes.dir}/com" />
</delete>
</target>
<target name="clean.all" description="清空classes目录">
<delete includeemptydirs="true" taskname="cleanup classes folder">
<fileset dir="${classes.dir}" includes="**/*" />
</delete>
</target>
<target name="clean.compressed" description=" 清空压缩过的资源">
<delete includeemptydirs="true" taskname="cleanup compressed js folder">
<fileset dir="${compressed.dir}/js" includes="**/*" />
</delete>
<delete includeemptydirs="true" taskname="cleanup compressed css folder">
<fileset dir="${compressed.dir}/css" includes="**/*" />
</delete>
</target>
<target name="compile" description="仅编译核心类" depends="init">
<javac srcdir="${src.dir}" destdir="${classes.dir}" encoding="utf-8" taskname="compile java src code">
<compilerarg line="-encoding UTF-8" />
<classpath>
<path refid="classpath" />
<path refid="test.classpath" />
</classpath>
</javac>
</target>
<!-- targets for test-->
<target name="-test.demo">
<copy todir="${test.classes.dir}" taskname="copy *.hbm.xml from test to classes" overwrite="true">
<fileset dir="${test.src.dir}">
<include name="**/*.hbm.xml" />
</fileset>
</copy>
</target>
<target name="test.compile" depends="compile,-test.demo" description="编译测试类">
<javac srcdir="${test.src.dir}" destdir="${test.classes.dir}" encoding="utf-8" taskname="compile java test code">
<compilerarg line="-encoding UTF-8" />
<classpath>
<pathelement location="${classes.dir}" />
<path refid="classpath" />
<path refid="test.classpath" />
</classpath>
</javac>
</target>
<target name="test.clean.classes" description="仅清空编译后的测试类">
<delete includeemptydirs="true" taskname="delete com.egolife.ssh.test/**/* from classes" failonerror="false">
<fileset dir="${classes.dir}/${package.top}/test" />
</delete>
</target>
<target name="test.clean.reports" description="清空测试报告">
<delete includeemptydirs="true" taskname="delete junit from reports" failonerror="false">
<fileset dir="${reports.junit.dir}" />
</delete>
</target>
<target name="test.run" description="自动运行单元测试" depends="test.clean.reports,test.clean.classes,test.compile">
<mkdir dir="${reports.junit.dir}" taskname="make junit reports dir" />
<property name="testcase" value="*Test" />
<!--printsummary="yes" : 在测试的最后一行生成一个单行的概要-->
<!--haltonerror="yes" haltonfailure="yes" :如果失败或产生错误将停止编译-->
<!--fork="yes" :每个测试分别使用一个单独的java虚拟机(JUM)-->
<junit printsummary="yes" haltonerror="no" haltonfailure="no" fork="no" taskname="run batch junit autotest">
<formatter type="plain" usefile="false" />
<formatter type="xml" />
<!--自动找出要运行的测试-->
<batchtest todir="${reports.junit.dir}">
<fileset dir="${test.classes.dir}">
<include name="${package.top}/**/${testcase}.class" />
</fileset>
</batchtest>
<!-- 添加classpath -->
<classpath>
<pathelement location="${classes.dir}" />
<path refid="classpath" />
<path refid="test.classpath" />
</classpath>
</junit>
</target>
<target name="test.report" depends="test.run" description="生成单元测试报告">
<mkdir dir="${reports.junit.dir}/html" taskname="make junit html dir" />
<junitreport todir="${reports.junit.dir}">
<fileset dir="${reports.junit.dir}">
<include name="TEST-*.xml" />
</fileset>
<report todir="${reports.junit.dir}/html" />
</junitreport >
</target>
<!--TODO javac and junit-->
<target name="javadoc" description="生成javadoc">
<mkdir dir="${doc.api.dir}" taskname="make javadoc api dir" />
<javadoc packagenames="${package.top}/*" sourcepath="${src.dir}" destdir="${doc.dir}/api" encoding="utf-8" charset="utf-8" author="dylanninin" version="true" use="true">
<classpath refid="classpath" />
</javadoc>
</target>
<target name="javadoc.clean" description="清空javadoc">
<delete includeemptydirs="true" taskname="cleanup javadoc api folder">
<fileset dir="${doc.api.dir}"/>
</delete>
</target>
<!-- targets about jpdl-->
<target name="jpdl.clean.zip" description="清空jpdl zip文件">
<delete includeemptydirs="false" taskname="delete **/*.zip from resource" failonerror="false">
<fileset dir="${resource.jpdl.dir}">
<include name="**/*.zip" />
</fileset>
</delete>
</target>
<target name="jpdl.clean.deploy" description="清空已发布的jpdl zip文件">
<delete includeemptydirs="false" taskname="delete **/*.zip from classes/jpdl" failonerror="false" dir="${classes.dir}/jpdl">
<fileset includes="**/*.zip" />
</delete>
</target>
<target name="jpdl.deploy" description="发布jdpl/**/*.zip文件到classes目录">
<property name="jpdl" value="*" />
<copy todir="${classes.dir}" overwrite="true">
<fileset dir="${resource.dir}">
<include name="jpdl/**/${jpdl}.zip" />
</fileset>
</copy>
</target>
<target name="jpdl.zip" description="打包jpdl文件">
<property name="jpdl" value="" description="待打包的jpdl" />
<zip destfile="${resource.jpdl.dir}/${jpdl}.zip" update="true">
<fileset dir="${resource.jpdl.dir}">
<include name="${jpdl}.jpdl.xml" />
<include name="${jpdl}.png" />
<exclude name="**/*.zip" />
</fileset>
</zip>
</target>
<!-- targets for compress js ,css etc.-->
<target name="dist.js.compress" depends="clean.compressed" description="压缩 .js 和 .css 文件">
<taskdef name="compress" classname="com.yahoo.platform.yui.compressor.YUICompressTask">
<classpath refid="path.build.classpath.yuicompress"/>
</taskdef>
<compress linebreak="150" warn="false" munge="yes" charset="UTF-8"
preserveallsemicolons="true" outputfolder="${dir.build.js}">
<fileset dir="${dir.src.js}">
<include name="**/*.js"/>
</fileset>
</compress>
<compress linebreak="150" warn="false" munge="yes" charset="UTF-8"
preserveallsemicolons="true" outputfolder="${dir.build.css}">
<fileset dir="${dir.src.css}">
<include name="**/*.css"/>
</fileset>
</compress>
</target>
<!-- targets for dist and deploy-->
<target name="dist.clean.war" description="清除SSH.war文件">
<delete file="${dist.dir}/${ant.project.name}.war" failonerror="false" taskname="delete SSH.war in dist dir" />
</target>
<target name="dist.clean.deploy" description="清除已发布的SSH.war文件">
<delete file="${tomcat.webapps.dir}/${ant.project.name}.war" taskname="delete ssh.war in tomcat webapps dir" failonerror="false" />
<delete includeemptydirs="true" failonerror="false" taskname="delete SSH from tomcat webapps dir">
<fileset dir="${tomcat.webapps.dir}/${ant.project.name}" />
</delete>
</target>
<target name="dist.clean.context" description="删除Tomcat Conf下的SSH.xml">
<delete file="${tomcat.conf.localhost}/${ant.project.name}.xml" failonerror="false" taskname="delete SSH.xml from tomcat conf" />
</target>
<target name="dist.context" description="发布context.xml到Tomcat Conf目录下" depends="dist.clean.deploy">
<copy tofile="${tomcat.conf.localhost}/${ant.project.name}.xml" file="${webapp.dir}\META-INF\context.xml" overwrite="true" taskname="move context.xml to tomcat conf" />
</target>
<target name="dist.war" description="打包项目" depends="dist.clean.war,clean.all,compile">
<war destfile="${dist.dir}/${ant.project.name}.war" webxml="${webapp.dir}/WEB-INF/web.xml" taskname="war WebRoot to war package" update="true">
<fileset dir="${webapp.dir}">
<exclude name="META-INF/context.xml" />
</fileset>
</war>
</target>
<target name="dist.deploy" description="发布项目到Tomcat webapps目录下" depends="dist.clean.deploy,dist.clean.context,dist.war">
<copy todir="${tomcat.webapps.dir}" file="${dist.dir}/${ant.project.name}.war" taskname="deploy ssh.war to tomcat webapps dir">
</copy>
</target>
<!-- TODO startup and shutdown-->
<!-- TODO use apply instead of exec-->
<target name="tomcat.startup" description="启动tomcat">
<exec command="cmd" taskname="startup tomcat">
<arg value="/c" />
<arg value="${tomcat.home}/bin/startup.bat" />
</exec>
</target>
<target name="tomcat.shutdown" description="关闭tomcat">
<exec command="cmd" taskname="shutdown tomcat">
<arg value="/c" />
<arg value="${tomcat.home}/bin/shutdown.bat" />
</exec>
</target>
<!-- targets for help system-->
<target name="help.jpdl.deploy" description="jpdl.deploy使用帮助">
<echo message="Name:jpdl.deploy"/>
<echo message="Synopsis:ant [-Djpdl=path/name] jpdl.deploy"/>
<echo message="Description:jpdl is optional. jpdl specifies the jpdl zip
package based on resource/jpdl folder to deploy. And if jpdl dose not specified
all jpdl zip packages will be deployed."/>
<echo message="Example:1)ant -Djpdl=demo/leave jpdl.deploy" />
<echo message=" :just deploy resource/jpdl/demo/leave.zip to
${classes.dir}/jpdl/demo/leave.zip" />
<echo message=" :2)ant jpdl.deploy" />
<echo message=" :deploy all zip packages in resource/jpdl recursively to
${classes.dir}/jpdl" />
</target>
<target name="help.jpdl.zip" description="jpdl.zip使用帮助">
<echo message="Name:jpdl.zip"/>
<echo message="Synopsis:ant [-Djpdl=path/name] jpdl.zip"/>
<echo message="Description:jpdl is required. jpdl specifies the jpdl
resources based on resource/jpdl to package and compress with zip."/>
<echo message="Example:ant -Djpdl=demo/leave jpdl.zip" />
<echo message=" :just package resource/jpdl/demo/leave.jpdl and
resource/jpdl/demo/leave.png to resource/jpdl/demo/leave.zip" />
</target>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment