Shell脚本一键mvn发布war包 发表于 2019-07-02 | 分类于 Shell 字数统计: 824 字 | 阅读时长 ≈ 4 分钟 脚本初衷 客户想减少手动发布过程,减少部分工作量。但是客户不会使用jenkins,想直接脚本执行然后发布。 脚本发布步骤 登录github,拉取指定项目到本地 构建war包 停止tomcat服务 替换war包 启动tomcat服务 详细脚本123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195#!/bin/bash# author:key.liyk# version: v1.0# func: auto build war package[ -f /etc/init.d/functions ] && . /etc/init.d/functions[ $(id -u) != "0" ] && echo "You need use root to run this script" && exit 1clearcat <<EOF$(printf %.s# {1..44})# _ _ ## __ _ _ __ ___| |__ _ __ ___| |_ ## / _\ | '_ \ / __| '_ \| '_ \ / _ \ __| ## | (_| | | | | (__| | | | | | | __/ |_ ## \__,_|_| |_|\___|_| |_|_| |_|\___|\__| ## ## The script for maven deploy! #$(printf %.s+ {1..44})EOFcat <<EOF 1: Maven deploy 2: EXIT (or ctrl+c) EOF# 定义相关目录git_dir=/data/softapps #定义git拉取存放目录tomcat_dir=/data/tomcat9 #定义tomcat目录git_url="https://github.com/liyk1024/wartest.git" #定义自己github项目clone路径git_name=wartest #github项目名remove_dir=/tmp/remove-war #移除后war目录log_file=/tmp/install.log #定义日志目录mvn_path=/data/maven3/bin# 定义日志显示信息do_log() { local color=$1 local level=$2 shift # shift位置参数偏移 shift local msg=$@ local prefix="" if [[ $color -gt 30 ]]; then prefix="\033[1;$color;40m[$level]\033[0m" else prefix="[$level]" fi echo -e "$prefix [`date +'%Y-%m-%d %H:%M:%S'`] $msg"}log_info() { local msg=$@ do_log 0 INFO $msg}log_warn() { local msg=$@ do_log 33 WARN $msg}log_error() { local msg=$@ do_log 31 ERROR $msg}log_success() { local msg=$@ do_log 32 SUCCESS $msg}# 检查目录check_dir() {log_info check directory...for dir in $*;do [ ! -d $dir ] && mkdir -p $dir &>> $log_filedone[ $? -eq 0 ] && log_success check over}# 检查基础命令check_command() {log_info check basic commands...for cmd in $*;do hash $cmd &>> $log_file if [ $? -ne 0 ];then action "${cmd} check" /bin/false log_info ${cmd} not find,installing... yum -y install $cmd &>> $log_file else action "${cmd} check" /bin/true fidone[ $? -eq 0 ] && log_success check over}# 检查javacheck_java() {log_info check java...hash java &>> $log_fileif [ $? -eq 0 ];then action "java check" /bin/trueelse action "java check" /bin/false log_error Pls install java! exit 1fi}# 检查mavencheck_mvn() {log_info check maven...hash mvn &>> $log_fileif [ $? -ne 0 ];then action "maven check" /bin/trueelse action "maven check" /bin/false log_error Pls install maven! exit 1fi}# 拉取代码git_pull() {log_info check git project...find ${git_dir} -type d -name ${git_name} |xargs rm -rvf &>> $log_filelog_info git clone...cd ${git_dir}git clone $git_url &>> $log_file[ $? -eq 0 ] && log_success git pull over! }# 构建war包build() {log_info begin build warPackage...cd ${git_dir}/${git_name}if [ -f pom.xml ];then ${mvn_path}/mvn clean package -Dmaven.test.skip=true &>> $log_file war=`find ${git_dir}/${git_name} -name "*.war" |wc -l` if [ $war -eq 1 ];then log_success build war success! else log_error build war fail! exit 1 fielse log_warn Pls check,not find pom.xml! exit 1fi}# 停止tomcatstop_tomcat() {sh ${tomcat_dir}/bin/shutdown.sh &>> $log_file[ $? -eq 0 ] && log_success stop_tomcat over!}# 替换war包remove_war() {cd ${tomcat_dir}/webappsif [ -d ROOT ];then rm -rf ROOTfiwar_url=`find ${git_dir}/${git_name} -name "*.war"`cp ${war_url} ${tomcat_dir}/webapps/ROOT.war}# 启动tomcatstart_tomcat() {sh ${tomcat_dir}/bin/startup.sh &>> $log_file[ $? -eq 0 ] && log_success startup_tomcat over!}main() {check_dir $git_dir $remove_dircheck_command git wgetcheck_javacheck_mvngit_pullbuildstop_tomcatremove_warstart_tomcat}# 根据选择操作read -p "Please input your choice:" choiceif [ $choice = 1 ];then echo "" echo -e "You can view detail use: \033[5;33mtail -f ${log_file}\033[0m" log_info Start Maven deploy... mainelif [[ $choice = 2 ]];then log_info Your choice Exit! exit 0else log_warn pls choice 1|2 exit 1fi -------------本文结束感谢您的阅读------------- 原创技术分享,感谢您的支持。 打赏 微信支付 支付宝