svn+ant实现nightly build自动发布
发布日期:2021-09-18 01:36:53 浏览次数:2 分类:技术文章

本文共 5051 字,大约阅读时间需要 16 分钟。

帖自

调试了几天,搞定了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="">
   <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,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.failure.notify=true
MailLogger.success.notify=false
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

转载地址:https://blog.csdn.net/yuezu1026/article/details/4653552 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:一个大型MySQL分布式系统诞生
下一篇:SElinux 常用命令

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年04月28日 00时33分15秒