Ant模板
从构建,测试到发布整个流程
Demo
<?xml version="1.0" ?>
<project name="core-diary" default="default">
<description>
This is build file compiles,tests,packages and
distributes the core library of the diary application.
</description>
<target name="default" depends="dist"
description="package everything and checksum it" />
<target name="dist" depends="package-everything,checksum"
description="create the distributables" />
<target name="publish" depends="email-announcement-html"
description="publish the files">
<antcall target="scp">
<param name="server" value="" />
</antcall>
<antcall target="scp">
<param name="server" value="" />
</antcall>
<antcall target="ftp-upload">
<param name="server" value="" />
</antcall>
</target>
<target name="test" depends="unit-tests"
description="compile and run the tests" />
<target name="clean"
description="Delete all generated files">
<delete dir="${build.dir}" />
<delete dir="${dist.dir}" />
<delete dir="${test.data.dir}" />
<delete dir="${test.reports.dir}" />
</target>
<!--Path of property file-->
<property file="build.properties" />
<!--Set Path of build-->
<property name="build.dir" location="build" />
<property name="build.classes.dir" location="${build.dir}/classes" />
<property name="test.dir" location="$(build.dir)/test" />
<property name="test.classes.dir" location="${test.dir}/classes" />
<property name="test.data.dir" location="${test.dir}/data" />
<property name="test.reports.dir" location="${test.dir}/reports" />
<!--Set Path of dist-->
<property name="dist.dir" location="dist" />
<property name="dist.doc.dir" location="${dist.dir}/doc" />
<property name="javadoc.dir" location="${dist.dir}/api" />
<property name="project.name" value="diary-core" />
<property name="project.version" value="0.1alpha" />
<property name="project.name-ver" value="${project.name}-${project.version}" />
<property name="target.jar" location="${dist.dir}/${target.name}" />
<property name="target.zip" loaction="${dist.dir}/${project.name-ver}.zip" />
<property name="src.zip" location="${dist.dir}/${project.name-ver}-src.zip" />
<property name="unjar.dir" location="${build.dir}/unjar" />
<property name="build.debug" value="true" />
<!--junit fork-->
<property name="junit.fork" value="true" />
<property name="junit.forkmode" value="once" />
<property name="test.suite" value="dl.core.test.AllTests" />
<property name="source.files.tocopy" value="**/*.properties,**/*.dtd,**/*.xml,**/*.xsd,**/*.jpg" />
<property name="bin.dir" location="bin" />
<property name="unix.scripts" value="**/*.sh,**/*.pl,**/*.py" />
<property name="dos.scripts" value="**/*.bat,**/*.cmd" />
<property name="unzip.dir" location="${build.dir}/unzip" />
<property name="unzip.bin.dir" location="${unzip.dir}/bin" />
<property name="unzip.src.dir" location="${unzip.dir}/src" />
<property name="untar.dir" location="${build.dir}/untar" />
<property name="unjar.dir" location="${build.dir}/unjar" />
<property name="main.class" value="dl.core.Diagnostics" />
<path id="compile.classpath">
<fileset dir="lib">
<include name="*.jar" />
</fileset>
</path>
<path id="test.compile.classpath">
<path refid="compile.classpath"/>
<pathelement location="${target.jar}" />
</path>
<path id="test.classpath">
<path refid="test.compile.classpath" />
<pathelement location="${test.classes.dir}" />
</path>
<patternset id="failig.tests">
<exclude unless="run.failing.tests"
name="d1/core/test/JavaAssertTest.*" />
</patternset>
<fileset id="ftp.upload.fileset" dir="${dist.dir}">
<include name="*.zip" />
<include name="*.zip.sha1" />
<include name="*.tar.gz.sha1" />
</fileset>
<tstamp>
<format property="timestamp.isoformat"
pattern="yyyy-mm-dd'T'HH:mm:ss" locale="en" />
</tstamp>
<target name="init">
<mkdir dir="${build.dir.dir}" />
<mkdir dir="${build.classes.dir}" />
<mkdir dir="${dist.dir}" />
<mkdir dir="${dist.doc.dir}" />
<mkdir dir="${javadoc.dir}" />
</target>
<target name="compile" depends="init">
<condition property="build.debuglevel1"
value="lines,source"
else="lines,vars,source" >
<isset property="release.build" />
</condition>
<echo level="verbose">debug level=${build.debuglevel1}</echo>
<javac destdir="${build.classes.dir}"
debug="true"
debuglevel1="${build.debuglevel1}"
includeAntRuntime="false"
srcdir="src">
<classpath refid="compile.classpath" />
</javac>
<copy todir="${build.classes.dir}" >
<fileset dir="src"
includes="${source.files.tocopy}" />
</copy>
</target>
<target name="jar"
description="create the JAR file"
depends="compile">
<property name="manifest.mf"
location="${build.dir}/manifest.mf" />
<manifest file="${manifest.mf}" >
<attribute name="Built-By" value="${user.name}" />
<attribute name="Sealed" value="true" />
<attribute name="Built-On" value="${timestamp.isoformat}" />
<attribute name="Main-Class" value="${main.class}" />
</manifest>
<jar destfile="${target.jar}"
manifest="${manifest.mf}">
<fileset dir="${build.classes.dir}" />
</jar>
</target>
<target name="unjar" depends="init">
<unjar
src="${target.jar}"
dest="${unjar.jar}">
</unjar>
</target>
<target name="test-init" depends="jar">
<mkdir dir="${test.classes.dir}" />
<delete dir="${test.data.dir}" />
<delete dir="${tests.reports.dir}" />
<mkdir dir="${test.data.dir}" />
<mkdir dir="${test.reports.dir}" />
</target>
<target name="test-compile" depends="compile,test-init">
<javac destdir="${test.classes.dir}"
debug="true"
includeAntRuntime="yes"
srcdir="test">
<classpath refid="test.compile.classpath" />
</javac>
<copy todir="${test.classes.dir}" >
<fileset dir="test" includes="${source.files,tocopy}" />
</copy>
</target>
<target name="unit-tests" depends="test-compile">
<junit printsummary="false"
errorProperty="test.failed"
failureProperty="test.failed"
fork="${junit.fork}"
forkmode="${junit.forkmode}" >
<classpath refid="${test.forking.classpath}" />
<assertions enableSystemAssertions="true">
<enable package="d1.core" />
<enable />
<disable class="d1.core.Events" />
</assertions>
<sysproperty key="debug" value="true" />
<sysproperty key="data.dir" file="./data" />
<formatter type="brief" usefile="false" />
<formatter type="xml" />
<test name="${testcase}" todir="${test.data.dir}"
if="testcase" />
<batchtest todir="${test.data.dir}"
unless="testcase">
<fileset dir="${test.classes.dir}" >
<include name="**/test/*Test.class" />
<patternset refid="failing.tests" />
</fileset>
</batchtest>
</junit>
<junitreport todir="${test.data.dir}">
<fileset dir="${test.data.dir}">
<include name="TEST-*.xml" />
</fileset>
<report format="frames"
todir="${test.reports.dir}" />
</junitreport>
<fail if="test.failed">Tests failed.Check ${test.reports.dir}</fail>
</target>
<target name="javadocs" depends="compile"
description="make the java docs">
<javadoc
access="private"
destdir="${javadoc.dir}"
packagenames="d1.*"
sourcepath="src"
windowtitle="${ant.project.name}"
failonerror="true">
<classpath refid="compile.classpath" />
</javadoc>
</target>
<target name="dist-docs" depends="init"
description="patch documents while copying" >
<tstamp />
<copy todir="${dist.doc.dir}" overwrite="true">
<fileset dir="doc" include="**/*.html" />
<filterset>
<filter token="DATE" value="${DSTAMP}" />
<filter token="TIME" value="${TSTAMP}" />
</filterset>
</copy>
<copy todir="${dist.doc.dir}" overwrite="true">
<fileset dir="doc" includes="**/*.txt" />
<filterset begintoken="[[" endtoken="]]" >
<filter token="TIMESTAMP" value="${timestamp.isoformat}" />
</filterset>
</copy>
</target>
<target name="scirpts" depends="init">
<copy todir="${dist.dir}" overwrite="true" >
<fileset dir="bin"
includes="${unix.scripts},${dos.scripts}" />
<filter token="TARGET.NAME"
value="${target.name}" />
</filterset>
</copy>
<fixcrlf srcdir="${dist.dir}" eol="unix"
includes="${unix.scripts}" />
<fixcrlf srcdir="dist.dir" eol="dos"
includes="${dos.scripts}" />
<chmod dir="${dist.dir}" perm="a+x"
includes="${unix.scripts}" />
</target>
<target name="fix-docs" depends="dist-docs,javadocs">
<property name="readme.windows.file"
location="${dist.doc.dir}/readme.txt" />
<property name="readme.unix.file"
location="${dist.doc.dir}/README" />
<copy name="${readme.windows.file}"
tofile="${readme.unix.file}" />
<fixcrlf eol="crlf" file="${readme.windows.file}"
tab="remove" tablength="4" />
<fixcrlf eol="lf" file="${readme.unix.file}"
tab="remove" tablength="2" />
</target>
<target name="docs" depends="fix-docs" />
<target name="init-security" >
<property name="keystore.dir"
location="${user.home}/.secret" />
<mkdir dir="${keystore.dir}" />
<chmod file="${keystore.dir}" perm="700" />
<property name="keystore"
location="${keystore.dir}/local.keystore" />
<property file="${keystore.dir}/keystore.properties" />
<property name="keystore.alias" value="code.singer" />
</target>
<target name="get-password" depends="init-security" >
<input addproperty="keystore.password">password:</input>
</target>
<target name="create-signing-key" depends="get-password">
<genkey
alias="${keystore.alias}"
keystore="${keystore}"
storepass="${keystore.password}"
validity="366">
<dname>
<param name="CN" value="autosigner" />
<param name="OU" value="Steve and Erik" />
<param name="O" value="Apache Software Foundation" />
<param name="C" value="EU" />
</dname>
</genkey>
</target>
<target name="delete-keystore" depends="init-security" >
<delete file="${keystore}" />
</target>
<target name="sign-jar" depends="jar,get-password">
<fail>
no keystore ${keystore},run create-signing-key" >
<condition>
<not><available file="${keystore}" /></not>
</condition>
</fail>
<signjar jar="${target.jar}"
alias="${keystore.alias}"
keystore="${keystore}"
storepass="${keystore.password}" />
</target>
<target name="package-everything"
description="package everything"
depends="create-src-zipfile,create-bin,zipfile" />
<target name="create-bin-zipfile"
depends="sign-jar,docs,scripts"
description="create the distributable Zip files">
<zip destFile="${target.zip}" >
<zipfileset file="${target.jar}"
prefix="${project.name-ver}" />
<zipfileset dir="${dist.doc.dir}"
include="**/*"
prefix="${project.name-ver}/docs" />
<zipfileset dir="${javadoc.dir}"
includes="**/*"
prefix="${project.name-ver}/docs/api" />
</zip>
</target>
<target name="unzip-bin-zipfile" depends="create-bin-zipfile" >
<unzip src="${target.zip}" dest="${unzip.bin.dir}" />
</target>
<target name="create-src-zipfile" depends="sign-jar" >
<zip destfile="${src.zip}">
<zipfileset file="${target.jar}"
prefix="${project.name-ver}" />
<zipfileset dir="."
includes="src/**,test/**,doc/**,*.xml"
prefix="${project.name-ver}" />
</zip>
</target>
<target name="unzip-src-zipfile" depends="create-src-zipfile">
<unzip src="${src.zip}" dest="${unzip.src.dir}" />
</target>
<target name="checksum" depends="init">
<checksum file="${target.zip}"
algorithm="sha1" format="MD5SUM" />
<checksum file="${src.zip}"
algorithm="sha1" format="MD5SUM" />
<checksum file="target.zip"
algorithm="sha1" format="MD5SUM"
property="target.zip.sha1" />
<checksum file="${src.zip}" algorithm="sha1" format="MD5SUM"
property="src.zip.sha1" />
</target>
<target name="email-announcement-html" depends="checksum">
<mail
tolist="${email.to}" from="${email.from}"
subject="New release of ${target.name}"
mailhost="${email.server}" mailport="${email.port}"
ssl="${email.ssl}"
user="${email.user}"
password="${email.password}"
messageMimeType="text/html">
<message><![CDATE[
<p>
Here is a new build of ${target.name}
The SHA1 checksum of the file is:
</p>
<pre>
${target.zip.sha1}
</pre>
<p><i>-The development team</i></p>
]]></message>
<fileset file="${target.zip}" />
<fileset file="${target.zip}.sha1" />
</mail>
</target>
<target name="ssh-init" depends="init">
<fail unless="server">Set the "Server" property!</fail>
<property name="ssh.propfile" location="secure/${server}.ssh.properties" />
<loadproperties srcFile="${ssh.propfile}" />
<property name="tmp.dir" location="${build.dir}/tmp" />
<mkdir dir="${tmp.dir}" />
</target>
<target name="ssh-mkdirs" depends="ssh-init" >
<sshexec host-"${ssh.server}"
username="${ssh.user}"
passphrase="${ssh.passphrase}"
keyfile="${ssh.keyfile}"
command="mkdir -p ${ssh.dir}"/>
</target>
<target name="scp-upload" depends="ssh-init,ssh-mkdirs" >
<echo>SCP target is ${ssh.server}</echo>
<property name="ssh.path"
value="${ssh.user}@${ssh.server}:${ssh.dir}" />
<scp remoteToDir="${ssh.path}"
passphrase="${ssh.passphrase}"
keyfile="${ssh.keyfile}"
verbose="${ssh.verbose}" >
<fileset refid="ftp.upload.fileset" />
</scp>
</target>
<target name="ssh-chmod" depends="scp-upload" >
<sshexec host="${ssh.server}"
username="${ssh.user}"
passphrase="${ssh.passphrase}"
keyfile="${ssh.keyfile}"
command="chmod a+r public_html/*" />
</target>
<target name="scp-download" depends="ssh-init,checksum" >
<basename property="remote.f" file="${target.zip}" />
<property name="ssh.download.src"
value="${ssh.user}@${ssh.server}:${ssh.dir}/${remote.f}" />
<tempfile property="ssh.download.dest" prefix="ssh"
suffix=".zip" destDir="${java.io.tmpdir}" />
<echo>Downloading ${ssh.download.src} to ${ssh.download.dest}</echo>
<fail>
<condition>
<not>
<checksum algorithm="sha1" format="MD5SUM"
property="${target.zip.sha1}"
file="${ssh.download.dest}" />
</not>
</condition>
Checksum failure for ${ssh.download.dest}
</fail>
</target>
<target name="scp" depends="ssh-chmod,scp-download"
description="upload the file then download it using SCP" />
</project>