Remote tomcat deployment with Gradle

Recently, I am playing Gradle, which is a similar tool as Maven. What I usual do is to depoly webiste into Tomcat. Refer this Deploy Website into Tomcat 7 with Maven. Here is my note to deploy website with Gradle.

1. Configure Gradle SSH Plugin into your project.

This is a demo from my ongoing project DealRen-Java-SDK

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
plugins {
id 'groovy'
id 'war'
id 'org.hidetake.ssh' version '1.0.1'
}

tasks.withType(JavaCompile) {
sourceCompatibility = 1.7
targetCompatibility = 1.7
options.encoding = "UTF-8"
}

remotes {
codeashobby {
host = project.property('host')
user = project.property('host.user')
password = project.property('host.password')
}
}

task showPlatformVersion << {
ssh.run {
session(remotes.codeashobby) {
execute('uname -a')
execute('cat /etc/*-release || true')
}
}
}

ext.webappsDir = project.property('host.webapps.dir')
war.archiveName = project.property('war.name')

task deploy(dependsOn: war) << {
ssh.run {
session(remotes.codeashobby) {
put(war.archivePath.path, webappsDir)
//execute('sudo service tomcat restart')
}
}
}

task wrapper(type: Wrapper) {
gradleVersion = '2.2.1'
}

repositories {
mavenCentral()
}

dependencies {
testCompile 'junit:junit:4.11'
compile 'org.codehaus.groovy:groovy-all:2.3.9'
compile 'com.google.guava:guava:18.0'
true// many many more
}

2. Then open a terminal, and type gradle deploy.

This will connection your server with SSH, and overwrite the previous war file, which is different from Maven, which use tomcat manager page to deploy war file.

Note:

When you run gradle deploy, you may get such error: reject HostKey Find a solution here: http://anahorny.blogspot.com/2013/05/solution-for-comjcraftjschjschexception.html, and it works great.