Categories: Java

How to Gradle Spring MVC Web Project

The article describes steps that are required to build the a Spring MVC web application project using Gradle tool.

Step 1: Web Application Folder

Make sure you have maven-based web application folder. We recommend you to check our article published on different possible layouts of web application folders. As a recap, following is how the web application folder structure would look like:

  • src/main/java (Consists of Java files)
  • src/main/resources
  • src/main/scripts
  • src/main/webapps: This would further have following folder structure:
    • assets (publicly accessible files)
      • css
      • js
      • images
    • META-INF
    • WEB-INF
      • lib (spring/hibernate & other libraries)
      • views (jsp files)
    • hibernate.cfg.xml (if hibernate is used as well)
    • spring-servlet.xml
    • web.xml

 

Step 2: Create a Gradle Script

Create a gradle script in the root folder. Name it as build.gradle. Following is how the gradle script would look like:

apply plugin: 'java'
apply plugin: 'war'

repositories {
    flatDir { dirs "src/main/webapp/WEB-INF/lib" }
    mavenCentral()
}

dependencies {        
        compile 'org.springframework:spring-webmvc:4.0.3.RELEASE'
        compile 'org.springframework:spring-core:4.0.3.RELEASE'
        compile 'org.springframework:spring-aop:4.0.3.RELEASE'       
        compile 'org.springframework:spring-web:4.0.3.RELEASE'       
        compile 'org.springframework:spring-beans:4.0.3.RELEASE'  
        compile 'org.springframework:spring-context:4.0.3.RELEASE'  
        compile 'org.springframework:spring-tx:4.0.3.RELEASE'  
        compile 'org.hibernate:hibernate-core:4.3.5.FINAL'  
        compile 'org.hibernate:hibernate-jpa:2.1-api-1.0.0.FINAL'          
}

war {    
    webInf { from 'src/main/webapp/WEB-INF'} // adds a file-set to the WEB-INF dir.    
    classpath fileTree('src/main/webapp/WEB-INF/lib') // adds a file-set to the WEB-INF/lib dir.
    webXml = file('src/main/webapp/WEB-INF/web.xml') // copies a file to WEB-INF/web.xml        
    from('src/main/webapp') { include ('bootstrap/css/*.css', 'bootstrap/js/*.js', 'bootstrap/img/*.*')  }
}

task deploy (dependsOn: war){
copy {
    from "build/libs"
    into "D:/Apache Software Foundation/Tomcat 8.0/webapps"
    include "*.war"
}
}

 

Step 3: Build using Gradle

Last step is to build the web application. Go to root folder of web application. Execute the command “gradle war” to build. Optionally, execute “gradle deploy” and the web application will be built and the war file will be deployed in tomcat webapps folder.

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.

Recent Posts

Agentic Reasoning Design Patterns in AI: Examples

In recent years, artificial intelligence (AI) has evolved to include more sophisticated and capable agents,…

2 months ago

LLMs for Adaptive Learning & Personalized Education

Adaptive learning helps in tailoring learning experiences to fit the unique needs of each student.…

3 months ago

Sparse Mixture of Experts (MoE) Models: Examples

With the increasing demand for more powerful machine learning (ML) systems that can handle diverse…

3 months ago

Anxiety Disorder Detection & Machine Learning Techniques

Anxiety is a common mental health condition that affects millions of people around the world.…

3 months ago

Confounder Features & Machine Learning Models: Examples

In machine learning, confounder features or variables can significantly affect the accuracy and validity of…

3 months ago

Credit Card Fraud Detection & Machine Learning

Last updated: 26 Sept, 2024 Credit card fraud detection is a major concern for credit…

3 months ago