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 {
          mavenCentral()
      }
      
      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
    6. 
      dependencies {
          compile("org.springframework:spring-context:4.0.0.RELEASE")
          compile("org.springframework:spring-webmvc:4.0.0.RELEASE")
          compile("org.aspectj:aspectjrt:1.7.4")
      }
      
    7. Add rest of our application Dependencies as shown below
    8. 
      dependencies {
          compile("javax.inject:javax.inject:1")
          compile("javax.servlet:servlet-api:2.5")
          compile("javax.servlet:jstl:2.5")
          compile("javax.servlet.jsp:jsp-api:1.2")	
          compile("org.slf4j:slf4j-api:1.7.5")
          compile("org.slf4j:jcl-over-slf4j:1.7.5")
          compile("org.slf4j:slf4j-log4j12:1.7.5")
          compile("log4j:log4j:1.2.15")
      }
      
    9. Add our Application Unit Test dependencies as shown below.
    10. 
      dependencies {
          testCompile("junit:junit:4.7") 
      }
      

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

      
      compile("org.springframework:spring-context:4.0.0.RELEASE")
      

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

      
      providedRuntime("javax.servlet:jstl:2.5")
      

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

      
      testCompile("junit:junit:4.7") 
      
    11. Our Application complete build.gradle dependencies are shown below.
    12. build.gradle

      
      dependencies {
          compile("org.springframework:spring-context:4.0.0.RELEASE")
          compile("org.springframework:spring-webmvc:4.0.0.RELEASE")
          compile("org.aspectj:aspectjrt:1.7.4")
      
          compile("javax.inject:javax.inject:1")
          compile("javax.servlet:servlet-api:2.5")
          compile("javax.servlet:jstl:2.5")
          compile("javax.servlet.jsp:jsp-api:1.2")	
          compile("org.slf4j:slf4j-api:1.7.5")
          compile("org.slf4j:jcl-over-slf4j:1.7.5")
          compile("org.slf4j:slf4j-log4j12:1.7.5")
          compile("log4j:log4j:1.2.15")
      
          testCompile("junit:junit:4.7") 
      }
      
    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.
    18. 
      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.
  4. 
    D:\apache-tomcat-8.0.24\bin>startup.bat
    
  5. Test our application
  6. Access homepage using this url: http://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/

    springmvc-gradle-loginpage

    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.

Comments

  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:

    Hi,
    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:

      Hi
      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..)

      Ram

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages