This blog post describes how we can create a web application project with Gradle.

To be more specific, we want to create a web application project that uses Java, package our web application into a WAR file, and run our web application in a development environment.

Let’s find out how we can fulfil these requirements.

Additional Reading: If you are not familiar with Gradle, you should read the following blog post before you continue reading this blog post: Getting Started With Gradle: Introduction helps you to install Gradle, describes the basic concepts of a Gradle build, and describes how you can add functionality to your build by using Gradle plugins.

Getting Started With Gradle: Our First Java Project describes how you can create a Java project by using Gradle and package your application into an executable jar file.

Getting Started With Gradle: Dependency Management describes how you can manage the dependencies of your Gradle project.

Creating a Web Application Project

If we want to create a web application project that uses Java and Gradle, the first thing that we have to do is to create a Java project. Let’s see how we can do that.

Creating a Java Project

We can create a Java project by applying the Java plugin. We can do this by adding the following line to the build.gradle file:

apply plugin: 'java'

Additional Reading: The Java plugin adds new conventions (e.g. the default directory layout), tasks, and properties to our build. If you want to know more about this, you should read the following blog post: Getting Started with Gradle: Our First Java Project

Let’s move on and find out how we can package our web application.

Packaging Our Web Application

Before we can package our web application by using the War plugin, we have to add it to our build. After we have applied the War plugin, the build.gradle file looks as follows:

apply plugin: 'java' apply plugin: 'war'

The War plugin adds a new directory to the project’s directory layout, adds two new dependency management configurations, and adds a new task to our project. These changes are described in the following:

The War plugin adds the src/main/webapp directory to the project’s directory layout. This directory contains the sources of the web application (CSS files, Javascript files, JSP files, and so on).

The War plugin adds two new dependency management configurations called providedCompile and providedRuntime. These two two configurations have the same scope than the compile and runtime configurations, but the difference is that that the dependencies belonging to these new configurations are not added to the WAR archive.

The War plugin also adds the war task to our web application project. This task assembles a WAR archive to the build/libs directory.

Even though the War plugin adds the src/main/webapp directory to the project’s directory layout, it does not create it. If we need that directory, we have to create it ourself. Additional Reading: If you don’t know what the compile and runtime configurations are, you should read the following blog post: Getting Started with Gradle: Dependency Management

We can now package our web application by running the command gradle war at command prompt. When we do this, we should see the following output:

> gradle war :compileJava :processResources :classes :war BUILD SUCCESSFUL Total time: 4.937 secs

If everything went as expected, we should find the web-application.war file from the build/libs directory.

Note:

Additional Reading: If you need additional information about the War plugin or the war task, or you want to override the default configuration of the War plugin or the war task, you should take a closer look at the following web pages: Gradle User Guide: 25. The War plugin

Gradle DSL Reference: War

Let’s find out how we can run our web application in a development environment.

Running Our Web Application

We can run our web application in a development environment by using Gretty. It supports both Jetty and Tomcat, and it doesn’t suffer from the problem caused by Gradle’s leaking SLF4J bindings. Let’s move on and configure our build to run our web application with Gretty.

First, we have to configure the dependencies of our build script. We can do this by following these steps:

Configure the build script to use the Bintray’s JCenter Maven repository when it resolves its dependencies. Add the Gretty plugin dependency to the classpath of the build script.

The source code of the build.gradle file looks as follows:

buildscript { repositories { jcenter() } dependencies { classpath 'org.akhikhl.gretty:gretty:+' } } apply plugin: 'java' apply plugin: 'war'

Additional Reading: If you want to learn more about using binary plugins that have been published as jar files, you should read the following sections of the Gradle User Guide: Gradle User Guide: 20.4 Applying plugins with the buildscript block

Gradle User Guide: 59.6 External Dependencies for the build script

Second, we have to apply the Gretty plugin. After we have done this, the build.gradle file looks as follows:

buildscript { repositories { jcenter() } dependencies { classpath 'org.akhikhl.gretty:gretty:+' } } apply plugin: 'java' apply plugin: 'war' apply plugin: 'org.akhikhl.gretty'

Third, we need to configure Gretty by following these steps:

Configure Gretty to use Jetty 9 as a servlet container when it runs our web application. Configure Jetty to listen to port 8080. Configure Jetty to run our web application by using the context path ‘/’.

The source code of the build.gradle file looks as follows:

buildscript { repositories { jcenter() } dependencies { classpath 'org.akhikhl.gretty:gretty:+' } } apply plugin: 'java' apply plugin: 'war' apply plugin: 'org.akhikhl.gretty' gretty { port = 8080 contextPath = '/' servletContainer = 'jetty9' }

We can now start and stop our web application by running the following commands at command prompt:

the command gradle appStart will run our web application.

The command gradle appStop will stop our web application.

Let’s move on and summarize what we learned from this blog post.

Summary

This blog post has taught us four things:

We learned that if we use the default configuration, we must put the sources of our web application to the src/main/webapp directory.

We learned that the War plugin adds two new dependency configurations (providedCompile and providedRuntime) to our build, and we learned how these dependency configurations work.

We learned to package our web application into a WAR file.

We learned to run our web application in a development environment.

P.S. You can get the example application of this blog post from Github.