gradle之ssh插件

诸葛砚文
2023-12-01

以前每次在本地编译完后,需要手动复制部署文件到远程机器,但是复制次数多后,也比较懒了,在网上找找有没有自动远程部署的。通过搜索发现可以使用ssh plugin。完整的文档手册可以参考https://gradle-ssh-plugin.github.io/docs/


下面是我自己的一个例子:

build.gradle


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

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

compileJava.options.encoding = 'UTF-8'

repositories {
    jcenter()
    mavenCentral()
}

dependencies {

    // https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12
    compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.5'

    // https://mvnrepository.com/artifact/log4j/log4j
    compile group: 'log4j', name: 'log4j', version: '1.2.17'

    testCompile group: 'junit', name: 'junit', version: '4.11'

}

ssh.settings {
    knownHosts = allowAnyHosts
}

remotes {
    smsserver_1 {
        host = '<ip_address>'
        port = 22
        user = "root"
        password = "<password>"
    }
}

task deployWar(dependsOn: war) << {
    ssh.run {
        session(remotes.smsserver_1) {
            // Deploy War
            put from: '$buildDir/libs/xxx.war', into: '$REMOTE_APP_DIR/webapps'

            execute 'ls $REMOTE_APP_DIR/webapps'
        }
    }
}


 类似资料: