Detailed Explanation of Deployment Upload Project for Gradle Learning Tutorial

  • 2021-09-05 00:53:53
  • OfStack

Preface

Gradle, a breakthrough building tool based on JVM. Gradle is rapidly becoming the choice of many open source projects and cutting-edge enterprise building systems, while also challenging legacy automated building projects.

Originally in the company to do the project, wrote a simple project based on gradle deployment of the script, today turned out to record 1, the following words do not say much, to a look at the details of the introduction.

The method is as follows:

1. 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'

 // Exclude Log4j Dependency 
 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 = " Copy all dependent jar Bag "
 from configurations.runtime
 into 'build/libs'
 }

 task copyPartDependencies(type: Copy, dependsOn: jar) {
 description = " Copy partially dependent jar"
 from configurations.runtime
 into 'build/libs'
 doLast {
 file("build/libs").listFiles({ !it.name.endsWith("-SNAPSHOT.jar") } as FileFilter).each {
 it.delete()
 }
 }
 }
}

2. build. gradle under the corresponding module


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 = " Copy jar Package and start java Services "
 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("############# Directory [$appHome] Nonexistent , Will automatically create ############")
 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("############### Ready to start java Services [$javaCommand]####################")
 execute("$javaCommand")
 sleep(10000)
 pid = execute("jps -l |grep '$mainClass' |awk \'{print $r}\'")
 if (pid) {
 println("#####$mainClass [$pid]  Startup succeeded ...######")
 execute("rm -f $appHome/shell.log")
 } else {
 println("#$mainClass  Startup failure ... The output log is as follows :#")
 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("############### Ready to start java Services [$javaCommand]####################")
 execute("$javaCommand")
 sleep(10000)
 pid = execute("jps -l |grep '$main Class' |awk \'{print $r}\'")
 if (pid) {
 println("#$mainClass [$pid]  Startup succeeded ...#")
 execute("rm -f $appHome/shell.log")
 } else {
 println("#$mainClass  Startup failure ... The output log is as follows :#")
 execute("cat $appHome/shell.log")
 }
 }
 }
 }
}

3. How to use it

Step 1 Run first gradle copyAll -x test Performs a package operation, which will put all the dependent jar of the module

2. Enter the corresponding module to run gradle deploy -Denv=xxx -Dindex=xxx What do you mean? -Denv represents which 1 environment-Dindex specifies which node in that environment to publish

3. gradle start -Denv=xxx -Dindex=xxx Run the application in the current environment

Summarize


Related articles: