具体包括编译、代码检出、运行单元测试、邮件通知(代码检出结果,版本发布等)、安装、打包、ftp上传等。
某些需要将相关的jar包放到ant安装路径下的lib目录中,具体如下:
邮件通知为:activation.jar和Mail.jar。
单元测试为:junit.jar。
ftp上传为:commons-net-2.0.jar和jakarta-oro-2.0.8.jar。
曾经使用过的一个build.xml代买如下:
<project name="server" basedir="." default="dev"> <property name="version" value="1.0" /> <property name="root" value="${basedir}" /> <property name="package" value="${basedir}/../package" /> <property name="src" location="src" /> <property name="etc" location="etc" /> <property name="test" location="test" /> <property name="webroot" location="${basedir}/WebRoot" /> <property name="webinf" location="${webroot}/WEB-INF" /> <property name="classes" location="${webinf}/classes" /> <property name="lib" location="${webinf}/lib" /> <property name="tomcat_server_folder" value="${basedir}/tomcat" /> <property name="build" location="Build" /> <property name="build_XXXXXXX" location="${build}/XXXXXXX" /> <property name="build_WebAppDir" value="${build_XXXXXXX}/webapps/XXXXXXX" /> <property name="build_tomcat_bin" value="${build_XXXXXXX}/bin" /> <property name="build_jdk_bin" value="${build_XXXXXXX}/jdk/jdk_linux/bin" /> <property name="package_jdk" value="${package}/jdk" /> <property name="package_package_jdkwindows" value="${package}/jdk/jdk_windows" /> <property name="package_jdklinux" value="${package}/jdk/jdk_linux" /> <property name="package_tomcat6.0" value="${package}/tomcat/tomcat6.0" /> <property name="install_package_dir" location="/opt/xxx/xxx" /> <property name="install_dir" location="/opt/xxx/xxx/install/XXXXXXX" /> <property name="install_jdk_bin" value="${install_dir}/jdk/jdk_linux/bin" /> <property name="install_tomcat_bin" value="${install_dir}/bin" /> <property name="mail_build_list" value="[email protected], [email protected]" /> <property name="mail_release_list" value="[email protected],[email protected], [email protected],[email protected]" /> <property name="mail_build_cclist" value="[email protected], [email protected]"/> <property name="mail_release_cclist" value="[email protected], [email protected]"/> <property name="findbugs.home" value="/opt/xxx/tools/findbugs-1.3.9" /> <path id="findbugs.path"> <fileset dir="${findbugs.home}"> <include name="**/*.jar" /> </fileset> </path> <property environment="env" /> <path id="classpath"> <pathelement path="${env.classpath}" /> <fileset dir="${lib}"> <include name="**/*.jar" /> <include name="**/*.zip" /> <exclude name="**/excluded/" /> </fileset> <pathelement location="${classes}" /> </path> <target name="clean"> <delete dir="${build}" /> <delete dir="${classes}" /> </target> <target name="init"> <mkdir dir="${classes}" /> <mkdir dir="${build}" /> </target> <!-- classpath setting --> <property environment="env" /> <path id="server.classpath"> <pathelement path="${env.classpath}" /> <fileset dir="${lib}"> <include name="**/*.jar" /> <include name="**/*.zip" /> </fileset> </path> <target name="compile" depends="init"> <javac srcdir="${src}" destdir="${classes}" encoding="UTF-8" classpathref="server.classpath" debug="true" deprecation="false" fork="true"> <!-- <compilerarg value="-Xlint:deprecation"/> --> </javac> <javac srcdir="${test}" destdir="${classes}" encoding="UTF-8" classpathref="server.classpath" debug="true" deprecation="false" fork="true"> <!-- <compilerarg value="-Xlint:deprecation"/> --> </javac> <copy todir="${classes}"> <fileset dir="${src}"> <exclude name="**/*.java" /> </fileset> </copy> <copy todir="${classes}" preservelastmodified="yes"> <fileset dir="${etc}" includes="**/*.*" /> </copy> <native2ascii src="${etc}" mce_src="${etc}" dest="${classes}" encoding="UTF-8"> <include name="**/*.properties" /> </native2ascii> </target> <target name="build" depends="compile"> <!-- install tomcat--> <copy todir="${build_XXXXXXX}"> <fileset dir="${package_tomcat6.0}/"> <exclude name="**/*.template" /> </fileset> </copy> <!-- install jdk--> <copy todir="${build_XXXXXXX}/jdk"> <fileset dir="${package_jdk}"> <exclude name="**/*.template" /> </fileset> </copy> <!-- install app--> <copy todir="${build_WebAppDir}"> <fileset dir="${webroot}"> <exclude name="**/*.template" /> </fileset> </copy> <!-- copy startup.sh--> <copy todir="${build_XXXXXXX}/bin"> <fileset dir="${tomcat_server_folder}"> <include name="startup.sh" /> </fileset> </copy> <!-- copy shutdown.sh--> <copy todir="${build_XXXXXXX}/bin"> <fileset dir="${tomcat_server_folder}"> <include name="shutdown.sh" /> </fileset> </copy> <!-- copy server.xml--> <copy todir="${build_XXXXXXX}/conf"> <fileset dir="${tomcat_server_folder}"> <include name="server.xml" /> </fileset> </copy> <copy todir="${build_XXXXXXX}/mibs"> <fileset dir="./mibs"> <exclude name="**/.svn" /> </fileset> </copy> <!-- copy database dir--> <copy todir="${build_XXXXXXX}/database"> <fileset dir="./database"> <exclude name="**/.svn" /> </fileset> </copy> <!-- chmod bulid bin --> <chmod perm="777" type="file"> <fileset dir="${build_tomcat_bin}"> <include name="**/*" /> </fileset> <fileset dir="${build_jdk_bin}"> <include name="**/*" /> </fileset> </chmod> </target> <target name="install" depends="build"> <!-- install tomcat--> <copy todir="${install_dir}"> <fileset dir="${build_XXXXXXX}/"> <exclude name="**/.svn" /> </fileset> </copy> <!-- chmod install --> <chmod perm="777" type="file"> <fileset dir="${install_tomcat_bin}"> <include name="**/*" /> </fileset> <fileset dir="${install_jdk_bin}"> <include name="**/*" /> </fileset> </chmod> </target> <target name="package" depends="install"> <tar destfile="${install_package_dir}/XXXXXXX.tar"> <fileset dir="${install_dir}"> <include name="**/*" /> <exclude name="**/.svn" /> </fileset> </tar> </target> <target name="gzip" depends="package"> <gzip src="${install_package_dir}/XXXXXXX.tar" mce_src="${install_package_dir}/XXXXXXX.tar" zipfile="${install_package_dir}/XXX_${v}.tar.gz"> </gzip> </target> <target name="dev" depends="compile" /> <tstamp> <!-- <format property="TODAY" pattern="yyyy-MM-dd HH点mm分" locale="en" /> --> <format property="TODAY" pattern="yyyy-MM-dd" locale="en" /> </tstamp> <!-- FindBugs --> <taskdef name="findbugs" classname="edu.umd.cs.findbugs.anttask.FindBugsTask" classpathref="findbugs.path" /> <target name="findbugs" depends="compile"> <findbugs home="${findbugs.home}" output="html" outputFile="${findbugs.home}/report/XXX/findbugs/report-XXX-${TODAY}.html"> <auxClasspath path="${findbugs.home}/lib/findbugs-ant.jar" /> <auxClasspath> <fileset dir="${lib}" includes="*.jar" /> </auxClasspath> <sourcePath path="${src}" /> <class location="${classes}" /> </findbugs> </target> <!-- Send Build Mails --> <target name="sendbuildmail" description="Tell team of the new build"> <mail mailhost="XXXXXXXXX" mailport="25" user="[email protected]" password="XXXXXXXXXX" subject="XXXX Build Notice" from="[email protected]" tolist="${mail_build_list}" cclist="${mail_build_cclist}"> <message>The build in xxxx is available for testing.</message> <fileset dir="/${findbugs.home}/report/XXX/findbugs"> <include name="**/report-XXX-${TODAY}.html"/> </fileset> </mail> </target> <!-- Send Release Mails --> <!-- ant sendreleasemail -Dv=Release_0001.04 --> <target name="sendreleasemail" description="Tell team of the new build"> <mail mailhost="XXXXXXXXX" mailport="25" user="[email protected]" password="XXXXXXXXXX" subject="XXX_${v} Release Notice" from="[email protected]" tolist="${mail_release_list}" cclist="${mail_release_cclist}"> <message>All, The XXXX has been released. The package is: XXX_${v}.tar.gz. The release notes is: XXX_${v}_ReleaseNotes.doc. All have been placed on //10.0.0.214/XXX/XXX. Thanks. somebody ${TODAY} </message> </mail> </target> <!-- Run JUnit Test --> <target name="unittest" description="Invoke pre-deployment JUnit test cases"> <junit printsummary="yes" haltonfailure="no"> <classpath refid="classpath"/> <formatter type="xml" /> <batchtest fork="yes" todir="${findbugs.home}/report/XXX/junit/xml"> <fileset dir="${basedir}/test"> <include name="**/*Test.java" /> </fileset> </batchtest> </junit> <junitreport todir="${findbugs.home}/report/XXX/junit/html"> <fileset dir="${findbugs.home}/report/XXX/junit/xml"> <include name="*.xml"/> </fileset> <report format="frames" todir="${findbugs.home}/report/XXX/junit/html"/> </junitreport> </target> <target name="upload" description="Upload build to an FTP server"> <ftp action="send" server="10.0.0.204" remotedir="/opt/xxx/BiTV" userid="xxx" password="xxx" separator="/" verbose="yes" binary="yes"> <fileset dir="${install_package_dir}"> <include name="**/XXX_${v}.tar.gz"/> </fileset> </ftp> </target> <target name="dayBuild"> <ant target="build"/> <ant target="findbugs"/> <ant target="unittest"/> <ant target="install"/> <ant target="sendbuildmail"/> </target> <!-- ant releaseBuild -Dv=Release_0001.05 --> <target name="releaseBuild"> <ant target="build"/> <ant target="install"/> <ant target="gzip"/> <ant target="upload"/> <ant target="sendreleasemail"/> </target> </project>