用Groovy的AntBuilder编写wab部署工具

最近有个项目,有5、6个web分别部署在3地的服务器,每次发布都很头疼,已有的部署工具要么功能不灵活,要么太庞大,部署工具的部署本身就很麻烦。所以自己用Groovy的AntBuilder写了一个部署工具。在此记录:

部署工具的功能包括拷贝代码,压缩war包,上传至服务器,停止tomcat,解压缩,启动tomcat。(同步SVN和编译用IDE完成)

1、AntBuilder的copy

new AntBuilder().copy(todir:destdir) {
	fileset(dir:"${srcdir}/WebContent") {
		include(name:"**/*")
		exclude(name:"**/spring/**")
	}
}

 todir:目标目录

 fileset的dir:源目录

 include:包含文件的规则

 exclude:例外文件的规则

 

2、搜索本地maven库,拷贝jar文件。AntBuilder的fileScanner

def ant = new AntBuilder()
ant.fileScanner {
	fileset(dir:'D:/work/env/repos', casesensitive:false) {
		include(name:"**/${dep.artifactId}-${ver}.jar")
	}
}.each { jar ->
	println jar.path
	ant.copy(file:jar.path, tofile:"${destdir}/WEB-INF/lib/${jar.name}")
}
 fileScanner在某个目录中搜索某种规则的文件

 dir:base目录

 include:包含的文件规则

 

3、AntBuilder的war

new AntBuilder().war(destfile:"${dir}editor.war", webxml:"${dir}/WEB-INF/web.xml") {
	fileset(dir:"${dir}")
}

 

4、ssh登陆服务器,执行命令

new AntBuilder().sshexec(host: ${host},
	username: ${ssh_acc},
	password: ${ssh_pwd},
	command: "${tomcat_dir}/bin/shutdown.sh ; rm -f ${tomcat_dir}/${pro}editor.war")

  多命令用分号隔开

 

5、ssh登陆,上传文件

new AntBuilder().scp(file: "${tmpdir}/${pro}editor.war",
	todir: remote_dir,
	password: ssh_pwd,
	trust:"true")  //false:need knownhosts

 file:本地文件

 todir:远程目录。格式是‘登陆账号@服务器ip:保存目录’

 trust:false是需要knownhosts文件的。这个文件时ssh自动生成的。

 

6、AntBuilder的批量按序执行

new AntBuilder().sequential {
    ...
    ...
}

 

 

代码清单:

执行文件

package com.emg.groovy.dmtool

@groovy.util.logging.Log4j2
class dmtool {
	def ant = new AntBuilder()
	
	def copyWeb(srcdir, destdir) {
		ant.sequential {
			echo("begin copy srcdir:${srcdir}; destdir:${destdir}")
			delete(dir:destdir)
			mkdir(dir:destdir)
			//copy Webcontent
			copy(todir:destdir) {
				fileset(dir:"${srcdir}/WebContent") {
					include(name:"**/*")
					exclude(name:"**/spring/**")
				}
			}
			//copy classes
			copy(todir:"${destdir}/WEB-INF") {
				fileset(dir:"${srcdir}/target") {
					include(name:"**/*")
				}
			}
			echo("copy done")
		}
	}
	
	def copyDependencies(srcdir, destdir) {
		def pom = new XmlSlurper().parseText(new File("${srcdir}/pom.xml").text)
		pom.dependencies.dependency.each { dep ->
//			println "${dep.groupId}_${dep.artifactId}_${dep.version}"
			String ver = dep.version
			if (ver.startsWith("\$")) {
				ver = ver[2..-2]
				ver = pom.properties."$ver"
			}
			ant.fileScanner {
				fileset(dir:'D:/work/env/repos', casesensitive:false) {
					include(name:"**/${dep.artifactId}-${ver}.jar")
				}
			}.each { jar ->
				println jar.path
				ant.copy(file:jar.path, tofile:"${destdir}/WEB-INF/lib/${jar.name}")
			}
		}
	}
	
	def buildwar(dir) {
		ant.sequential {
			echo("begin build war dir:${dir}")
			delete(file:"${dir}editor.war")
			war(destfile:"${dir}editor.war", webxml:"${dir}/WEB-INF/web.xml") {
				fileset(dir:"${dir}")
			}
			echo("build war done")
		}
	}
	
