Gradle學(xué)習(xí)教程之部署上傳項(xiàng)目詳解
前言
Gradle,這是一個(gè)基于 JVM 的富有突破性構(gòu)建工具。Gradle 正迅速成為許多開(kāi)源項(xiàng)目和前沿企業(yè)構(gòu)建系統(tǒng)的選擇,同時(shí)也在挑戰(zhàn)遺留的自動(dòng)化構(gòu)建項(xiàng)目。
原先在公司做項(xiàng)目時(shí),寫(xiě)了一個(gè)簡(jiǎn)單的基于gradle部署項(xiàng)目的腳本,今天翻出來(lái)記錄一下,下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧。
方法如下:
一、build.gradle
buildscript { ext { env = System.getProperty("env") ?: "test" jvmArgs = "-server -Xms128m -Xmx128m -XX:NewRatio=4 -XX:SurvivorRatio=16 -XX:MaxTenuringThreshold=15 -XX:CMSInitiatingOccupancyFraction=80 -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:+ExplicitGCInvokesConcurrent -XX:+DoEscapeAnalysis -XX:-HeapDumpOnOutOfMemoryError" if (env == "prod") { jvmArgs = "-server -Xms2g -Xmx2g -XX:NewRatio=4 -XX:SurvivorRatio=16 -XX:MaxTenuringThreshold=15 -XX:CMSInitiatingOccupancyFraction=80 -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:+ExplicitGCInvokesConcurrent -XX:+DoEscapeAnalysis -XX:-HeapDumpOnOutOfMemoryError" } userHome = System.getProperty("user.home") osName = System.getProperty("os.name") } repositories { jcenter() } dependencies { classpath 'org.hidetake:gradle-ssh-plugin:2.7.0' classpath 'co.tomlee.gradle.plugins:gradle-thrift-plugin:0.0.6' } } allprojects { apply plugin: 'idea' apply plugin: 'eclipse' apply plugin: 'org.hidetake.ssh' group = 'com.mwee.information.core' version = '1.0-SNAPSHOT' ssh.settings { timeoutSec = 60 knownHosts = allowAnyHosts } defaultTasks 'clean', 'copyPartDependencies' //排除Log4j依賴 configurations { compile.exclude module: 'slf4j-log4j12' compile.exclude module: 'org.apache.logging.log4j' compile.exclude module: 'log4j' all*.exclude group: 'org.apache.logging.log4j' all*.exclude group: 'log4j' } } subprojects { apply plugin: 'java' sourceCompatibility = 1.8 targetCompatibility = 1.8 repositories { mavenLocal() maven { url "http://114.80.88.52:9001/nexus/content/groups/public/" } } sourceSets { main { java { srcDirs = ['src/main/java'] } resources { srcDirs = ["src/main/resources", "src/main/profile/$env"] } } } dependencies { compile("org.codehaus.groovy:groovy-all:2.2.1") compile 'org.codehaus.groovy:groovy-backports-compat23:2.4.5' compile("org.springframework.boot:spring-boot-starter-web:1.4.2.RELEASE") compile("org.apache.commons:commons-lang3:3.4") compile("org.apache.commons:commons-collections4:4.1") compile "org.apache.commons:commons-pool2:2.4.2" compile group: 'com.alibaba', name: 'fastjson', version: '1.2.12' // https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.8.6' // https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.8.6' compile group: 'org.aspectj', name: 'aspectjrt', version: '1.8.7' compile group: 'org.aspectj', name: 'aspectjweaver', version: '1.8.7' compile group: 'com.thoughtworks.xstream', name: 'xstream', version: '1.4.1' compile(group: 'org.mortbay.jetty', name: 'jetty', version: '6.1.26') compile group: 'org.projectlombok', name: 'lombok', version: '1.16.8' compile group: 'com.squareup.okhttp', name: 'okhttp', version: '2.7.5' compile group: 'com.google.guava', name: 'guava', version: '18.0' compile group: 'commons-lang', name: 'commons-lang', version: '2.6' compile group: 'com.jcraft', name: 'jsch', version: '0.1.53' testCompile group: 'junit', name: 'junit', version: '4.12' testCompile "org.springframework:spring-test:4.3.4.RELEASE" compile "javax.validation:validation-api:1.1.0.Final" compile "org.hibernate:hibernate-validator:5.2.4.Final" } //gradle utf-8 compile tasks.withType(JavaCompile) { options.encoding = 'UTF-8' } task copyAllDependencies(type: Copy, dependsOn: jar) { description = "拷貝全部依賴的jar包" from configurations.runtime into 'build/libs' } task copyPartDependencies(type: Copy, dependsOn: jar) { description = "拷貝部分依賴的jar" from configurations.runtime into 'build/libs' doLast { file("build/libs").listFiles({ !it.name.endsWith("-SNAPSHOT.jar") } as FileFilter).each { it.delete() } } } }
二、對(duì)應(yīng)模塊下的build.gradle
def mainClass = "com.hzgj.information.rest.user.run.UserServiceProvider" def appHome = "/home/appsvr/apps/rest_user" def javaCommand = "nohup java $jvmArgs -Djava.ext.dirs=$appHome/libs -Denv=$env $mainClass >$appHome/shell.log 2>&1 &" def index = System.getProperty("index") def remote = remotes { test_0 { role 'test_0' host = '10.0.21.152' if (file("$userHome/.ssh/id_rsa").exists()) { user = 'appsvr' identity = file("$userHome/.ssh/id_rsa") } else { user = 'appsvr' password = 'xxx' } } test_1 { role 'test_1' host = '10.0.146.20' if (file("$userHome/.ssh/id_rsa").exists()) { user = 'appsvr' identity = file("$userHome/.ssh/id_rsa") } else { user = 'appsvr' password = 'xxx' } } home { role 'home' host = '192.168.109.130' user = 'appsvr' password = 'xxx' // identity = file('id_rsa') } } task deploy << { description = "拷貝jar包并啟動(dòng)java服務(wù)" def roles = remote.findAll { def currentEnv = index == null ? "$env" : "$env" + "_" + index it['roles'][0].toString().contains(currentEnv) } ssh.run { roles.each { def role = it['roles'][0].toString() session(remotes.role(role)) { try { execute("ls $appHome") } catch (Exception e) { println("#############目錄[$appHome]不存在,將自動(dòng)創(chuàng)建############") execute("mkdir -p $appHome") } finally { def r = '$1' def pid = execute("jps -l |grep '$mainClass' |awk \'{print $r}\'") if (pid) { execute("kill -9 $pid") } put from: 'build/libs', into: "$appHome" println("###############準(zhǔn)備啟動(dòng)java服務(wù)[$javaCommand]####################") execute("$javaCommand") sleep(10000) pid = execute("jps -l |grep '$mainClass' |awk \'{print $r}\'") if (pid) { println("#####$mainClass [$pid] 啟動(dòng)成功...######") execute("rm -f $appHome/shell.log") } else { println("#$mainClass 啟動(dòng)失敗...輸出日志如下:#") execute("cat $appHome/shell.log") } } } } } } task stop << { def roles = remote.findAll { def currentEnv = index == null ? "$env" : "$env" + "_" + index it['roles'][0].toString().contains(currentEnv) } ssh.run { roles.each { session(remotes.role("$env")) { def r = '$1' def pid = execute("jps -l |grep '$mainClass' |awk \'{print $r}\'") if (pid) { execute("kill -9 $pid") } } } } } task start << { def roles = remote.findAll { def currentEnv = index == null ? "$env" : "$env" + "_" + index it['roles'][0].toString().contains(currentEnv) } ssh.run { roles.each { def role = it['roles'][0].toString() session(remotes.role(role)) { def r = '$1' def pid = execute("jps -l |grep '$mainClass' |awk \'{print $r}\'") if (pid) { execute("kill -9 $pid") } println("###############準(zhǔn)備啟動(dòng)java服務(wù)[$javaCommand]####################") execute("$javaCommand") sleep(10000) pid = execute("jps -l |grep '$main Class' |awk \'{print $r}\'") if (pid) { println("#$mainClass [$pid] 啟動(dòng)成功...#") execute("rm -f $appHome/shell.log") } else { println("#$mainClass 啟動(dòng)失敗...輸出日志如下:#") execute("cat $appHome/shell.log") } } } } }
三、使用方式
1.先運(yùn)行gradle copyAll -x test
進(jìn)行打包操作,該操作會(huì)將該模塊所有的依賴的jar
2.進(jìn)入到對(duì)應(yīng)的模塊下 運(yùn)行gradle deploy -Denv=xxx -Dindex=xxx
,什么意思呢?-Denv代表哪一個(gè)環(huán)境 -Dindex指定該環(huán)境下哪個(gè)節(jié)點(diǎn)進(jìn)行發(fā)布
3.gradle start -Denv=xxx -Dindex=xxx
運(yùn)行當(dāng)前環(huán)境下的應(yīng)用
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
DownloadManager實(shí)現(xiàn)文件下載功能
這篇文章主要為大家詳細(xì)介紹了DownloadManager實(shí)現(xiàn)文件下載功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11Android?Studio開(kāi)發(fā)實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了Android?Studio開(kāi)發(fā)實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05基于popupWindow實(shí)現(xiàn)懸浮半透明效果
這篇文章主要為大家詳細(xì)介紹了基于popupWindow實(shí)現(xiàn)懸浮半透明效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04Android實(shí)現(xiàn)底部彈出按鈕菜單升級(jí)版
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)底部彈出按鈕菜單的升級(jí)版,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10android短信管理器SmsManager實(shí)例詳解
這篇文章主要為大家詳細(xì)介紹了android短信管理器SmsManager實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11Android 編譯出錯(cuò)版本匹配問(wèn)題解決辦法
這篇文章主要介紹了Android 編譯出錯(cuò) app\build\intermediates\res\merged\debug\values-v23\values-v23.xml 的問(wèn)題解決辦法,需要的朋友可以參考下2017-07-07