Skip to content

Instantly share code, notes, and snippets.

@matthauck
Created August 26, 2013 06:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save matthauck/6338633 to your computer and use it in GitHub Desktop.
Save matthauck/6338633 to your computer and use it in GitHub Desktop.
simple gradle forking tomcat plugin (intended to be put in buildSrc)
package com.foo
import org.gradle.api.DefaultTask
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.GradleException
import org.gradle.api.file.FileCollection
import org.gradle.api.tasks.TaskAction
import org.gradle.api.plugins.WarPlugin
class TomcatExtension {
Map systemProperties = [:]
String contextPath = null
int port = 8080
}
class TomcatPlugin implements Plugin<Project> {
private static final TOMCAT_VERSION = '7.0.41'
void apply(Project project) {
project.extensions.create('tomcat', TomcatExtension)
project.plugins.apply(WarPlugin)
project.configurations.create('tomcat').setVisible(false).setTransitive(true)
project.dependencies {
tomcat(
"org.apache.tomcat.embed:tomcat-embed-core:${TOMCAT_VERSION}",
"org.apache.tomcat.embed:tomcat-embed-logging-juli:${TOMCAT_VERSION}",
"org.apache.tomcat.embed:tomcat-embed-jasper:${TOMCAT_VERSION}"
)
}
project.task('tomcat',
type: TomcatRunTask,
dependsOn: project.war,
group: 'Tomcat',
description: "Launch embedded tomcat on project's war file"
)
}
}
class TomcatRunTask extends DefaultTask {
def contextPath() {
def contextPath = project.tomcat.contextPath ?: project.war.baseName
if (contextPath.charAt(0) != '/') {
contextPath = '/' + contextPath
}
contextPath
}
def makeClasspath() {
def buildSrcJar = project.files( new File(project.rootProject.projectDir, 'buildSrc/build/libs/buildSrc.jar') )
buildSrcJar +
project.buildscript.configurations.classpath +
project.files(project.buildscript.dependencies.localGroovy().resolve()) +
project.configurations.getByName('tomcat')
}
@TaskAction
def run() {
def work = new File(project.buildDir, 'tomcat')
work.mkdirs()
project.javaexec {
main = 'com.foo.TomcatMain'
classpath = makeClasspath()
args = [ project.war.archivePath, contextPath(), project.tomcat.port ]
systemProperties = project.tomcat.systemProperties
workingDir = work
maxHeapSize = '1024m'
minHeapSize = '512m'
jvmArgs '-XX:MaxPermSize=256m'
}
}
}
class TomcatMain {
private File war
private String contextPath
private int port
private static String HTTP_PROTOCOL = 'org.apache.coyote.http11.Http11NioProtocol'
TomcatMain(war, contextPath, port) {
this.war = new File(war)
this.contextPath = contextPath
this.port = port.toInteger()
}
void run() {
def tomcat = new org.apache.catalina.startup.Tomcat()
// add webapp
def ctx = tomcat.addWebapp(null, contextPath, war.getCanonicalPath())
ctx.unpackWAR = false
// add connector
tomcat.service.removeConnector(tomcat.getConnector())
tomcat.service.addConnector(makeConnector())
// base dir (cwd of forked process)
tomcat.setBaseDir(".")
// start!
tomcat.start()
println("\nServer is started: http://localhost:${port}${contextPath}\n")
tomcat.server.await()
}
def makeConnector() {
def conn = new org.apache.catalina.connector.Connector(HTTP_PROTOCOL)
conn.setPort(port)
conn.setURIEncoding('UTF-8')
conn
}
static void main(String[] args) {
new TomcatMain(args[0], args[1], args[2]).run();
}
}
@chronodm
Copy link

chronodm commented Nov 6, 2013

Hey, Matt -- I'm running into exactly the SLF4J problem that caused you to create this. Can you tell me how to use it? I've tried dropping it into my buildSrc and adding

apply plugin: TomcatPlugin

to my build.gradle, but that gets me

> Could not find property 'TomcatPlugin' on root project 'myproject'

(I removed the package statement from TomcatPlugin.groovy per this StackOverflow comment, but no luck.)

Looks to me like it's not even compiling -- there's no .class files in buildSrc/build/libs/buildSrc.jar.

Total gradle newbie here, so any advice appreciated.

@chronodm
Copy link

chronodm commented Nov 6, 2013

Okay, I think the problem was that I wasn't putting it in buildSrc/src/main/groovy (got to learn those conventions!). Now it's failing because it can't find org.apache.catalina... but I imagine I can sort that out.

@danielabar
Copy link

How did you resolve "can't find catalina..."? I'm getting

TomcatPlugin.groovy: 102: unable to resolve class org.apache.catalina.startup.Tomcat
 @ line 102, column 18.
       def tomcat = new org.apache.catalina.startup.Tomcat()

@sreeharibs
Copy link

(intended to be put in buildSrc)

      I can't even find `buildSrc`

it would be nice if some one can help me in using this plugin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment