I released five new sample lessons from my Test With Spring course: Introduction to Spock Framework

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'
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:

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:

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:

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'
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:

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'
}
Additional Reading:

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.

About the Author

Petri Kainulainen 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.

About Petri Kainulainen →

17 comments… add one
  • Good post about gretty, but one of the things missing here is -why- WAR? I mean gretty supports hot reloading, running multiple containers etc which are great, but if you dont need them why not just create a JAR you can run without gradle anywhere (eg. production) ?

    Reply
    • Thank you for your comment! I will answer to your questions below.

      -why- WAR?

      This tutorial uses a WAR archive because it is familiar to pretty much every developer who wants to create a web application project that uses Java => You can get started as fast as possible even if you don’t have a lot of experience from Gradle.

      why not just create a JAR you can run without gradle anywhere (eg. production) ?

      The honest answer is that I don’t know how to do this yet (without using Spring Boot). This is a good idea for a new blog post though.

      Reply
  • Hi
    This tutorial was really great for me. I am a beginner in Gradle. Before coming to this post I watched
    some videos in YouTube but unfortunately could not understand anything . they were so high level.
    but when I read your posts about Gradle I learned it completely. Thanks a lot for your awesome tutorial for Gradle .

    Reply
    • You are welcome. It is nice to hear that this tutorial was useful to you.

      Reply
  • I followed each steps, when I tried to run gradle appStart, it said “task ‘appStart’ not found in project”. It’s true that the build script does not have appStart. Did I miss something?

    Reply
    • Hi,

      Unfortunately it’s impossible to say what can be wrong without seeing your build.gradle file. Have you compared your build.gradle file with the example application of this blog post?

      Reply
  • Very good tutorial! It helped me to build a ‘web-project’ using Gradle. So Fu**ing nice! Congratulations

    Reply
    • Thank you for your kind words. I really appreciate them.

      Reply
  • I am currently getting an error running “gradle war” on your project with no changes.

    C:\Users\drew.wasem\src\gradle-examples-master\web-application>gradle war

    FAILURE: Build failed with an exception.

    * Where:
    Build file ‘C:\Users\drew.wasem\src\gradle-examples-master\web-application\build.gradle’ line: 15

    * What went wrong:
    A problem occurred evaluating root project ‘web-application’.
    > Could not set unknown property ‘port’ for object of type org.akhikhl.gretty.GrettyExtension.

    * Try:
    Run with –stacktrace option to get the stack trace. Run with –info or –debug option to get more log output.

    BUILD FAILED

    Total time: 1.064 secs

    Reply
    • Hi,

      It seems that the author of the Gretty Gradle plugin changed the name of the port property to httpPort. In other words, you can use this snippet:

      
      gretty {
          httpPort = 8080
      	contextPath = '/'
          servletContainer = 'jetty9'
      }
      
      

      Thank you for pointing this out. I will update this blog post and the example application when I have time to do it.

      Reply
    • Found the issue. I had to change port to httpPort.

      Best,
      Drew

      Reply
      • Great. Thank you for pointing this out. I will update the blog post (and the example application) when I have time to do it.

        Reply
  • Awesome tutorial

    Reply
    • Thank you for your kind words. I really appreciate them.

      Reply

Leave a Comment