	def deploywar(job, pro, tmpdir) {
//		println job
		def host = job.host
		def ssh_acc = job.ssh_acc
		def ssh_pwd = job.ssh_pwd
		def JAVA_HOME = job.JAVA_HOME
		def tomcat_dir = job."${pro}".tomcat_dir
		def remote_dir = job."${pro}".remote_dir
		ant.sequential {
			echo("begin deploy war : ${pro}")
			//delete old war file,shutdown tomcat			
			sshexec(host: host,
				username: ssh_acc,
				password: ssh_pwd,
				command: "${tomcat_dir}/bin/shutdown.sh ; rm -f ${tomcat_dir}/${pro}editor.war")
			
			//upload war file
			scp(file: "${tmpdir}/${pro}editor.war",
				todir: remote_dir,
				password: ssh_pwd,
				trust:"true")  //false:need knownhosts
			
			//upzip war file
			sshexec(host: host,
				username: ssh_acc,
				password: ssh_pwd,
				command: "cd ${tomcat_dir}/${pro}editor ; ${JAVA_HOME}/bin/jar -xf ${tomcat_dir}/${pro}editor.war")
			
			//start tomcat
			sshexec(host: host,
				username: ssh_acc,
				password: ssh_pwd,
				command: "${tomcat_dir}/bin/startup.sh")
			
			echo("deploy war done : ${pro}")
		}
	}
	
	static void main(args) {
		def dmtool = new dmtool()
		//read config file
		def config = new ConfigSlurper().parse(new File('src/myconfig.groovy').toURI().toURL())
		String tmpdir = config.tmpdir
		println tmpdir
		
		config.jobs.each { job ->  //beijing, baoding, aliyun
			job.deploy.each { pro ->
				def src = job."${pro}".src_dir
				dmtool.ant.echo("make ${pro}")
				//copy source
				dmtool.copyWeb(src, "${tmpdir}/${pro}")
				//copy maven dependencies
				dmtool.copyDependencies(src, "${tmpdir}/${pro}")
				//war
				dmtool.buildwar("${tmpdir}/${pro}")
				//deploy
				dmtool.deploywar(job, pro, tmpdir)
			}
		}
	}
}

 

配置文件

通过jobs和每个服务器的deploy设置部署哪个服务器,哪些Web服务

//jobs = [beijing, baoding, aliyun]
jobs = [beijing]
tmpdir = "D:/work/tmpdir"

beijing {
	host = "192.168.XXX.XXX"
	ssh_acc = "user"
	ssh_pwd = "xxxx"
	JAVA_HOME = "/usr/java/jdk1.7.0_21"
	deploy = ['web', 'path', 'area']
	web {
		src_dir = "D:/work/eclipse/workspace-jee-luna/webeditor"
		tomcat_dir = "/xxxx/xxxx/tomcat7"
		remote_dir = "${ssh_acc}@${host}:${tomcat_dir}"
	}
	path {
		src_dir = "D:/work/eclipse/workspace-jee-luna/patheditor"
		tomcat_dir = "/xxxx/xxxx/tomcat_path"
		remote_dir = "${ssh_acc}@${host}:${tomcat_dir}"
	}
	area {
		src_dir = "D:/work/eclipse/workspace-jee-luna/areaeditor"
		tomcat_dir = "/xxxx/xxxx/tomcat_area"
		remote_dir = "${ssh_acc}@${host}:${tomcat_dir}"
	}
}

baoding {
	host = "xx.xx.xxx.xxx"
	ssh_acc = "user"
	ssh_pwd = "xxxxxx"
	JAVA_HOME = "/usr/java/jdk1.7.0_21"
	deploy = ['web']
	web {
		src_dir = "D:/work/eclipse/workspace-jee-luna/bd-webeditor"
		tomcat_dir = "/xxxx/xxxx/tomcat_web_9000"
		remote_dir = "${ssh_acc}@${host}:${tomcat_dir}"
	}
	path {
		src_dir = "D:/work/eclipse/workspace-jee-luna/patheditor"
		tomcat_dir = "/xxxx/xxxx/tomcat_path_9010"
		remote_dir = "${ssh_acc}@${host}:${tomcat_dir}"
	}
}

aliyun {
	host = "xx.xx.xxxx.xxx"
	ssh_acc = "user"
	ssh_pwd = "xxxxx"
	JAVA_HOME = "/usr/java1.7/jdk1.7.0_21"
	deploy = ['path']
	web {
		src_dir = "D:/work/eclipse/workspace-jee-luna/webeditor"
		tomcat_dir = "/xxxx/xxxx/tomcat_web_9000"
		remote_dir = "${ssh_acc}@${host}:${tomcat_dir}"
	}
	path {
		src_dir = "D:/work/eclipse/workspace-jee-luna/patheditor"
		tomcat_dir = "/xxxx/xxxx/tomcat_path_9010"
		remote_dir = "${ssh_acc}@${host}:${tomcat_dir}"
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值