利用gradle脚本自动编译部署到远程服务器,详细见下:
/*
* This file was generated by the Gradle 'init' task.
*/
buildscript {
repositories {
jcenter()
}
//ssh插件
dependencies {
classpath 'org.hidetake:gradle-ssh-plugin:2.9.0'
}
}
plugins {
id 'java'
id 'war'
}
apply plugin:'org.hidetake.ssh'
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web:2.3.1.RELEASE'
implementation 'org.springframework.boot:spring-boot-starter-data-redis:2.3.1.RELEASE'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.1'
}
war {
archiveName 'XXXX.war'
}
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
//设置为任意host
ssh.settings {
knownHosts = allowAnyHosts
}
//远程信息
remotes {
deployServer {
host = '192.168.***.***'
port = 2222
user = 'root'
password = '123456'
}
}
//停止tomcat服务
task shutdownTomcat() {
doLast{
ssh.run {
session(remotes.deployServer) {
println 'shut down tomcat...'
execute 'service tomcat8 stop'
}
}
}
}
//删除webapps中的文件
task del(dependsOn:shutdownTomcat) {
doLast{
ssh.run {
session(remotes.deployServer) {
println 'start deleting...'
executeScript '''#!/bin/sh
rm -rf /var/lib/tomcat8/webapps/hs-device-platform
rm -f /var/lib/tomcat8/webapps/hs-device-platform.war
'''
}
}
}
}
//拷贝war包到远程服务器
task copy(dependsOn:del) {
doLast{
ssh.run {
session(remotes.deployServer) {
println 'start copying war...'
put from: buildDir.toString() + '/libs/hs-device-platform.war', into: '/var/lib/tomcat8/webapps/hs-device-platform.war'
}
}
}
}
//启动tomcat
task deploy(dependsOn:copy) {
doLast{
ssh.run {
session(remotes.deployServer) {
println 'start tomcat...'
execute 'service tomcat8 start'
}
}
}
}
//输出日志
task printLogs(){
doLast{
ssh.run{
session(remotes.deployServer) {
println 'print logs...'
execute 'tail -f /var/lib/tomcat8/logs/catalina.out'
}
}
}
}