原先在公司做项目时,写了一个简单的基于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 运行当前环境下的应用

Gradle学习之部署上传项目的更多相关文章

  1. Git学习-上传项目到github

    现在流行把项目代码上传到git上,今天试了好久,终于成功上传到git了,特做点笔记. 准备工作 在github上注册一个账号,创建一个仓库. 创建好仓库,得到它的地址: 开始上传 一.新建一个文件夹, ...

  2. git上传项目代码到github

    参考: git学习——上传项目代码到github github上传时出现error: src refspec master does not match any解决办法 git 上传本地文件到gith ...

  3. GitHub教程--上传项目四步法 GitBash命令行下使用方法

    之前就用过GitHub,感觉用GitHub托管自己的代码非常不错.可是之前用的都是窗口化的TortoiseGit,省了很多命令行的操作,但是个人非常喜欢使用命令行,于是,今天就试着用了用GitBash ...

  4. Windows下上传项目到github

    首先,一定要有耐心.看到一大堆的命令行(其实并没有一大堆)不要觉得枯燥,最后当你成功把你的项目上传上去之后那种胜利的成果,还是挺有意思的.本人第一次写博客,勿喷. 我写的是主要的流程,详细内容还请移步 ...

  5. 手把手教你用git和SourceTree上传项目到github细解(转)

    尊重原创:https://blog.csdn.net/qq_32365567/article/details/52859166 一.引言 我想大家现在都很熟悉github了,也能运用github上开源 ...

  6. git上传项目到github远程库

    最近在学习使用 git 上传管理项目,依照教程,建好了一个远程库,也实现了本地库与远程库的项目同步上传,但是在试着将本地库里的项目上传到另一个新建远程库时遇到了问题,一直上传不成功,经过一番查找摸索终 ...

  7. 利用Git上传项目到github以及遇到的问题

    今天学习如何利用git从本地端上传项目,以及遇到问题的解决方法 1.要有自己的github账号,并创建一个仓库, 2.输入仓库的名称,直接Create 注:记住常见成功后的这个地址,后边要用到: 3. ...

  8. Git安装配置及第一次上传项目到GitHub

    平时的学习工作少不了保存自己的Code到代码库,这里必须要使用到Git与GitHub. 1.   关于Git的安装 下载Git:下载地址:https://git-scm.com/downloads  ...

  9. ubuntu在github上传项目

    GitHub是一个面向开源及私有软件项目的托管平台,因为只支持git 作为唯一的版本库格式进行托管,故名GitHub. 作为开源代码库以及版本控制系统,Github拥有超过900万开发者用户.随着越来 ...

随机推荐

  1. 从一次输入框无法输入的bug,谈如何限制输入框输入类型

    bug的产生和修改 上周临近周末休息的时候,一个同事跑过来了,对我说:"阿伦啊,有一个页面出问题了,火狐浏览器所有的input都没法输入了."我一听,是不是你给加了什么属性,让in ...

  2. hp MSA50 5盘RAID5重建为4盘RAID5怎么恢复数据

    [用户单位] XX省电视台[数据恢复故障描述] 一台HP 服务器,挂接一台HP MSA50磁盘阵列,内接5块1TB硬盘,原先结构为RAID5. 使用一段时间后,其中一块硬盘掉线,因RAID5支持一块硬 ...

  3. Pandas速查手册中文版

    本文翻译自文章: Pandas Cheat Sheet - Python for Data Science ,同时添加了部分注解. 对于数据科学家,无论是数据分析还是数据挖掘来说,Pandas是一个非 ...

  4. LeetCode & Q219-Contains Duplicate II

    Array Hash Table Description: Given an array of integers and an integer k, find out whether there ar ...

  5. kubernetes入门(01)kubernetes是什么?

    一.kubernetes是什么? Kubernetes是Google开源的一个容器编排引擎,它支持自动化部署.大规模可伸缩.应用容器化管理.在生产环境中部署一个应用程序时,通常要部署该应用的多个实例以 ...

  6. 2018年Web前端自学路线

    本文最初发表于博客园,并在GitHub上持续更新前端的系列文章.欢迎在GitHub上关注我,一起入门和进阶前端. 以下是正文. Web前端入门的自学路线 新手入门前端,需要学习的基础内容有很多,如下. ...

  7. VirtualBox的共享文件夹功能的使用演示

    演示环境 虚拟机 Oracle VM VirtualBox 宿主机 Windows 客户机 Linux 以下图片演示中使用的Linux客户机为CentOS.对于Debian系统的客户机,主要在安装增强 ...

  8. intelj idea 创建聚合项目(典型web项目,包括子项目util、dao、service)

    需求:第三方提供了http api接口,我们需要将其数据全部取回来,存放到本地Mysql数据库. 开发工具是intelj idea,准备基于maven创建聚合项目,util作为工具包,单独作为一个工程 ...

  9. POJ-1068 Parencodings---模拟括号的配对

    题目链接: https://vjudge.net/problem/POJ-1068 题目大意: 给出一种括号序列的表示形式名叫P序列,规则是统计出每个右括号之前的左括号个数作为序列每项的值.然后要求你 ...

  10. PHP 7.2 新功能介绍

    PHP 7.2 已經在 2017 年 11 月 30 日 正式發布 .這次發布包含新特性.功能,及優化,以讓我們寫出更好的代碼.在這篇文章裡,我將會介紹一些 PHP 7.2 最有趣的語言特性. 你可以 ...