Categories: JavaWeb

Gradle War Configuration for Eclipse Spring Web Application Project

The article describes the configuration one would need to create WAR file for an Eclipse-base web application (Dynamic Web) project.

Below code represents whats needed to compile your Java Eclipse-based web application project and create WAR file which can be deployed later, on any Java server such as Tomcat. Pay attention to some of the following facts:

  • repositories: “repositories” is referred in a local directory rather than external repository such as MavenCentral etc.
  • sourceSets: sourceSets define the path under which Java source code exists. It MUST BE NOTED that failing to define this would have Gradle look into Maven-based folder such as “src/main/java” for source code that would eventually result into failure. This primarily because Eclipse-based dynamic web projects place the Java code under “src” folder.
  • dependencies: Compile time dependencies is referred from within local directory.
  • war: Following are key war-related configurations:
    • WebInf is used to store files within WebContent/WEB-INF folder
    • classpath is used to copy libraries within WebContent/WEB-INF/lib folder
    • WebXML is used to locate path to web.xml (WebContent/WEB-INF/web.xml)
    • from { path } is used to copy files under WebContent folder. These are files such as static resources, html/jsp files if any.

 

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

// Depending upon the fact that one uses default
// WebContent folder to store web resources. Change
// name to name of WebContent folder appropriately.
def webInfPath = "WebContent/WEB-INF"

// Repositories accessed from local directory
repositories {
flatDir { dirs webInfPath + "/" + "lib" }
}

// Source folder defined as Eclipse stores Java source code
// in non-maven based folder structure
sourceSets {
main {
java { srcDir 'src' }
}
}

dependencies {
// Compile time dependencies read from local directory
compile fileTree(dir: webInfPath + "/" + "lib", include: '*.jar')
}

war {
webInf { from webInfPath} // adds a file-set to the WEB-INF dir.
classpath fileTree(webInfPath + "/" + "lib") // adds a file-set to the WEB-INF/lib dir.
webXml = file(webInfPath + "/" + "web.xml") // copies a file to WEB-INF/web.xml
from('WebContent') { include ('bootstrap/css/*.css', 'bootstrap/js/*.js', 'bootstrap/img/*.*') } //Copy Bootstrap folder to WEB-INF level
}

[adsenseyu1]

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.

View Comments

  • I have a small problem connected to Gradle and web plugin.
    I got spring mvc resources imported to my web project. But after running my web project under Tomcat, it seems that I have no src/main/resources files under my classpath. I was expecting gradle to take care of it, but it seems I have to config war manually. Any hints on this?

Recent Posts

Agentic Reasoning Design Patterns in AI: Examples

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

1 month ago

LLMs for Adaptive Learning & Personalized Education

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

1 month ago

Sparse Mixture of Experts (MoE) Models: Examples

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

2 months ago

Anxiety Disorder Detection & Machine Learning Techniques

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

2 months ago

Confounder Features & Machine Learning Models: Examples

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

2 months ago

Credit Card Fraud Detection & Machine Learning

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

2 months ago