Gradle构建并自动部署Java Web项目到远程服务器
Posted Herman-Hong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Gradle构建并自动部署Java Web项目到远程服务器相关的知识,希望对你有一定的参考价值。
思路介绍:1)停止对应项目的tomcat服务器 2)删除webapps其下的对应项目文件 3)构建war包并将war包传送到tomcat服务器webapps目录下 4)启动tomcat
一、环境配置
myeclipse2014、tomcat7.0.67(服务器端)、gradle2.14、Buildship2.0(执行gradle任务用,直接在eclipse marketplace中安装即可)、jdk1.7
二、配置build.gradle
关于gradle java web项目的创建可参见myeclipse中创建gradle web项目,主要配置如下:
1)配置ssh插件、提供远程服务器登陆,文件传输,命令执行等支持
apply plugin: 'org.hidetake.ssh'
buildscript
repositories
jcenter()
mavenCentral()
dependencies
classpath 'org.hidetake:gradle-ssh-plugin:2.6.0'
2)配置远程服务器信息
ssh.settings
knownHosts = allowAnyHosts
remotes
deployServer
host = 'xxx.xxx.xxx.xxx'
user = 'xxx'
password = 'xxx'
3)配置war包及其插件、将java web项目打war用
apply plugin: 'war'
war
archiveName 'cloud_server.war'
4)配置gradle task、共四个任务:关闭tomcat、删除war包及目录、拷贝文件、启动tomcat,且后者依赖前者
task shutdownTomcat() <<
ssh.run
session(remotes.deployServer)
println 'shut down tomcat...'
executeScript '''#!/bin/sh
cd /opt/app/apache-tomcat-7.0.67/bin
./shutdown.sh
'''
task del(dependsOn:shutdownTomcat) <<
ssh.run
session(remotes.deployServer)
println 'start deleting...'
executeScript '''#!/bin/sh
rm -rf /opt/app/apache-tomcat-7.0.67/webapps/cloud_server
rm -f /opt/app/apache-tomcat-7.0.67/webapps/cloud_server.war
'''
task copy(dependsOn:del) <<
ssh.run
session(remotes.deployServer)
println 'start copying war...'
put from: buildDir.toString() + '/libs/cloud_server.war', into: '/opt/app/apache-tomcat-7.0.67/webapps'
task deploy(dependsOn:copy) <<
ssh.run
session(remotes.deployServer)
println 'start tomcat...'
execute '/opt/app/apache-tomcat-7.0.67/bin/startup.sh'
三、执行gradle任务
(一)中说了用buildship可以执行gradle任务
1)调出Gradle Tasks视图
双击deploy,则添加了deploy任务(这个时候就会执行task)
2)配置deploy任务
run as-->external tools configuration
按照上图中配置即可
4)执行该任务
run as----->gradle build
选择上面配置的deploy任务即可,执行过程如下:
[sts] -----------------------------------------------------
[sts] Starting Gradle build for the following tasks:
[sts] clean
[sts] war
[sts] deploy
[sts] -----------------------------------------------------
:clean
:compileJava
:processResources
:classes
:war
:shutdownTomcat
Host key checking is off. It may be vulnerable to man-in-the-middle attacks.
shut down tomcat...
deployServer#16|Using CATALINA_BASE: /opt/app/apache-tomcat-7.0.67
deployServer#16|Using CATALINA_HOME: /opt/app/apache-tomcat-7.0.67
deployServer#16|Using CATALINA_TMPDIR: /opt/app/apache-tomcat-7.0.67/temp
deployServer#16|Using JRE_HOME: /opt/jdk7
deployServer#16|Using CLASSPATH: /opt/app/apache-tomcat-7.0.67/bin/bootstrap.jar:/opt/app/apache-tomcat-7.0.67/bin/tomcat-juli.jar
:del
Host key checking is off. It may be vulnerable to man-in-the-middle attacks.
start deleting...
:copy
Host key checking is off. It may be vulnerable to man-in-the-middle attacks.
start copying war...
:deploy
Host key checking is off. It may be vulnerable to man-in-the-middle attacks.
start tomcat...
deployServer#19|Using CATALINA_BASE: /opt/app/apache-tomcat-7.0.67
deployServer#19|Using CATALINA_HOME: /opt/app/apache-tomcat-7.0.67
deployServer#19|Using CATALINA_TMPDIR: /opt/app/apache-tomcat-7.0.67/temp
deployServer#19|Using JRE_HOME: /opt/jdk7
deployServer#19|Using CLASSPATH: /opt/app/apache-tomcat-7.0.67/bin/bootstrap.jar:/opt/app/apache-tomcat-7.0.67/bin/tomcat-juli.jar
deployServer#19|Tomcat started.
BUILD SUCCESSFUL
Total time: 59.412 secs
[sts] -----------------------------------------------------
[sts] Build finished succesfully!
[sts] Time taken: 1 min, 4 sec
[sts] -----------------------------------------------------
可以看到执行了build.gradle中配置的任务,这时候服务器上war包已经是最新的了,tomcat也已经重启。
参考:http://www.cnblogs.com/chenpi/p/5872313.html
以上是关于Gradle构建并自动部署Java Web项目到远程服务器的主要内容,如果未能解决你的问题,请参考以下文章
intellij idea 2017 gradle构建spring-boot web项目