svn+ant实现nightly build自动发布

本文介绍如何使用 SVN 和 Ant 实现 Java 项目的夜间自动构建与发布流程。该流程包括从 SVN 获取最新代码、自动编译、错误邮件通知、打包部署及远程 FTP 发布等功能。

http://blog.sina.com.cn/s/blog_48aa22a80100lpmi.html

svn+ant实现nightly build自动发布

(2010-09-14 15:55:47)
标签:

杂谈

分类: 配置管理

http://genstone.bokee.com/5762322.html                                       

 

调试了几天,搞定了svn ant实现nightly build自动编译发布java程序

脚本如下,可根据需要调整

应用环境
linux rhas4
ant 1.6.5
jdk 1.5
subversion 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.html
jakarta-oro-2.0.8.jar --http://jakarta.apache.org/oro/

3.svn需要的
svnant.jar     --http://subclipse.tigris.org/svnant.html
svnClientAdapter.jar
svnjavahl.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="aaa@company.com.cn,bbb@company.com.cn">
    <from address="svnadmin@pub.company.com.cn"/>
    <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,vars
target=1.5
source=1.5
work.space=workingcopy
build.dir=${work.space}/bin
dist.dir=${work.space}/dist
lib.dir=${work.space}/lib
java.source=${work.space}/src
web.dir=${work.space}/web
resource.dir=${work.space}/resources
war.dir=/usr/local/tomcat/webapps
war.file=${dist.dir}/${ant.project.name}demo.war
jar.file=${dist.dir}/${ant.project.name}_${build.version}.jar
tar.dir=/usr/local/apache/htdocs
tar.file=${ant.project.name}_${build.version}.tar.gz
urlRepos=svn://localhost/trunk/ProjectName

#---------------------------------------------
# MailLogger properties
#---------------------------------------------
MailLogger.mailhost=192.168.2.248
MailLogger.user=mailuser
MailLogger.password=test
MailLogger.from=svnadmin@pub.company.com.cn
MailLogger.failure.notify=true
MailLogger.success.notify=false
MailLogger.failure.to=aaaa@company.com.cn
MailLogger.success.to=bbbb@company.com.cn
MailLogger.failure.subject=${ant.project.name} Project Build Failure
MailLogger.success.subject=${ant.project.name} Project Build Success

3.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=$0
workdir="${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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值