svn+ant+tomcat+hudson 持续构建WEB应用最佳实践
经过几天的研究,终于实现自动构建部署WEB应用的目标,详细配置过程记录如下
一、准备安装包
jdk1.6.0_26
apache-ant-1.8.2
apache-tomcat-5.5.27
hudson-2.1.0
二、在hudson中配置环境,包括jdk、ant、svn信息等
将hudson-2.1.0拷贝到tomcat/webapps目录下,启动tomcat,startup.bat
打开IE在地址栏中输入 http://localhost:8080/hudson-2.1.0/
进入系统管理-》系统设置
JDK环境配置
ANT环境配置
MAVEN环境配置
SHELL命令设置( 重要) windows下输入cmd
MAVEN构建环境配置 ,主要是mvn选择和目标命令配置
邮件通知配置,用户默认邮件后缀将加在构建用户名后面作为邮件地址,以便每次构建邮件通知项目成员
三、在hudson中创建一个新任务,为了配置方便,我们这里选择自由风格
下面是项目的详细配置
SCM配置, url中输入工程代码仓库地址,这里涉及到权限验证,需要输入用户和密码
构建触发配置,poll scm 采用调度轮询源代码库,配置帮助可以查看输入框后的帮助
ANT构建配置
首先选择 Add build step,选择 Invoke Ant ,然后在ant version中下拉选择前面配置的ant-1.8.2,targets不是必填项,默认是ant构建default目标 default="deploy"
项目邮箱通知相关干系人, Recipients 为项目负责人,一般填写经理邮箱,
第一个选项为给所有人发送每一次不稳定的构建
第二个选项为发送给所有与这次构建相关的成员
最后不要忘记点击Save按钮保存配置
build.xml
将build.xml文件放置到工程根目录下,hudson构建会自动执行ANT
自动部署可以使用catalina-ant.jar中的deploy和undeploy命令,但是这种方式将要在tomcat中反复部署war,最终导致jvm内存不足,所以选择
shutdown和startup方式来操作tomcat,已达到持续构建的目标
启动和停止tomcat的方式有多种,其中可以在ant中调用windows计划任务,具体查看文件中的注释。
整个配置涉及到启动2个tomcat,所以需要更改tomcat/conf/server.xml文件中的相关端口,保证2个tomcat端口不冲突
hudson持续构建命令行输出
一、准备安装包
jdk1.6.0_26
apache-ant-1.8.2
apache-tomcat-5.5.27
hudson-2.1.0
二、在hudson中配置环境,包括jdk、ant、svn信息等
将hudson-2.1.0拷贝到tomcat/webapps目录下,启动tomcat,startup.bat
打开IE在地址栏中输入 http://localhost:8080/hudson-2.1.0/
进入系统管理-》系统设置
JDK环境配置
ANT环境配置
MAVEN环境配置
SHELL命令设置( 重要) windows下输入cmd
MAVEN构建环境配置 ,主要是mvn选择和目标命令配置
邮件通知配置,用户默认邮件后缀将加在构建用户名后面作为邮件地址,以便每次构建邮件通知项目成员
三、在hudson中创建一个新任务,为了配置方便,我们这里选择自由风格
下面是项目的详细配置
SCM配置, url中输入工程代码仓库地址,这里涉及到权限验证,需要输入用户和密码
构建触发配置,poll scm 采用调度轮询源代码库,配置帮助可以查看输入框后的帮助
ANT构建配置
首先选择 Add build step,选择 Invoke Ant ,然后在ant version中下拉选择前面配置的ant-1.8.2,targets不是必填项,默认是ant构建default目标 default="deploy"
项目邮箱通知相关干系人, Recipients 为项目负责人,一般填写经理邮箱,
第一个选项为给所有人发送每一次不稳定的构建
第二个选项为发送给所有与这次构建相关的成员
最后不要忘记点击Save按钮保存配置
build.xml
<?
xml version="1.0" encoding="utf-8"
?>
< project name ="category" basedir ="." default ="deploy" >
< property name ="lib.dir" value ="${basedir}/WebRoot/WEB-INF/lib" />
< property name ="src.dir" value ="${basedir}/java" />
< property name ="build.dir" value ="${basedir}/build" />
< property name ="app.dir" value ="D:\apache-tomcat-5.5.27" >
</ property >
< property name ="runtime.lib" value ="${app.dir}\common\lib" />
< property name ="deploy.lib" location ="${basedir}/deploylib" />
< property name ="deploy.dir" value ="${app.dir}\webapps" />
< property name ="app.url" value ="http://localhost:8899/" >
</ property >
< property name ="javac.debug" value ="on" />
< property name ="javac.optimize" value ="off" />
< property name ="javac.target" value ="1.5" />
< property name ="javac.source" value ="1.5" />
< condition property ="tomcat.open" >
< http url ="${app.url}" />
</ condition >
< condition property ="tomcat.close" >
< not >
< http url ="${app.url}" />
</ not >
</ condition >
< path id ="project.class.path" >
< fileset dir ="${lib.dir}" >
< include name ="***.jar" />
</ fileset >
< fileset dir ="${deploy.lib}" >
< include name ="*.jar" />
</ fileset >
</ path >
< target name ="init" >
< tstamp />
< property environment ="env" />
< delete dir ="${build.dir}" />
< mkdir dir ="${build.dir}" />
</ target >
< target name ="compaile" depends ="init" >
< javac srcdir ="${src.dir}" destdir ="${build.dir}" encoding ="utf-8" classpathref ="project.class.path" source ="${javac.source}" target ="${javac.target}" debug ="true" >
</ javac >
< copy todir ="${build.dir}" >
< fileset dir ="${basedir}/src" >
</ fileset >
< fileset dir ="${src.dir}" >
< include name ="**/*.xml" />
< include name ="***.properties" />
</ fileset >
</ copy >
</ target >
< target name ="war" depends ="compaile" >
< copy todir ="${basedir}/WebRoot/WEB-INF/classes" >
< fileset dir ="${build.dir}" >
</ fileset >
</ copy >
< war destfile ="${basedir}/${ant.project.name}.war" basedir ="${basedir}/WebRoot" webxml ="${basedir}/WebRoot/WEB-INF/web.xml" >
</ war >
</ target >
< target name ="tomcat-start" >
<!-- ALL OK -->
< exec dir ="${app.dir}/bin" executable ="cmd" >
< env path ="${app.dir}" key ="CATALINA_HOME" />
< env path ="${app.dir}\temp" key ="CATALINA_TMPDIR" />
< env path ="${app.dir}" key ="CATALINA_BASE" />
< arg value ="/C startup.bat" />
</ exec >
<!--
C:\>SCHTASKS /Create /RU SYSTEM /SC ONSTART /TN StartupTomcat /TR "D:\apache-tomcat-5.5.27\bin\startup.bat"
<exec executable="SCHTASKS">
<arg value="/Run"/>
<arg value="/TN"/>
<arg value="startup"/>
</exec>
-->
<!--
<exec executable="${app.dir}/bin/startup.bat" spawn="true" vmlauncher="false">
<env key="CATALINA_HOME" value="${app.dir}" />
<arg line="/c start ${app.dir}/bin/startup.bat" />
</exec>
-->
<!--
<java jar="${app.dir}/bin/bootstrap.jar" fork="true">
<jvmarg value="-Dcatalina.home=${app.dir}"/>
<arg line="start"/>
</java>
-->
</ target >
< target name ="tomcat-stop" if ="" >
<!-- <exec dir="${app.dir}/bin" executable="cmd">
<env path="${app.dir}" key="CATALINA_HOME" />
<arg value="/c shutdown.bat" />
</exec>
<echo>111</echo> -->
<!--
C:\>SCHTASKS /Create /RU SYSTEM /SC ONSTART /TN ShutdownTomcat /TR "D:\apache-tomcat-5.5.27\bin\shutdown.bat"
<exec executable="SCHTASKS">
<arg value="/Run"/>
<arg value="/TN"/>
<arg value="ShutdownTomcat"/>
</exec>
-->
< java jar ="${app.dir}/bin/bootstrap.jar" fork ="true" >
< jvmarg value ="-Dcatalina.home=${app.dir}" />
< arg line ="stop" />
</ java >
< waitfor maxwait ="5" maxwaitunit ="second" >
< available file ="errors.log" />
</ waitfor >
</ target >
< target name ="deploy" depends ="war" >
< antcall target ="tomcat-stop" />
< waitfor maxwait ="60000" >
< not >
< http url ="${app.url}" />
</ not >
</ waitfor >
< echo > wati for tomcat close </ echo >
< delete dir ="${deploy.dir}/${ant.project.name}" >
</ delete >
< copy todir ="${deploy.dir}" file ="${basedir}/${ant.project.name}.war" >
</ copy >
< antcall target ="tomcat-start" />
</ target >
</ project >
< project name ="category" basedir ="." default ="deploy" >
< property name ="lib.dir" value ="${basedir}/WebRoot/WEB-INF/lib" />
< property name ="src.dir" value ="${basedir}/java" />
< property name ="build.dir" value ="${basedir}/build" />
< property name ="app.dir" value ="D:\apache-tomcat-5.5.27" >
</ property >
< property name ="runtime.lib" value ="${app.dir}\common\lib" />
< property name ="deploy.lib" location ="${basedir}/deploylib" />
< property name ="deploy.dir" value ="${app.dir}\webapps" />
< property name ="app.url" value ="http://localhost:8899/" >
</ property >
< property name ="javac.debug" value ="on" />
< property name ="javac.optimize" value ="off" />
< property name ="javac.target" value ="1.5" />
< property name ="javac.source" value ="1.5" />
< condition property ="tomcat.open" >
< http url ="${app.url}" />
</ condition >
< condition property ="tomcat.close" >
< not >
< http url ="${app.url}" />
</ not >
</ condition >
< path id ="project.class.path" >
< fileset dir ="${lib.dir}" >
< include name ="***.jar" />
</ fileset >
< fileset dir ="${deploy.lib}" >
< include name ="*.jar" />
</ fileset >
</ path >
< target name ="init" >
< tstamp />
< property environment ="env" />
< delete dir ="${build.dir}" />
< mkdir dir ="${build.dir}" />
</ target >
< target name ="compaile" depends ="init" >
< javac srcdir ="${src.dir}" destdir ="${build.dir}" encoding ="utf-8" classpathref ="project.class.path" source ="${javac.source}" target ="${javac.target}" debug ="true" >
</ javac >
< copy todir ="${build.dir}" >
< fileset dir ="${basedir}/src" >
</ fileset >
< fileset dir ="${src.dir}" >
< include name ="**/*.xml" />
< include name ="***.properties" />
</ fileset >
</ copy >
</ target >
< target name ="war" depends ="compaile" >
< copy todir ="${basedir}/WebRoot/WEB-INF/classes" >
< fileset dir ="${build.dir}" >
</ fileset >
</ copy >
< war destfile ="${basedir}/${ant.project.name}.war" basedir ="${basedir}/WebRoot" webxml ="${basedir}/WebRoot/WEB-INF/web.xml" >
</ war >
</ target >
< target name ="tomcat-start" >
<!-- ALL OK -->
< exec dir ="${app.dir}/bin" executable ="cmd" >
< env path ="${app.dir}" key ="CATALINA_HOME" />
< env path ="${app.dir}\temp" key ="CATALINA_TMPDIR" />
< env path ="${app.dir}" key ="CATALINA_BASE" />
< arg value ="/C startup.bat" />
</ exec >
<!--
C:\>SCHTASKS /Create /RU SYSTEM /SC ONSTART /TN StartupTomcat /TR "D:\apache-tomcat-5.5.27\bin\startup.bat"
<exec executable="SCHTASKS">
<arg value="/Run"/>
<arg value="/TN"/>
<arg value="startup"/>
</exec>
-->
<!--
<exec executable="${app.dir}/bin/startup.bat" spawn="true" vmlauncher="false">
<env key="CATALINA_HOME" value="${app.dir}" />
<arg line="/c start ${app.dir}/bin/startup.bat" />
</exec>
-->
<!--
<java jar="${app.dir}/bin/bootstrap.jar" fork="true">
<jvmarg value="-Dcatalina.home=${app.dir}"/>
<arg line="start"/>
</java>
-->
</ target >
< target name ="tomcat-stop" if ="" >
<!-- <exec dir="${app.dir}/bin" executable="cmd">
<env path="${app.dir}" key="CATALINA_HOME" />
<arg value="/c shutdown.bat" />
</exec>
<echo>111</echo> -->
<!--
C:\>SCHTASKS /Create /RU SYSTEM /SC ONSTART /TN ShutdownTomcat /TR "D:\apache-tomcat-5.5.27\bin\shutdown.bat"
<exec executable="SCHTASKS">
<arg value="/Run"/>
<arg value="/TN"/>
<arg value="ShutdownTomcat"/>
</exec>
-->
< java jar ="${app.dir}/bin/bootstrap.jar" fork ="true" >
< jvmarg value ="-Dcatalina.home=${app.dir}" />
< arg line ="stop" />
</ java >
< waitfor maxwait ="5" maxwaitunit ="second" >
< available file ="errors.log" />
</ waitfor >
</ target >
< target name ="deploy" depends ="war" >
< antcall target ="tomcat-stop" />
< waitfor maxwait ="60000" >
< not >
< http url ="${app.url}" />
</ not >
</ waitfor >
< echo > wati for tomcat close </ echo >
< delete dir ="${deploy.dir}/${ant.project.name}" >
</ delete >
< copy todir ="${deploy.dir}" file ="${basedir}/${ant.project.name}.war" >
</ copy >
< antcall target ="tomcat-start" />
</ target >
</ project >
将build.xml文件放置到工程根目录下,hudson构建会自动执行ANT
自动部署可以使用catalina-ant.jar中的deploy和undeploy命令,但是这种方式将要在tomcat中反复部署war,最终导致jvm内存不足,所以选择
shutdown和startup方式来操作tomcat,已达到持续构建的目标
启动和停止tomcat的方式有多种,其中可以在ant中调用windows计划任务,具体查看文件中的注释。
整个配置涉及到启动2个tomcat,所以需要更改tomcat/conf/server.xml文件中的相关端口,保证2个tomcat端口不冲突
hudson持续构建命令行输出