应用maven自动部署的脚本

@(编程)

最近写了一个自动部署的脚本,可以一键部署到测试服务器或者生产服务器上,包括一个函数脚本和一个调用脚本,比较简单,记录如下。

特点如下:

  • 部署前自动备份
  • 可以部署tomcat项目和java项目
  • 支持参数,可以部署测试环境和生产环境

function.sh

#!/bin/bash
##author:zch
##date:20171116

host=192.168.163.50

if [ "$1"x = "online"x ]; then
host=111.111.111.111
echo "host is 111.111.111.111"
else
host=192.168.163.50
echo "host is 192.168.163.50"
fi
##远程调用,传入参数
remote_call(){
ssh -tt [email protected]$host << remotessh
$1
exit
remotessh
}

##更新、编译代码,参数是名称,如admin或者app_server
build_code(){
git checkout $1_online
git pull
mvn clean install dependency:copy-dependencies
echo "build code success"
}

##启动远程tomcat,参数是名称,如admin或者app_server
start_remote_process(){
remote_call "/home/hhtd/bin/start_$1.sh"
}

##杀掉远程tomcat,参数是名称,如admin或者app_server
kill_remote_process(){
remote_call "/home/hhtd/bin/kill_$1.sh"
}

##备份远程tomcat,参数是名称,如admin或者app_server
backup_remote(){
remote_call "/home/hhtd/bin/backup_$1.sh"
}

##删除远程tomcat日志,参数是全路径,如/home/hhtd/online/admin-tomcat-8082/
rm_tomcat_logs(){
remote_call "rm -rf $1/logs/*.*"
}

##删除远程的release目录下的root,参数是名称,如admin或者app_server
rm_remote_release_root(){
remote_call "rm -rf /home/hhtd/release/hhtd_$1/ROOT"
}

##把本地ROOT复制到远程release目录下,参数是名称,如admin或者app_server
scp_root_remote(){
scp -r ./hhtd_$1/target/ROOT [email protected]$host:/home/hhtd/release/hhtd_$1/ROOT
}

