Gradle学习教程之部署上传项目详解

 更新时间:2018年04月24日 08:31:31   作者:洛阳融科聂晨  
这篇文章主要给大家介绍了关于Gradle学习教程之部署上传项目的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。

前言

Gradle,这是一个基于 JVM 的富有突破性构建工具。Gradle 正迅速成为许多开源项目和前沿企业构建系统的选择,同时也在挑战遗留的自动化构建项目。

原先在公司做项目时,写了一个简单的基于gradle部署项目的脚本,今天翻出来记录一下,下面话不多说了,来一起看看详细的介绍吧。

方法如下: 

一、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()
 }
 }
 }
}

二、对应模块下的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包并启动java服务"
 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]不存在,将自动创建############")
 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("###############准备启动java服务[$javaCommand]####################")
 execute("$javaCommand")
 sleep(10000)
 pid = execute("jps -l |grep '$mainClass' |awk \'{print $r}\'")
 if (pid) {
 println("#####$mainClass [$pid] 启动成功...######")
 execute("rm -f $appHome/shell.log")
 } else {
 println("#$mainClass 启动失败...输出日志如下:#")
 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("###############准备启动java服务[$javaCommand]####################")
 execute("$javaCommand")
 sleep(10000)
 pid = execute("jps -l |grep '$main Class' |awk \'{print $r}\'")
 if (pid) {
 println("#$mainClass [$pid] 启动成功...#")
 execute("rm -f $appHome/shell.log")
 } else {
 println("#$mainClass 启动失败...输出日志如下:#")
 execute("cat $appHome/shell.log")
 }
 }
 }
 }
}

三、使用方式

1.先运行gradle copyAll -x test 进行打包操作,该操作会将该模块所有的依赖的jar

2.进入到对应的模块下 运行gradle deploy -Denv=xxx -Dindex=xxx ,什么意思呢?-Denv代表哪一个环境 -Dindex指定该环境下哪个节点进行发布

3.gradle start -Denv=xxx -Dindex=xxx 运行当前环境下的应用

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

相关文章

  • Android Toast使用的简单小结(推荐)

    Android Toast使用的简单小结(推荐)

    这篇文章主要介绍了Android Toast使用的简单小结,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-03-03
  • Android中Market的Loading效果实现方法

    Android中Market的Loading效果实现方法

    这篇文章主要介绍了Android中Market的Loading效果实现方法,较为详细的分析了Android中loading效果的相关布局及功能实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-10-10
  • 源码剖析Android中Okio的使用

    源码剖析Android中Okio的使用

    这篇文章主要将从源码出发,带大家剖析一下Android中Okio的具体使用,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以了解一下
    2023-02-02
  • Android 自定义ContentProvider简单实例

    Android 自定义ContentProvider简单实例

    这篇文章主要介绍了Android 自定义ContentProvider简单实例的相关资料,需要的朋友可以参考下
    2017-06-06
  • Android仿微信支付密码弹出层功能

    Android仿微信支付密码弹出层功能

    最近项目中使用到了支付密码功能,感觉这类界面应该是比较常用的,涉及支付密码的输入的一般都会用到吧,所以单独地把这部分抽取出来,有需要的朋友可以拿去用用
    2017-04-04
  • Android源代码仓库及其管理工具Repo分析详解

    Android源代码仓库及其管理工具Repo分析详解

    本篇文章主要介绍了Android源代码仓库及其管理工具Repo分析详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01
  • Android主题切换之探究白天和夜间模式

    Android主题切换之探究白天和夜间模式

    所谓的主题切换,就是能够根据不同的设定,呈现不同风格的界面给用户,也就是所谓的换肤。下面跟小编一起来看看怎么实现Android主题切换中的白天和夜间模式。
    2016-07-07
  • Android属性动画实现布局的下拉展开效果

    Android属性动画实现布局的下拉展开效果

    这篇文章主要为大家详细介绍了Android属性动画实现布局的下拉展开效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-07-07
  • Android+SQLite数据库实现的生词记事本功能实例

    Android+SQLite数据库实现的生词记事本功能实例

    这篇文章主要介绍了Android+SQLite数据库实现的生词记事本功能,结合具体实例形式分析了Android操作SQLite数据库实现生词记录功能的操作步骤与相关注意事项,需要的朋友可以参考下
    2017-09-09
  • android中TabHost的图标(48×48)和文字叠加解决方法

    android中TabHost的图标(48×48)和文字叠加解决方法

    开发过程中,有时候图标稍微大点,比如48×48的时候,文字就会和图标叠加起来,遇到这种问题我们该怎样处理呢?本文将详细介绍希望对你有所帮助
    2013-01-01

最新评论