帖自
调试了几天,搞定了svn+ant实现nightly build自动编译发布java程序
脚本如下,可根据需要调整
应用环境
linux rhas4ant 1.6.5jdk 1.5subversion 1.4应用情景描述:
要求每天凌晨12:30自动从svn取出最新版本的java项目编译,如果发生错误,发送邮件给相关的人员,编译成功则打包,将打包后项目文件移到apache目录发布,将war文件ftp到远程服务器上发布需要的第三方软件包:(请将以下软件包放到$ANT_HOME/lib下)
1.邮件发送需要的activation.jar –http://java.sun.com/products/javabeans/glasgow/jaf.html).mail.jar –http://java.sun.com/products/javamail/2.ftp上传需要的
commons-net-1.4.1.jar –http://jakarta.apache.org/commons/net/index.htmljakarta-oro-2.0.8.jar –http://jakarta.apache.org/oro/3.svn需要的
svnant.jar –http://subclipse.tigris.org/svnant.htmlsvnClientAdapter.jarsvnjavahl.jar运行脚本
1.Ant build.xml<?xml version="1.0" encoding="UTF-8"?>
<!– WARNING: Eclipse auto-generated file. Any modifications will be overwritten. To include a user specific buildfile here, simply create one in the same directory with the processing instruction <?eclipse.ant.import?> as the first entry and export the buildfile again. –><project basedir="." default="main" name="ProjectName"> <!– all properties are in build.properties –> <property file="build.properties" /><!– load the svn task –>
<taskdef resource="svntask.properties"/><path id="project.classpath">
<pathelement location="${lib.dir}/commons-logging.jar"/> <pathelement location="${lib.dir}/hibernate3.jar"/> <pathelement location="${lib.dir}/j2ee.jar"/> <pathelement location="${lib.dir}/jxl.jar"/> <pathelement location="${lib.dir}/spring.jar"/> <pathelement location="${lib.dir}/struts.jar"/> </path> <target name="main" depends="deploy"> <delete dir="${work.space}"/> </target><!–check out project files from svn–>
<target name="svn"> <svn username="svnuser" password="svnpassword" javahl="false"> <checkout url="${urlRepos}" destPath="${work.space}"/> </svn> </target><!–compile the project files–>
<target name="compile" depends="svn"> <echo message="${ant.project.name}: ${ant.file}"/> <mkdir dir="${build.dir}"/> <javac debug="true" debuglevel="${debuglevel}" destdir="${build.dir}" source="${source}" target="${target}"> <src path="${java.source}"/> <classpath refid="project.classpath"/> </javac> </target><!–compress the project build files–>
<target name="compress" depends="compile" description="Build the distribution .jar file"> <mkdir dir="${dist.dir}"/> <!–compress jar–> <jar jarfile="${jar.file}" basedir="${build.dir}"> <include name="${rootpackage}*.class"/> <manifest> <attribute name="${ant.project.name}-Version" value="${build.version}"/> </manifest> </jar> <!–compress j2ee war file–> <war destfile="${war.file}" webxml="${work.space}/web.xml"> <fileset dir="${web.dir}"/> <classes dir="${resource.dir}"/> <lib dir="${lib.dir}"/> <lib dir="${dist.dir}"/> </war> <!–compress the whole project files–> <tar destfile="${tar.file}" basedir="${work.space}" compression="gzip"/> </target><!–deploy the project files,include moving to deploy directory, ftp to the remote server and mail the team members–>
<target name="deploy" depends="compress"> <copy file="${war.file}" todir="${war.dir}"/> <move file="${tar.file}" todir="${tar.dir}"/> <ftp server="192.168.2.212" binary="true" verbose="true" userid="tomcat" password="tomcat" remotedir="/home/tomcat"> <fileset dir="${dist.dir}"/> </ftp> <!–<mail mailhost="192.168.2.248" subject="New Build" user="test" password="test" bcclist=""> <from address="> <message mimetype="text/html" src="mail.html" /> <fileset dir="dist"> <includes name="**/*.tar.gz"/> </fileset></mail>–>
</target></project>2.Ant build.properties(请根据需要做相应的修改)
# —————————————————————————–# build.properties# This file is referenced by the sample build.xml file.# —————————————————————————–build.version=1.0.0
rootpackage=com/company/frameworks
debuglevel=source,lines,varstarget=1.5source=1.5work.space=workingcopybuild.dir=${work.space}/bindist.dir=${work.space}/distlib.dir=${work.space}/libjava.source=${work.space}/srcweb.dir=${work.space}/webresource.dir=${work.space}/resourceswar.dir=/usr/local/tomcat/webappswar.file=${dist.dir}/${ant.project.name}demo.warjar.file=${dist.dir}/${ant.project.name}_${build.version}.jartar.dir=/usr/local/apache/htdocstar.file=${ant.project.name}_${build.version}.tar.gzurlRepos=svn://localhost/trunk/ProjectName#———————————————
# MailLogger properties#———————————————MailLogger.mailhost=192.168.2.248MailLogger.user=mailuserMailLogger.password=testMailLogger.failure.notify=trueMailLogger.success.notify=falseMailLogger.failure.subject=${ant.project.name} Project Build FailureMailLogger.success.subject=${ant.project.name} Project Build Success3.build.sh
#!/bin/sh
#crontab -e
#run the nightly build at 00:30 every day#30 0 * * * /path/to/build.sh#get the current directory
currentdir=`pwd`#get this shell script directory
workdir=$0workdir="${workdir/build.sh/}"cd $workdir
ANT_HOME=/usr/local/ant
JAVA_HOME=/usr/java/jdk$ANT_HOME/bin/ant -f build.xml -logger org.apache.tools.ant.listener.MailLogger
#return the initial directory
cd $currentdir执行脚本
请将以上三个脚本放到一个目录下,给build.sh运行权限编辑当前用户的crontab如下crontab -e#加入30 0 * * * /path/to/build.sh