In the previous section, we created a Java project with a web
subproject. The web
project has a simple servlet. To execute the servlet, we must create a WAR file and deploy the WAR file to a servlet container, such as Tomcat or Jetty. You can learn more about Jetty at http://www.eclipse.org/jetty/. With the Jetty plugin, we can run our web
project from the command line in a Jetty web container. We don't have to install Jetty on our computer, we only need to apply the Jetty plugin to our project. The plugin will take care of configuring Jetty and starting the web container. If everything is okay, we can open a web browser and access our servlet.
To add the Jetty plugin to our web
project, let's create a new build.gradle
file in the web directory. Here, we will use the apply()
method to add the Jetty plugin to the project:
apply plugin: 'jetty'
The plugin adds the following tasks to our project: jettyRun
, jettyRunWar
, and jettyStop
. The following table shows the different tasks:
Task |
Depends on |
Type |
Description |
|
|
|
This is to start a Jetty web container and deploy the exploded web application |
|
|
|
This is to start a Jetty web container and deploy the WAR file |
|
- |
|
This is to stop a running Jetty web container |
We can test our servlet in a web browser after we execute the jettyRun
or jettyWar
task. We get the following output when we execute the jettyRun
task from the root of the multi-project build:
$ gradle :web:jettyRun :common:compileJava :common:processResources UP-TO-DATE :common:classes :common:jar :services:sample:compileJava :services:sample:processResources UP-TO-DATE :services:sample:classes :services:sample:apiJar :services:sample:jar :web:compileJava :web:processResources UP-TO-DATE :web:classes > Building 92% > :web:jettyRun > Running at http://localhost:8080/web
Gradle will keep running, and at the end, we will see that the application is running at
http://localhost:8080/web
. We can open a web browser and access our web application. In the following screenshot, we can see the output of the servlet:
To stop the Jetty web container, press Ctrl + C at the command line to return to our prompt.
We can change the port number via the httpPort
project convention property added by the Jetty plugin or the httpPort
task property of the jettyRun
and jettyRunWar
tasks. To change the context path, we can set the contextPath
property of the jettyRun
and jettyRunWar
tasks.
If we want the Jetty container to automatically scan for changes, we can set the reload
property to automatic.
If the property is set to manual
, we must press Enter on the command line to reload changes. We can set the scan interval in seconds with the scanIntervalSeconds
property.
In the following sample build file, we will customize the Jetty web container with another HTTP port, context path, and automatic reloading:
apply plugin: 'jetty' httpPort = 8090 jettyRun { contextPath = 'sample' reload = 'automatic' scanIntervalSeconds = 10 }
We can even customize the Jetty container further with custom Jetty configuration files. We could use the jettyRun
task property, jettyConfig
, to use configuration files. We can also add extra runtime libraries with the additionalRuntimeJars
property.
If we want to use the jettyStop
task, we must also define the stopPort
and stopKey
properties in either our project or task. If we have defined these properties, we can open a new command-line prompt and invoke the jettyStop
task to stop a running Jetty web container.
In the following example build file, we will apply some of these properties and methods to customize the Jetty configuration:
apply plugin: 'jetty' configurations { // Extra configuration to // be used in the jettyRun task. jettyAdditionalLibs } dependencies { jettyAdditionalLibs 'org.slf4j:slf4j-simple:1.7.3' } // Properties for stopping Jetty with jettyStop stopPort = 8109 stopKey = 'JettyStop' jettyRun { // External Jetty configuration file. jettyConfig = file('src/jetty/jetty.xml') // Extra libraries for Jetty runtime. additionalRuntimeJars configurations.jettyAdditionalLibs }
18.191.189.186