gradle 自定义task 实现发版

鄢禄
2023-12-01

1.使用org.hidetake.ssh,通过自定义 task 实现。

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'org.hidetake:gradle-ssh-plugin:2.9.0'
    }
}

apply plugin: 'java'
apply plugin: 'org.hidetake.ssh'
apply plugin: 'war'

war {
    archiveName 'test.war'
}

ssh.settings {
    knownHosts = allowAnyHosts
}

//配置远程服务器信息
remotes {
    deployServer {
        host = 'ip地址'
        user = '登陆用户'
        password = '密码'
    }
}

//关闭tomcat
task shutdownTomcat(dependsOn: build) << {
    ssh.run {
        session(remotes.deployServer) {
            println 'shut down tomcat...'
            executeScript '''#!/bin/sh  
                        cd /home/user/tomcat/bin/  
                        ./shutdown.sh  
                    '''
        }
    }
}
//删除旧war包
task del(dependsOn:shutdownTomcat) << {
    ssh.run {
        session(remotes.deployServer) {
            println 'start deleting...'
            executeScript '''#!/bin/sh  
                        rm -f /home/user/tomcat/webapps/test.war
                        rm -rf /home/user/tomcat/webapps/test
                    '''
            println 'delete old war'
        }
    }
}

task copy(dependsOn:del) << {
    ssh.run {
        session(remotes.deployServer) {
            println 'start copying war...'
            put from: buildDir.toString() + '/libs/test.war', into: '/home/user/tomcat/webapps'
            println '上传成功'
        }
    }
}

task deploy(dependsOn:copy) << {
    ssh.run {
        session(remotes.deployServer) {
            println 'start tomcat...'
            execute '/home/user/tomcat/bin/startup.sh'
            println 'done'
        }
    }
}

2.放到windows 某tomcat下,使用 copy

war {
    archiveName 'test.war'
}

//复制任务
def dir = "C:\\Users\\zzw\\Desktop\\ssh"
task(copy, type: Copy) {
    from(file('build/libs/test.war'))
    into(dir)
}
 类似资料: