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:

	trunk
		example-core
		example-parent

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

	trunk
		example-core
		example-parent
		example-web

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.

	example-web
		src
			main
				java
				resources
				webapp
			test
				java
				resources

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:

	dependsOn(':example-core')

And

	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

jettyRun

. 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 https://github.com/jhollandus/opiblog, 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.

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 *

*

*