##把本地文件复制到远程,适用于非web项目,包括两个参数,第一个是名称,第二个是全路径
scp_release_remote(){
remote_call "mkdir $2"
remote_call "mkdir $2/bin"
remote_call "mkdir $2/lib"
remote_call "mkdir $2/config"
remote_call "mkdir $2/logs"
scp -r ./release/hhtd_$1/bin/*.* [email protected]$host:$2/bin/
scp -r ./release/hhtd_$1/lib/*.* [email protected]$host:$2/lib/
}

##删除远程服务器上的tomcat下的root,参数是全路径,如/home/hhtd/online/admin-tomcat-8082/
rm_tomcat_root(){
remote_call "rm -rf $1/webapps/ROOT"
}

##备份远程服务器上tomcat下面的attach(attache)文件夹,参数是全路径,如/home/hhtd/online/admin-tomcat-8082/
backup_tomcat_attach(){
remote_call "mv $1/webapps/ROOT/attache $1/webapps/attache"
}
##把release目录下的root复制到online目录下,包括两个参数,第一个是名称,第二个是全路径
cp_tomcat_release_online(){
remote_call "cp -R /home/hhtd/release/hhtd_$1/ROOT $2/webapps/ROOT"
}

##把备份的tomcat下的attach目录恢复回去
recover_tomcat_attach(){
remote_call "mv $1/webapps/attache $1/webapps/ROOT/attache"
}

##备份tomcat配置文件,参数是全路径,如/home/hhtd/online/admin-tomcat-8082/
backup_tomcat_config(){
remote_call "cp $1/webapps/ROOT/WEB-INF/classes/ $1/*.properties "
}

##把配置文件复制回去,参数是全路径,如/home/hhtd/online/admin-tomcat-8082/
recover_tomcat_config(){
remote_call "cp $1/*.properties $1/webapps/ROOT/WEB-INF/classes/"
}

##复制依赖jar文件到release目录
copy_dependencies_jar(){
rm -f ./release/hhtd_$1/lib/*.jar
cp ./hhtd_$1/target/dependency/*.jar ./release/hhtd_$1/lib/
cp ./hhtd_$1/target/*hhtd_$1*.jar ./release/hhtd_$1/lib/
}

##给sh执行权限,参数是名称,适用于非web项目
chmod_x(){
remote_call "chmod +x /home/hhtd/online/hhtd_$1/bin/start.sh"
}

##适用于非web项目,参数是名称
backup_remote_config(){
remote_call "rm -rf /home/hhtd/tmp/"
remote_call "mkdir /home/hhtd/tmp/"
remote_call "cp /home/hhtd/online/hhtd_$1/config/*.* /home/hhtd/tmp/"
}

##适用于非web项目,参数是路径
rm_remote_folder(){
remote_call "rm -rf $1"
}
##适用于非web项目,参数是路径
recover_config(){
remote_call "cp /home/hhtd/tmp/*.* $1/config/"
}

##适用于web项目,参数是路径
rm_tomcat_config(){
remote_call "rm $1/webapps/ROOT/WEB-INF/classes/*.properties"
}

admin.sh

#!/bin/bash
##author:zch
##date:20171116

time1=`date +"%s"`

name=admin
folder=admin-tomcat-8082
fullPath=/home/hhtd/online/$folder

source function.sh

## build source code
build_code $name

## remote rm files scp to online
rm_remote_release_root $name
scp_root_remote $name

##kill tomcat
kill_remote_process $name

## backup tomcat
backup_remote $name

## rm tomcat logs
rm_tomcat_logs $fullPath

##rm ROOT
backup_tomcat_attach $fullPath
rm_tomcat_root $fullPath
echo "root deleted"

##cp ROOT
cp_tomcat_release_online $name $fullPath
recover_tomcat_attach $fullPath
echo "cp root finished"

##rm tomcat config
rm_tomcat_config $fullPath

##cp config
recover_tomcat_config $fullPath

##start tomcat
start_remote_process $name

time2=`date +"%s"`
((total=$time2-$time1))
echo "deploy finished total time is "$total seconds
时间: 2024-07-31 11:09:58

应用maven自动部署的脚本的相关文章

【原创】jenkins+maven自动部署脚本

jenkins+maven自动部署脚本 2019-02-01 09:34:38 前提,所有主机都做了免密登录操作,相信下面的版本大家应该可以看得明白,我是两台服务器,但只放开了一个服务器的配置,原理很简单: 1.配置一些经常变化的变量 2.进入svn下载下来的源代码空间目录,然后编译 3.远程备份文件(很重要) 4.进入编译后的文件夹target,将文件拷贝至远程生产主机 5.远程重启服务,搞定! #!/bin/bash source /etc/profile #基础环境变量 SERVER_23

(转)maven自动部署web项目到tomcat8(向下兼容7)

maven自动部署web项目到tomcat8(向下兼容7) 2014-08-29 10:52 网站上线以后,为了保证网站运行的连续性,有新功能更新时,不能重启Tomcat服务器去部署新增功能.因此,就研 究一下用maven的自动部署功能. 1首先要保证自己电脑上装了Tomcat服务器 进入Tomcat安装目录,打开..\conf\tomcat-users.xml,在这个配置文件中加入以下内容: <role rolename="manager-gui"/> <role

maven 自动部署war(项目)到tomcat

1.软件版本 apache-tomcat-7.0.55 apache-maven-3.2.2 eclipse-standard-luna-R-win32-x86_64 2.修改文件 2.1 tomcat  tomcat-users.xml 增加如下用户 角色 manager-script manager-script - Access to the tools-friendly plain text interface that is described in this document, an

使用Maven自动部署Java Web项目到Tomcat问题小记

导读 首先说说自己为啥要用maven管理项目,一个直接的原因是:我在自己电脑上开发web项目,每次部署到服务器上时都要经历如下步骤: 首先在Eclipse里将项目打包成war包 将服务器上原来的项目文件夹删掉 cd /var/lib/tomcat7/webapps sudo rm XXX.war sudo rm -rf XXX 将war包传到服务器上,比如用pscp命令上传 pscp -pw "xxx" XXX.war [email protected]:/var/lib/tomcat

maven自动部署项目以及常见问题解决

Maven自动部署war到Tomcat1. 在maven项目的pom里配置如下信息 <build> <finalName>dianxiao</finalName> <!-- WAR包的名字 --> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>tomcat-maven-plugin</artifactId

maven自动部署到tomcat的问题

最近需要使用Maven将项目自动部署到Tomcat,在网络上也查找了很多文章,内容大同小异,今天打算在这里给自己做一个小总结 参考网址:http://blog.csdn.net/dilaomimi/article/details/6955850 我使用的环境是:Eclipse Java EE IDE for Web Developers(Helios Service Release 1) apache-tomcat-6.0.10 apache-maven-2.2.1 通常的做法是这样的 1.配置

Maven自动部署war到Tomcat6

Maven自动部署war到Tomcat 软件版本:tomcat6.maven3 1.  在maven项目的pom里配置如下信息 <build> <finalName>spring_web</finalName> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>tomcat-maven-plugin</artifactI

maven 自动部署到 tomcat7

多方搜索,终于使maven项目可以自动发布到tomcat下了. tomcat7 需要使用 tomcat-maven-plugin 的新版本,版本支持tomcat6和tomcat7,groupId也由org.codehaus.mojo改为org.apache.tomcat.maven.  可以参考看看:http://tomcat.apache.org/maven-plugin.html 主菜来了. 1.修改项目的pom.xml a.在project节点下 添加tomcat-maven-plugin

自动部署LNMP脚本

最近抽时间写了一份LNMP部署脚本,使用源码安装所需软件,源码软件包网络上很容易获取,这里仅贴出脚本内容,大家可以自行在网络上下载对应的软件放在脚本当前目录即可,实际下载的软件包如果与脚本所调用的软件版本号及压缩格式有差异时,可以修改脚本开始的变量定义即可.        脚本会检测目标主机的语音环境,如果目标主机运行中文环境,则脚本运行中的所有提示信息均为中文,反之则提示信息为英文.脚本在安装相关软件的依赖包时会调用YUM安装对应的软件,运行脚本前确认YUM是可用的,否则脚本检测无YUM源可用