Spring MVC Gradle

Filed Under: Gradle

In this post, we will develop and test simple Spring MVC Gradle application using Eclipse Gradle Plugin.

This post is not intended to train you on Spring MVC Framework Basics. If you are not familiar with Spring MVC, Please read Spring MVC Example.

Spring MVC Gradle

We are going to convert Spring MVC maven example from Spring MVC Tutorial into spring mvc gradle example.

It’s also recommended to go through my previous Gradle posts at Gradle Tutorial.

Spring MVC Convert Maven to Gradle

Below are the steps to follow for converting maven to gradle application.

  1. Develop the Maven based Spring MVC Application by following this link: Spring MVC Tutorial or download the code from below link.
  2. Import this spring mvc maven project into Eclipse IDE.
  3. Remove pom.xml file
  4. Create empty build.gradle file.
  5. Now start converting each and every component from pom.xml to build.gradle file.
    1. Base build.gradle template file
    2. build.gradle

      apply plugin: 'java'
      apply plugin: 'eclipse'
      sourceCompatibility = 1.7
      repositories {
      dependencies {
        // Dependencies goes here

      We will take this one as base build file and update it with required elements one by one.

    3. As it is a Spring MVC web application, we need to create WAR file. So Please apply war plugin and define war attributes as shown below.
    4. Also Provide war file name and it’s version.

      apply plugin: "war"
      war {
          baseName = 'SpringMVCExample'
          version = '1.0.0-BUILD-SNAPSHOT'
    5. Add Spring Dependencies as shown below
      dependencies {
    7. Add rest of our application Dependencies as shown below
      dependencies {
    9. Add our Application Unit Test dependencies as shown below.
      dependencies {

      We use “compile()” element to add dependencies.


      We use “providedRuntime()” element to add dependencies at runtime.


      We use “testCompile()” element to add dependencies for running only unit tests.

    11. Our Application complete build.gradle dependencies are shown below.
    12. build.gradle

      dependencies {
    13. Final project structure will looks like below image now.
    14. spring mvc gradle example

    15. Open command prompt at Project Root Directory to execute gradle commands.
    16. spring mvc gradle cmd

    17. Now build war file by executing the following gradle commands.
      gradle clean
      gradle build

      Or execute both commands at once using the following syntax. It’s similar to executing Maven commands.

      gradle clean build

      spring mvc gradle run commands

      I always recommend everyone to use “clean” command before building our application. It cleans our old build stuff, compiles and creates new WAR/JAR with latest files.

    19. Observe our newly created war file at ${PROJECT_ROOT_DIR}/build/libs/ folder as shown below.
    20. spring mvc gradle war file

      War file is created with given Name and Version in build.gradle file. i.e. SpringMVCExample-1.0.0-BUILD-SNAPSHOT.war

Test Spring MVC Gradle Application

We have developed Spring MVC gradle application successfully. Now it’s time to deploy and test this application. You can deploy this application into any Web/Application Server. I’m going to test this application using Apache Tomcat 8.

${TOMCAT8_HOME} = D:\apache-tomcat-8.0.24
${TOMCAT8_BIN} = D:\apache-tomcat-8.0.24\bin
  1. Copy our war file “SpringMVCExample-1.0.0-BUILD-SNAPSHOT.war” to ${TOMCAT8_HOME}/webapps/ folder
  2. spring mvc gradle tomcat copy war file webapps

  3. Open CMD Prompt at ${TOMCAT8_BIN} and start Tomcat server.
  5. Test our application
  6. Access homepage using this url: https://localhost:8888/SpringMVCExample-1.0.0-BUILD-SNAPSHOT/

    Here our application RootContext is “SpringMVCExample-1.0.0-BUILD-SNAPSHOT”. If required, we can give proper name like “SpringMVCExample” to the WAR file.

    gradle spring mvc example

    Access Login page using this url: localhost:8888/SpringMVCExample-1.0.0-BUILD-SNAPSHOT/login/


    Provide Username,click on “Login” button and observe next page as shown below:

    gradle example spring mvc web application

    spring mvc gradle login output

Observations between Gradle and Maven Projects

If we observe our both SpringMVCExample Maven and Gradle based projects, we can observe the following things.

  1. We are getting same war file and output.
  2. Maven’s build.xml is XML file where as Gradle’s build.gradle file is plain text file.
  3. Maven’s build.xml is too big and contains lot of XML configuration. Gradle’s build.gradle contains very minimal configuration.
  4. Gradle’s build.gradle file contains less configuration and also very light weight build script file.

Apart from these benefits, We can also get many more benefits from Gradle file. Please refer them at my first post about Gradle.

Because of these reasons, now-a-days most of the new projects are starting with Gradle build tool and existing projects are converting from Maven to Gradle.

That’s it for gradle example with spring mvc application. Like this we can develop any application using Gradle build tool.


  1. Shashank says:

    I tried the same build.gradle but not getting the same project structure

  2. Abdul says:

    I tried umpteen times but not working at all

  3. YASHODA B says:

    i’m a beginner in java and a regular reader of your site. You are doing a great job. Thanks for your tutorials ..I have a small doubt. why are you deploying the application using command prompt instead of doing it in Eclipse IDE ?

    1. Rambabu says:

      When compare to IDE, Developing and testing applications from CMD Prompt is bit tedious task in typing commands and bit tough to use those commands. As a beginner, we should understand “How to use all commands and their syntax”. Once we are comfortable we can go with easy method, Use IDE with mouse clicks. Here we don’t know what is going on inside and which commands are executing, their syntax. If someone is not interested in learning commands syntax, then they can directly start with IDEs (Easy approach..)


Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors