Reputation: 889
My current goal is to get gradle to start my web application running in a Jetty instance on my machine so that I can write Selenium tests against it. The Gretty plugin seems to be loading, but I haven't been able to find any actual instructions on how to create and configure a task to do that.
Part of the problem is that there is confusion with different versions and editions of the Gretty plugin. Just getting it to load in the first place was an exercise in trial-and-error.
I am trying to do the using Gradle 5.4.1 and Gretty 2.3.1, which I believe are the current versions (at this time).
I have three sub-projects "laoi" depends on "aofl" depends on "efl". The laoi build produces a WAR file. The task appRunWar seems to want to create and run its own WAR file, rather than using mine (at least that seems to be what's happening).
settings.gradle:
rootProject.name = 'aoi'
include 'cm', 'efl', 'aofl', 'laoi', 'uiTest'
build.gradle (laoi):
static def getDate() {
return new Date().format('yyyyMMdd-HHmmss')
}
final String timepickerAddonVersion = '1.6.3'
final String datatablesVersion = '1.10.19'
final String jqueryUIVersion = '1.12.1'
final String jqueryVersion = '3.2.1'
if (null == System.properties['aoi.release'] || null == System.properties['aoi.iteration']) {
if (null == System.env['RELEASE'] || null == System.env['ITERATION']) {
ext.ITERATION = "un"
ext.RELEASE = "dev"
} else {
ext.ITERATION = System.env['ITERATION']
ext.RELEASE = System.env['RELEASE']
}
} else {
ext.ITERATION = System.properties['aoi.iteration']
ext.RELEASE = System.properties['aoi.release']
}
System.setProperty('aoi.iteration', ext.ITERATION)
System.setProperty('aoi.release', ext.RELEASE)
if (null == System.properties['aoi.manifest']) {
if (null == System.env['MANIFEST']) {
System.out.println("Using default manifest name.")
ext.MANIFEST = "aoiManifest"
System.out.println("Manifest: ${ext.MANIFEST}")
} else {
ext.MANIFEST = System.env['MANIFEST']
}
} else {
ext.MANIFEST = System.properties['aoi.manifest']
}
System.setProperty('aoi.manifest', ext.MANIFEST)
System.out.println("Manifest: ${ext.MANIFEST}")
final String warFileName = 'aoi-'+ System.properties['aoi.release'] +'_'+ System.properties['aoi.iteration'] + ".war"
println "War file name: ${warFileName}"
final String sourceManifestName = "${ext.MANIFEST}.xml"
println "Source manifest name: ${sourceManifestName}"
def rootLibs = new File("${rootDir}/libs")
repositories {
mavenCentral()
jcenter()
flatDir {
dirs "${rootDir}/libs"
}
}
compileScala {
dependsOn ":efl:test", ":aofl:test"
}
task copyManifest(type: Copy) {
from('src/main/resources') {
include sourceManifestName
rename sourceManifestName, 'aoiManifest.xml'
}
into("${buildDir}/resources/main/bootstrap/liftweb")
}
task createVersionFile {
dependsOn "processResources"
doLast {
new File("${buildDir}/resources/main/aoiVersion.conf").text = "AOI_VERSION=" + System.properties['aoi.release'] +'-'+ System.properties['aoi.iteration'] + " (" + getDate() + ")"
new File("${buildDir}/resources/main/WebJarVersions.conf").text =
"""jQuery-Timepicker-Addon=$timepickerAddonVersion
datatables=$datatablesVersion
jquery=$jqueryVersion
jquery-ui=$jqueryUIVersion
jquery-ui-themes=$jqueryUIVersion
"""
}
}
war {
dependsOn ":aofl:test", "compileScala", "copyManifest", "processResources", "createVersionFile"
setDestinationDirectory(rootLibs)
setArchiveFileName(warFileName)
from('${buildDir}/resources/main') {
include '**/*.xml'
into("classes")
}
}
dependencies {
implementation project(":efl")
implementation project(":aofl")
implementation "org.webjars:jquery:$jqueryVersion"
implementation "org.webjars:jquery-ui:$jqueryUIVersion"
implementation "org.webjars:jquery-ui-themes:$jqueryUIVersion"
implementation "org.webjars:datatables:$datatablesVersion"
implementation "org.webjars:jQuery-Timepicker-Addon:$timepickerAddonVersion"
implementation 'org.webjars:webjars-servlet-2.x:1.1'
}
Upvotes: 1
Views: 1196
Reputation: 889
Since no answers were forthcoming I had to find an answer on my own.
Jetty has a feature that allows you to drop WAR files in a directory and it will load them. In order to make use of that, you have to either figure out what directory it's looking in, or tell it what directory to look in. I went for the second option.
The easiest way to specify a deploy directory is to put it in a "serverConfigFile" which is specified in the gretty options.
gretty {
serverConfigFile = "$contextFile"
servletContainer = 'jetty9.4'
jvmArgs <--- a bunch of arguments go here --->
}
Jetty comes with a bunch of configuration files in the "etc" directory. I viciously and blatantly plagiarized the main Jetty configuration file along with the deployment manager configuration file - but setting the path to one of my choosing. I created a task that writes the serverConfigFile.
task createJettyContextsFile {
doLast {
new File("$contextFile").text =
"""<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">
<!-- =============================================================== -->
<!-- Documentation of this file format can be found at: -->
<!-- https://www.eclipse.org/jetty/documentation/current/ -->
<!-- -->
<!-- Additional configuration files are available in JETTY_HOME/etc -->
<!-- and can be mixed in. See start.ini file for the default -->
<!-- configuration files. -->
<!-- -->
<!-- For a description of the configuration mechanism, see the -->
<!-- output of: -->
<!-- java -jar start.jar -? -->
<!-- =============================================================== -->
<!-- =============================================================== -->
<!-- Configure a Jetty Server instance with an ID "Server" -->
<!-- Other configuration files may also configure the "Server" -->
<!-- ID, in which case they are adding configuration to the same -->
<!-- instance. If other configuration have a different ID, they -->
<!-- will create and configure another instance of Jetty. -->
<!-- Consult the javadoc of o.e.j.server.Server for all -->
<!-- configuration that may be set here. -->
<!-- =============================================================== -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Arg name="threadpool"><Ref refid="threadPool"/></Arg>
<!-- =========================================================== -->
<!-- Add shared Scheduler instance -->
<!-- =========================================================== -->
<Call name="addBean">
<Arg>
<New class="org.eclipse.jetty.util.thread.ScheduledExecutorScheduler"/>
</Arg>
</Call>
<!-- =========================================================== -->
<!-- Http Configuration. -->
<!-- This is a common configuration instance used by all -->
<!-- connectors that can carry HTTP semantics (HTTP, HTTPS, etc.)-->
<!-- It configures the non wire protocol aspects of the HTTP -->
<!-- semantic. -->
<!-- -->
<!-- This configuration is only defined here and is used by -->
<!-- reference from other XML files such as jetty-http.xml, -->
<!-- jetty-https.xml and other configuration files which -->
<!-- instantiate the connectors. -->
<!-- -->
<!-- Consult the javadoc of o.e.j.server.HttpConfiguration -->
<!-- for all configuration that may be set here. -->
<!-- =========================================================== -->
<New id="httpConfig" class="org.eclipse.jetty.server.HttpConfiguration">
<Set name="secureScheme"><Property name="jetty.httpConfig.secureScheme" default="https" /></Set>
<Set name="securePort"><Property name="jetty.httpConfig.securePort" deprecated="jetty.secure.port" default="8443" /></Set>
<Set name="outputBufferSize"><Property name="jetty.httpConfig.outputBufferSize" deprecated="jetty.output.buffer.size" default="32768" /></Set>
<Set name="outputAggregationSize"><Property name="jetty.httpConfig.outputAggregationSize" deprecated="jetty.output.aggregation.size" default="8192" /></Set>
<Set name="requestHeaderSize"><Property name="jetty.httpConfig.requestHeaderSize" deprecated="jetty.request.header.size" default="8192" /></Set>
<Set name="responseHeaderSize"><Property name="jetty.httpConfig.responseHeaderSize" deprecated="jetty.response.header.size" default="8192" /></Set>
<Set name="sendServerVersion"><Property name="jetty.httpConfig.sendServerVersion" deprecated="jetty.send.server.version" default="true" /></Set>
<Set name="sendDateHeader"><Property name="jetty.httpConfig.sendDateHeader" deprecated="jetty.send.date.header" default="false" /></Set>
<Set name="headerCacheSize"><Property name="jetty.httpConfig.headerCacheSize" default="4096" /></Set>
<Set name="delayDispatchUntilContent"><Property name="jetty.httpConfig.delayDispatchUntilContent" deprecated="jetty.delayDispatchUntilContent" default="true"/></Set>
<Set name="maxErrorDispatches"><Property name="jetty.httpConfig.maxErrorDispatches" default="10"/></Set>
<Set name="blockingTimeout"><Property deprecated="jetty.httpConfig.blockingTimeout" name="jetty.httpConfig.blockingTimeout.DEPRECATED" default="-1"/></Set>
<Set name="persistentConnectionsEnabled"><Property name="jetty.httpConfig.persistentConnectionsEnabled" default="true"/></Set>
<Set name="requestCookieCompliance"><Call class="org.eclipse.jetty.http.CookieCompliance" name="valueOf"><Arg><Property name="jetty.httpConfig.requestCookieCompliance" deprecated="jetty.httpConfig.cookieCompliance" default="RFC6265"/></Arg></Call></Set>
<Set name="responseCookieCompliance"><Call class="org.eclipse.jetty.http.CookieCompliance" name="valueOf"><Arg><Property name="jetty.httpConfig.responseCookieCompliance" default="RFC6265"/></Arg></Call></Set>
<Set name="multiPartFormDataCompliance"><Call class="org.eclipse.jetty.server.MultiPartFormDataCompliance" name="valueOf"><Arg><Property name="jetty.httpConfig.multiPartFormDataCompliance" default="RFC7578"/></Arg></Call></Set>
</New>
<!-- =========================================================== -->
<!-- Set the default handler structure for the Server -->
<!-- A handler collection is used to pass received requests to -->
<!-- both the ContextHandlerCollection, which selects the next -->
<!-- handler by context path and virtual host, and the -->
<!-- DefaultHandler, which handles any requests not handled by -->
<!-- the context handlers. -->
<!-- Other handlers may be added to the "Handlers" collection, -->
<!-- for example the jetty-requestlog.xml file adds the -->
<!-- RequestLogHandler after the default handler -->
<!-- =========================================================== -->
<Set name="handler">
<New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
<Set name="handlers">
<Array type="org.eclipse.jetty.server.Handler">
<Item>
<New id="Contexts" class="org.eclipse.jetty.server.handler.ContextHandlerCollection"/>
</Item>
<Item>
<New id="DefaultHandler" class="org.eclipse.jetty.server.handler.DefaultHandler"/>
</Item>
</Array>
</Set>
</New>
</Set>
<!-- =========================================================== -->
<!-- extra server options -->
<!-- =========================================================== -->
<Set name="stopAtShutdown"><Property name="jetty.server.stopAtShutdown" default="true"/></Set>
<Set name="stopTimeout"><Property name="jetty.server.stopTimeout" default="5000"/></Set>
<Set name="dumpAfterStart"><Property name="jetty.server.dumpAfterStart" deprecated="jetty.dump.start" default="false"/></Set>
<Set name="dumpBeforeStop"><Property name="jetty.server.dumpBeforeStop" deprecated="jetty.dump.stop" default="false"/></Set>
<Call name="addBean">
<Arg>
<New id="DeploymentManager" class="org.eclipse.jetty.deploy.DeploymentManager">
<Set name="contexts">
<Ref refid="Contexts" />
</Set>
<Call name="setContextAttribute">
<Arg>org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern</Arg>
<Arg>.*/[^/]*servlet-api-[^/]*\\.jar\$|.*/javax.servlet.jsp.jstl-.*\\.jar\$|.*/org.apache.taglibs.taglibs-standard-impl-.*\\.jar\$</Arg>
</Call>
<Call id="webappprovider" name="addAppProvider">
<Arg>
<New class="org.eclipse.jetty.deploy.providers.WebAppProvider">
<Set name="monitoredDirName">
<Property>
<Name>jetty.deploy.monitoredPath</Name>
<Default>
<Property name="jetty.deploy.monitoredDir" deprecated="jetty.deploy.monitoredDirName" default="$deployDirectory"/>
</Default>
</Property>
</Set>
<Set name="defaultsDescriptor">
<Property>
<Name>jetty.deploy.defaultsDescriptorPath</Name>
<Default>
<Property name="jetty.home" default="." />/etc/webdefault.xml
</Default>
</Property>
</Set>
<Set name="scanInterval"><Property name="jetty.deploy.scanInterval" default="1"/></Set>
<Set name="extractWars"><Property name="jetty.deploy.extractWars" default="true"/></Set>
<Set name="configurationManager">
<New class="org.eclipse.jetty.deploy.PropertiesConfigurationManager">
</New>
</Set>
</New>
</Arg>
</Call>
</New>
</Arg>
</Call>
</Configure>
"""
}
}
Then I added a task to copy the WAR file into the correct directory.
task copyWar(type: Copy) {
dependsOn ":cm:buildPS", ":laoi:war", 'createJettyContextsFile'
from("$rootLibs") {
include "*.war"
}
into("$deployDirectory")
}
I had to tweak the task dependencies a bit, but now the Gretty tasks that start up the Jetty server will all run with my WAR file properly deployed.
Upvotes: 1