说明:
前后端分离的vue项目,为了解决跨域问题,需要部署到java web项目的resources/static目录下,打包流程:vue打包 >> copy resources >> java打包 >> 上传服务器 >> 环境预处理 >> 执行 >> 跟踪。
网上查看了大量资料,每一步都是坑,需要做这个的,具有很好的参考价值。
1、将以下脚本命名为deploy.sh,放在web项目和api项目所在目录的公共父级(方便找到路径),并配置相关参数
2、执行
chmod +x deploy.sh
./deploy.sh
脚本内容:
#!/bin/bash
# by labi3285
readonly name='???'
readonly version='0.0.1'
readonly web_path='/???'
readonly web_name='???'
readonly api_env_path='/???'
readonly web_env_path='/???/index'
readonly api_path='/???'
readonly api_jar_name='???.jar'
readonly serve_name='root'
readonly serve_port='8080'
readonly serve_passowrd='???'
readonly serve_host='???.???.???.???'
readonly serve_path="/???"
# 初始化
cd $(dirname $0)
_work_path=$(pwd) # 取到脚本目录
_web_path="${_work_path}${web_path}"
_web_dist_path="${_web_path}/dist"
_api_path="${_work_path}${api_path}"
_api_static_path="${_api_path}/src/main/resources/static"
_api_jar_file="${_api_path}/build/libs/${api_jar_name}"
_serve_project_path="${serve_path}/${name}"
_serve_project_jar_file="${_serve_project_path}/${version}.jar"
_serve_project_log_file="${_serve_project_path}/${version}.log"
_serve_project_start_sh="${_serve_project_path}/start.sh"
# def 远程服务器操作
auto_ssh(){
__host=$1
__name=$2
__password=$3
__todo=$4
/usr/bin/expect <<EOD
set timeout 86400
spawn ssh ${__name}@${__host} ${__todo}
expect {
"*yes/no*" {send "yes\r"; exp_continue}
"*password:" {send "$__password\r"; exp_continue}
"*Password:" {send "$__password\r"}
}
EOD
}
# def 上传远程服务器
auto_scp() {
__host=$1
__name=$2
__password=$3
__src=$4
__dest=$5
/usr/bin/expect <<EOD
set timeout 86400
spawn scp ${__src} ${__name}@${__host}:${__dest}
expect {
"*yes/no*" {send "yes\r"; exp_continue}
"*password:" {send "$__password\r"; exp_continue}
"*Password:" {send "$__password\r"}
}
EOD
}
# 打包 vue
echo -e "\033[32m vue 打包 \033[0m"
cd ${_web_path}
npm run build
# 复制vue架包到java
echo -e "\033[32m 复制vue架包到java \033[0m"
cp -a "${_web_dist_path}/static" ${_api_static_path}
cp -a "${_web_dist_path}/index.html" ${_api_static_path}
# 打包 api
echo -e "\033[32m java 打包 \033[0m"
cd ${_api_path}
gradle bootJar
# 上传服务器
echo -e "\033[32m vue 上传服务器 \033[0m"
auto_ssh ${serve_host} ${serve_name} ${serve_passowrd} "mkdir -p ${_serve_project_path}"
auto_scp ${serve_host} ${serve_name} ${serve_passowrd} ${_api_jar_file} ${_serve_project_jar_file}
# 配置服务脚本
echo -e "\033[32m 配置服务脚本 \033[0m"
touch .service_start_cache.sh
echo "#!/bin/bash" > .service_start_cache.sh
echo "kill -9 \$(netstat -nlp | grep :${serve_port} | awk '{ print \$7 }' | awk -F\"/\" '{ print \$1 }')" >> .service_start_cache.sh
echo "nohup java -jar ${_serve_project_jar_file} >> ${_serve_project_log_file} &" >> .service_start_cache.sh
echo "tail ${_serve_project_log_file}" >> .service_start_cache.sh
auto_scp ${serve_host} ${serve_name} ${serve_passowrd} .service_start_cache.sh ${_serve_project_start_sh}
# 启动服务
echo -e "\033[32m 启动服务 \033[0m"
auto_ssh ${serve_host} ${serve_name} ${serve_passowrd} "chmod +x ${_serve_project_start_sh}"
auto_ssh ${serve_host} ${serve_name} ${serve_passowrd} "${_serve_project_start_sh}"
# OK!!
echo -e "\033[32m 完成 \033[0m"
echo -e "\033[32m 上传jar路径:${_serve_project_jar_file} \033[0m"
echo -e "\033[32m log路径:${_serve_project_jar_file} \033[0m"
echo -e "\033[32m start脚本路径:${_serve_project_start_sh} \033[0m"
echo -e "\033[32m api测试地址:${serve_host}:${serve_port}${api_env_path} \033[0m"
echo -e "\033[32m web测试地址:${serve_host}:${serve_port}${web_env_path} \033[0m"