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.
-
- Develop the Maven based Spring MVC Application by following this link: Spring MVC Tutorial or download the code from below link.
- Import this spring mvc maven project into Eclipse IDE.
- Remove
pom.xml
file - Create empty
build.gradle
file. - Now start converting each and every component from pom.xml to build.gradle file.
- Base build.gradle template file
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.
-
-
- 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.
-
Also Provide war file name and it’s version.
apply plugin: "war"
war {
baseName="SpringMVCExample"
version = '1.0.0-BUILD-SNAPSHOT'
}
-
-
- Add Spring Dependencies as shown below
-
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")
}
-
-
- Add rest of our application Dependencies as shown below
-
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")
}
-
-
- Add our Application Unit Test dependencies as shown below.
-
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")
-
-
- Our Application complete build.gradle dependencies are shown below.
-
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")
}
-
-
- Final project structure will looks like below image now.
-
-
-
- Open command prompt at Project Root Directory to execute gradle commands.
-
-
-
- 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
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.
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.24bin
-
- Copy our war file “SpringMVCExample-1.0.0-BUILD-SNAPSHOT.war” to ${TOMCAT8_HOME}/webapps/ folder
-
- Open CMD Prompt at ${TOMCAT8_BIN} and start Tomcat server.
D:apache-tomcat-8.0.24bin>startup.bat
-
- Test our application
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.
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:
Observations between Gradle and Maven Projects
If we observe our both SpringMVCExample Maven and Gradle based projects, we can observe the following things.
- We are getting same war file and output.
- Maven’s build.xml is XML file where as Gradle’s build.gradle file is plain text file.
- Maven’s build.xml is too big and contains lot of XML configuration. Gradle’s build.gradle contains very minimal configuration.
- 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.