Adding a Web Module to a Gradle Project

It’s common in a lot of projects the need to separate parts of the system into their own modules. Gradle is well suited for this approach by allowing a single project to have many logically different modules. This post is continuation of my previous post Building With Gradle.

In this post we’ll create a second module for deploying to a servlet container. We’ll call this module example-web and it will produce a WAR artifact for deployment in Tomcat, Jetty, or any other suitable application server.

So lets roll…

Lets review, currently we have our project structured as shown below:


Now we’ll add one more directory for our web module and our new structure will be so:


Now we’ll need to crack open the settings.Gradle file located in trunk and change it to add the new module.

At this point we can start creating the web module, we’ll start with a simple build file (build.Gradle in the example-web directory) to get it going.

At the top of the file you notice the apply plugin: ‘war’ line. The war plugin is a standard Gradle plugin that is used to create compliant war archives. The war plugin also includes the java plugin so all of those conventions will also be present. The default layout for the source, test and web files is as follows.


All source files are found in the main directory while tests are in tests. Within the main directory the java, resources, and webapp directories hold java source files, non-java resource files and the webapp files (css, html, css, web.xml etc.) respectively.

I’ve built a simple web app that greets the guest when the root of the application is accessed, this builds on the previous post. To do this I needed to add a dependency on the javax servlet API. But, since the official API jar dependency isn’t found in maven central I used the one from the geronimo project. I’ve also added a dependency to the example-core project.

In order to include the project as a dependency I used two lines:



	compile project(':example-core')

The first line is used by Gradle internally to order how projects are built. Without the dependsOn clause Gradle would build them in the order found, usually alphabetical. The second line adds the example-core project to the compile dependencies for example-web. You should always include both of these lines when adding a project as a dependency.

Finally we’ll add the jetty plugin to the example-web build file so we can run the app. The final build file will appear as below.

Now we can start the webapp by issuing the Gradle task


. Here’s the output:

At this point a jetty instance should be running on port 8080 with our application. This link should get you to the front page http://localhost:8080/example-web/. To kill the server issue a Ctrl-C at the command line.

Well that wraps up setting up the web module and if you’d like you can still go back and run example-core. The implementation is still there and works exactly the same as the web version. As usual you can find the project at, for this post look at the p2 tag. You’ll also note that I’ve added eclipse files to the projects. In the next post I’ll go through how to set that up so you can keep it up to date with the Gradle build files as they change.

About the Author

Object Partners profile.

One thought on “Adding a Web Module to a Gradle Project

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Blog Posts
Google Professional Machine Learning Engineer Exam 2021
Exam Description A Professional Machine Learning Engineer designs, builds, and productionizes ML models to solve business challenges using Google Cloud technologies and knowledge of proven ML models and techniques. The ML Engineer is proficient in all aspects […]
Designing Kubernetes Controllers
There has been some excellent online discussion lately around Kubernetes controllers, highlighted by an excellent Speakerdeck presentation assembled by Tim Hockin. What I’d like to do in this post is explore some of the implications […]
React Server Components
The React Team recently announced new work they are doing on React Server Components, a new way of rendering React components. The goal is to create smaller bundle sizes, speed up render time, and prevent […]
Jolt custom java transform
Jolt is a JSON to JSON transformation library where the transform is defined in JSON. It’s really good at reorganizing the json data and massaging it into the output JSON you need. Sometimes, you just […]