ant 实现svn checkout代码 + Junit测试生成报告 + ftp上传 + 发邮件
这里做个记录
build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="hollykam5" basedir="." default="pack">
<!--define properties-->
<tstamp>
<format property="nowstamp" pattern="yyyyMMdd" locale="en"/>
</tstamp>
<property name="project" value="osas_statistic" />
<property name="project.web" value="${project}/webapp" />
<property name="project.lib" value="${project}/WebRoot/WEB-INF/lib" />
<property name="junitreport" value="${nowstamp}/junitReport" />
<property name="classes" value="${project.web}/WEB-INF/classes" />
<property name="lib" value="${project.web}/WEB-INF/lib" />
<property name="junitXml" value="E:\work\apache-ant-1.8.4\etc" />
<property file="svn.properties" />
<property file="ftp.properties" />
<property file="MailLogger.properties" />
<!--define classPath-->
<path id="master-classpath">
<fileset dir="${project.lib}">
<include name="*.jar"/>
</fileset>
<pathelement path="${classes}"/>
<fileset dir="E:\work\lib">
<include name="*.jar"/>
</fileset>
</path>
<!--define Ant Task extends-->
<typedef resource="org/tigris/subversion/svnant/svnantlib.xml" />
<taskdef resource="net/sf/antcontrib/antlib.xml" />
<taskdef name="junit" classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask" />
<!--clean-->
<target name="clean">
<delete dir="${nowstamp}" />
<delete dir="${classes}" />
<delete file="TESTS-TestSuites.xml" />
</target>
<target name="cleanAll" depends="clean">
<delete dir="${project}" />
</target>
<!--init-->
<target name="init">
<mkdir dir="${junitreport}" />
<mkdir dir="${classes}" />
<mkdir dir="${lib}" />
</target>
<!--checkout source-->
<target name="checkout">
<svn username="${svn.repository.user}" password="${svn.repository.passwd}">
<checkout url="${svn.url}" revision="HEAD" destPath="${project}" />
</svn>
</target>
<!--compile-->
<target name="compile" depends="clean, init">
<javac srcdir="${basedir}" destdir="${classes}" nowarn="on" encoding="utf-8" source="1.6" >
<classpath refid="master-classpath" />
</javac>
<copy todir="${classes}">
<fileset dir="${project}/src">
<include name="**/*" />
</fileset>
</copy>
<copy todir="${project.web}/WEB-INF/lib">
<fileset dir="${project.lib}">
<include name="**/*.jar" />
</fileset>
</copy>
<copy todir="${project.web}/WEB-INF/">
<fileset dir="${project}/WebRoot/WEB-INF/">
<include name="**/*.xml" />
</fileset>
</copy>
<copy todir="${project.web}/">
<fileset dir="${project}/WebRoot/">
<include name="**/*" />
<exclude name="WEB-INF" />
</fileset>
</copy>
</target>
<!--junit test and report-->
<target name="test" depends="compile">
<junit printsummary="on">
<classpath refid="master-classpath" />
<formatter type="xml"/>
<batchtest fork="on" todir="${junitreport}">
<fileset dir="${project}/test">
<include name="**/*.java"/>
</fileset>
</batchtest>
</junit>
<junitreport>
<fileset dir="${junitreport}">
<include name="TEST-*.*"/>
</fileset>
<report format="noframes" styledir="${junitXml}" todir="${nowstamp}" />
</junitreport>
</target>
<!--pakage Jar-->
<target name="package" depends="clean, init, compile">
<jar jarfile="${project}.jar">
<fileset dir="${project.web}">
<include name="**/*"/>
</fileset>
</jar>
<move file="${project}.jar" todir="${nowstamp}" />
</target>
<!--pakage war-->
<target name="war" depends="compile">
<war warfile="${nowstamp}/${project}.war" webxml="${project}/WebRoot/WEB-INF/web.xml">
<fileset dir="${project}/WebRoot/" >
<include name="**/*" />
<exclude name="WEB-INF" />
</fileset>
<lib dir="${project.lib}">
<include name="*.jar"/>
</lib>
<classes dir="${classes}"/>
</war>
</target>
<!--send report email-->
<target name="mail" >
<mail mailhost="${MailLogger.mailhost}" mailport="25" subject="${project} Junit Test Result ${nowstamp}">
<from address="${MailLogger.from}"/>
<to address="${MailLogger.failure.to}"/>
<message>项目:${project} 测试日期:${nowstamp} 测试结果请查收附件!</message>
<attachments>
<fileset dir="${nowstamp}">
<include name="junit-*.html" />
</fileset>
</attachments>
</mail>
</target>
<!--ftp&deployed-->
<target name="ftp">
<ftp server="${ftp.url}" port="${ftp.port}"
remotedir="${ftp.dir}" binary="yes"
userid="${ftp.user}" password="${ftp.passwd}">
<fileset dir="${nowstamp}">
<include name="${project}.war"/>
</fileset>
</ftp>
</target>
<!--shortcut-->
<target name="run" depends="cleanAll, checkout,init, compile, test, package, mail, ftp" />
<target name="build" depends="checkout, compile" />
<target name="pack" depends="checkout, compile, test,package,war" />
<target name="report" depends="test, mail" />
<target name="deploy" depends="pack, report, ftp" />
</project>
svn.properties:
svn.url=http://192.168.1.98/svn/repos/osas/trunk/osas-statist
svn.repository.user=rhguo
svn.repository.passwd=guoronghua
MailLogger.properties
MailLogger.mailhost=mail.cm-topsci.com
MailLogger.port=25
[email protected]
[email protected]
[email protected]
MailLogger.failure.subject=Build Failure
MailLogger.success.subject=Build Success
MailLogger.success.notify=true
ftp.properties
ftp.url=192.168.1.30
ftp.port=21
ftp.dir=OSAS
ftp.user=guo
ftp.passwd=guo
ant -f build.xml deploy -logger org.apache.tools.ant.listener.MailLogger