Skip to content

Instantly share code, notes, and snippets.

@carey
Last active July 28, 2018 07:46
Show Gist options
  • Save carey/70fd0078b2aded5b7cf773c019bca8c3 to your computer and use it in GitHub Desktop.
Save carey/70fd0078b2aded5b7cf773c019bca8c3 to your computer and use it in GitHub Desktop.
Use Jetty Ant plugin from Gradle
import org.apache.tools.ant.BuildEvent
import org.apache.tools.ant.BuildListener
plugins {
id 'war'
}
ext {
jettyKey = 'f4f2aada-6224-4480-b2f4-b0063c0eb8a0'
}
configurations {
jetty {
exclude group: 'ant', module: 'ant'
exclude group: 'ant', module: 'ant-launcher'
}
}
dependencies {
compile group: 'com.google.guava', name: 'guava', version: '23.4-jre'
providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
jetty group: 'org.eclipse.jetty', name: 'jetty-ant', version: jettyVersion
jetty group: 'org.eclipse.jetty', name: 'jetty-jmx', version: jettyVersion
jetty group: 'org.vibur', name: 'vibur-dbcp', version: '22.2'
jetty group: 'com.sun.mail', name: 'javax.mail', version: '1.6.1'
jetty group: 'com.microsoft.sqlserver', name: 'mssql-jdbc', version: '6.4.0.jre8'
jetty group: 'org.slf4j', name: 'slf4j-simple', version: slf4jVersion
}
task jettyRun {
description = 'Run Jetty test server.'
group = 'Jetty'
dependsOn sourceSets.main.runtimeClasspath
doLast {
ant.taskdef(name: 'jettyRun', classname: 'org.eclipse.jetty.ant.JettyRunTask', classpath: configurations.jetty.asPath, loaderref: 'jetty.loader')
ant.typedef(name: 'connector', classname: 'org.eclipse.jetty.ant.types.Connector', classpath: configurations.jetty.asPath, loaderref: 'jetty.loader')
ant.typedef(name: 'webApp', classname: 'org.eclipse.jetty.ant.AntWebAppContext', classpath: configurations.jetty.asPath, loaderref: 'jetty.loader')
BuildListener listener = new BuildListener() {
void buildStarted(BuildEvent buildEvent) {}
void buildFinished(BuildEvent buildEvent) {}
void targetStarted(BuildEvent buildEvent) {}
void targetFinished(BuildEvent buildEvent) {}
void taskStarted(BuildEvent buildEvent) {
Thread.currentThread().contextClassLoader = buildEvent.project.getReference('jetty.loader') as ClassLoader
}
void taskFinished(BuildEvent buildEvent) {}
void messageLogged(BuildEvent buildEvent) {}
}
try {
ant.project.addBuildListener(listener)
ant.jettyRun(jettyXml: file('config/jetty9.xml'), scanIntervalSeconds: 15, stopPort: 7081, stopKey: jettyKey) {
connectors { connector(port: 7080) }
webApp(war: webAppDir, contextPath: '/', jettyEnvXml: file('config/jetty9-env.xml')) {
(sourceSets.main.runtimeClasspath - configurations.providedRuntime).each {
if (it.isDirectory()) { classes(dir: it) } else { lib(file: it) }
}
}
}
} finally {
ant.project.removeBuildListener(listener)
}
}
}
task jettyStop {
description = 'Stop Jetty test server.'
group = 'Jetty'
doLast {
ant.taskdef(name: 'jettyStop', classname: 'org.eclipse.jetty.ant.JettyStopTask', classpath: configurations.jetty.asPath, loaderref: 'jetty.loader')
ant.jettyStop(stopPort: 7081, stopKey: jettyKey)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment