Gradle脚本部署SpringBoot应用到远程服务器

2021-03-09 08:28

阅读:619

标签:path   classpath   on()   groov   set   star   ola   org   har   

引入hidetake.ssh插件

buildscript {
    repositories {
        mavenLocal()
        maven { url "https://maven.aliyun.com/repositorys/public/" }
        maven { url "https://repo.spring.io/milestone" }
        maven {
            url ‘https://plugins.gradle.org/m2/‘
        }
    }
    dependencies {
        classpath ‘org.hidetake:gradle-ssh-plugin:2.6.0‘
    }
}
apply plugin: ‘org.hidetake.ssh‘

指定可执行Jar的主类

bootJar { // 允许打成可执行jar
    enabled = true
}
jar {
    manifestContentCharset ‘utf-8‘
    metadataCharset ‘utf-8‘
    manifest {
        attributes "Main-Class": "com.xxx.dataservice.api.ApiApplication" // 入口类路径
    }
    from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

设置远程服务器的信息

ssh.settings {  // 允许任何主机进行连接
    knownHosts = allowAnyHosts
}

remotes {
    deployServer { 
        host = ‘127.0.0.1‘
        user = ‘root‘
        password = ‘root‘
    }
}

设置服务器部署目录和执行的脚本

def remoteDir = ‘/opt/xxx/‘ // 服务器部署目录

def jarName = archivesBaseName + ‘-‘ + this.getVersion() + ‘.jar‘; // 打包的jar包的名称

// 以下代码是执行脚本,在jar包上传后,需要执行这段脚本来重启应用
// 当然你可以把这段脚本写在.sh文件中,之后把这个.sh文件放到服务器上执行
def shell = ‘‘‘
cd ‘‘‘+remoteDir+‘‘‘
APP_NAME=‘‘‘+jarName+‘‘‘
#重启命令
pid=`ps -ef | grep $APP_NAME | grep -v grep |awk ‘{print $2}‘`
if [ $pid ]; then
    echo :App  is  running pid=$pid
    kill -9 $pid
fi
nohup nohup java -jar -Dspring.profiles.active=test $APP_NAME > log.log & 
tail -f /opt/xxx/log.log
‘‘‘

编写部署脚本

task remoteDeploy(dependsOn: [clean, bootJar]) {
    bootJar.mustRunAfter clean
    doLast {
        ssh.run {
            session(remotes.getByName(‘deployServer‘)) {
                println ‘>>> start copying jar...‘
                put from: libsDir.toString() +‘\\‘+jarName, into: remoteDir // 将jar包上传上去
                println ‘>>> start app... ‘
                // 上传完成后执行重启脚本
                executeScript ‘‘‘
                ‘‘‘+shell+‘‘‘ 
                ‘‘‘
                //execute ‘sh ‘+remoteDir+‘/restart.sh‘
                println ‘>>> remote deploy OK...‘
            }
        }
    }
}

Gradle脚本部署SpringBoot应用到远程服务器

标签:path   classpath   on()   groov   set   star   ola   org   har   

原文地址:https://www.cnblogs.com/jlog/p/14184611.html


评论


亲,登录后才可以留言!