Enterprise Java

Getting Started with Gradle: Creating a Web Application Project

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:

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'

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:

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.

Additional Reading:

If you don’t know what the compile and runtime configurations are, you should read the following blog post:

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:

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:

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:

  1. Configure the build script to use the Bintray’s JCenter Maven repository when it resolves its dependencies.
  2. 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'

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:

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:

  1. Configure Gretty to use Jetty 9 as a servlet container when it runs our web application.
  2. Configure Jetty to listen to port 8080.
  3. 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'
}

If you want to learn more about Gretty, you should take a closer look at its documentation:

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.

If you want to learn how to use Gradle, you should take a look at my Gradle tutorial.

Petri Kainulainen

Petri is passionate about software development and continuous improvement. He is specialized in software development with the Spring Framework and is the author of Spring Data book.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button