Gradle Web Application Development
Web Application Project
A normal Java application can be created with gradle init.
This section explains how a web application project is created and organized.
Creating a web application
A web application has a different structure and execution method from a normal Java application. It uses not only Java classes but also HTML and JSP, so those files must be collected and packaged into a WAR file. It also requires a servlet container, or Java server, to run.
Because of this, a normal Java application project is not enough by itself. The following steps create an actual project and explain the development flow.
First, create a project directory.
$ mkdir GradleWebApp
$ cd GradleWebApp
Then initialize Gradle.
$ gradle init --type java-application
This is the same initialization method as a normal Java application.
$ mkdir GradleWebApp
$ cd GradleWebApp
$ gradle init --type java-application
Starting a Gradle Daemon (subsequent builds will be faster)
BUILD SUCCESSFUL in 16s
2 actionable tasks: 2 executed
$ ls
build.gradle gradle gradlew gradlew.bat settings.gradle src
As of December 2017, Gradle did not provide a dedicated web application project type.
Therefore, initialize a java-application project and create the remaining web project structure manually.
Web application directory structure
Prepare the directory structure for the web application. It looks like this.
.
├── build.gradle, others omitted
└── src
└── main
│ ├── java
│ ├── resources
│ └── webapp
│ └── WEB-INF
└── test
└── java
└── AppTest.java
The main difference between a web application and a normal Java application is the structure under the main directory.
It contains the following three directories.
java: Directory for Java source files. Servlet source code is placed here.webapp: Public directory for the web application. JSP, HTML, and other web resources are placed here. CreateWEB-INFunder it and placeweb.xmlthere.resources: Directory for program resources required by the application. This is not for images displayed on web pages; those belong inwebapp. For example, files such aspersistence.xmlused by JPA are placed here.
Create webapp and resources under src/main, then create WEB-INF under webapp.
web.xml
Prepare web.xml, which describes information for the web application.
This example uses Servlet 3.1.
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
</web-app>
Preparing a servlet
Create a simple servlet that displays a web page.
Under src/main/java, create com/devkuma/web, then create SampleServlet.java.
package com.devkuma.web;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/hello")
public class SampleServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html><head><title>Servlet</title></head>");
out.println("<body><h1>Sample Servlet</h1>");
out.println("<p>Welcome to Sample Servlet page!</p>");
out.println("</body></html>");
}
}
This servlet uses the @WebServlet annotation to map the servlet to /hello.
Because of this setting, Servlet API 3.0 or later is required.
Depending on the servlet container version, it may not work, so check the runtime environment.
JSP and HTML files can be placed under webapp.
Final directory and file structure
After all files are created, the structure is as follows.
.
├── build.gradle, others omitted
└── src
├── main
│ ├── java
│ │ ├── App.java
│ │ └── com
│ │ └── devkuma
│ │ └── web
│ │ └── SampleServlet.java
│ ├── resources
│ └── webapp
│ └── WEB-INF
│ └── web.xml
└── test
└── java
└── AppTest.java
Writing the build.gradle Build File
Write build.gradle for the web application.
It differs in several ways from a normal Java application.
build.gradle
The full source is as follows.
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'gretty'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath group:'org.akhikhl.gretty', name:'gretty-plugin', version:'+'
}
}
repositories {
jcenter()
}
dependencies {
testCompile group:'junit', name:'junit', version:'4.12'
}
After writing the file, build the WAR file.
$ gradle war
This creates a web application WAR file.
After the command runs, GradleWebApp.war is created in the libs directory under build.
Next, check the application behavior.
$ gradle run
When the command runs, Jetty is downloaded and started. Open the following URL to access the servlet.
http://localhost:8080/GradleWebApp/hello
Used plugins
The build file applies three plugins.
apply plugin : 'java'
apply plugin : 'war'
apply plugin : 'gretty'
java provides tasks such as compileJava for Java programs.
war packages the project as a WAR file.
It is an essential plugin for web applications.
The earlier gradle war command is provided by this plugin.
gretty provides Jetty servlet container integration.
Gretty is an external plugin for using Jetty from Gradle, and gradle run starts Jetty and runs the web application.
buildscript and dependencies
The build file includes a buildscript block.
buildscript {
repositories {
// repositories
}
dependencies {
// package settings
}
}
buildscript configures the script used to run the build itself.
If the build only uses built-in Gradle functionality, this block is not needed.
It is required when the build uses external libraries or plugins.
In this example, the Gretty plugin is external, not part of standard Gradle.
Therefore, the build script must specify which repository to use and which library to load.
Those settings are written in repositories and dependencies inside buildscript.
The repositories and dependencies blocks after buildscript are used by the project build itself.
In this example, JUnit is declared there.
This completes the basic build and execution flow for a Gradle web application.