Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Spring Boot - Packaging
Next article icon

Spring Boot - Packaging

Last Updated : 09 Jan, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The Spring Framework was created to provide an open-source application framework to feature better infrastructure support for developing Java applications. It is one of the most popular Java Enterprise Edition (Java EE) frameworks, Spring framework helps developers in creating high-performing applications using plain old Java objects (POJOs). However, working with Spring is a cumbersome process because we have to manage its configurations manually. Here comes the Spring-Boot which was developed to ease the application development process with the help of its main feature Auto-configuration. Also, at the center of essential features of Spring Boot is its Packaging feature.

Pre-requisites:

  • All packaging types are suitable for their respective different constraints.
  • Therefore, you should the respective packaging type which is better for your application's constraints.

Importance of Spring Boot Application Packaging

  • Spring Boot's packaging feature helps to manage the application and its modules which further is important for application deployment.
  • Maven and Gradle are the most used package managing technologies that the Spring and Spring Boot support.
  • They both provide their respective plugins which actually manage to package - (Spring Boot Maven Plugin / Spring Boot Gradle Plugin).
  • They ensure that all dependency libraries are included within the executable JAR file and available on the runtime classpath.
  • Spring Boot Maven Plugin allows developers to package executable jar or war archives with running the application in place. Spring provides embedded container support.
  • It also gives us the option to exclude dependencies to avoid potential jar conflicts when deploying in an external container.
  • It also helps to run the 'jar' files independently without the use of the jar command - (java -jar).
Packaging as a container and manager

Packaging Types in Spring Boot

  1. jar
  2. war
  3. pom
  4. ear
Packaging methods used hierarchy

1. JAR Packaging

  • JAR stands for Java Archive.
  • It encapsulates one or more Java classes ( EJB - Enterprise Java Beans ), a manifest, and a descriptor ( eg - XML file) is called a JAR file.
  • Its extension is '.jar'
  • It is the lowest level packaging.
  • JAR is traditional packaging used to package libraries (java classes and metadata) and the occasional desktop UI application.
  • In Spring Boot Applications, 'jar' is the default packaging that is deployed in Spring's embedded servers.
  • Here, the choice of JAR packaging is a cloud-minded choice.
  • All java cloud platforms are capable of running an executable JAR file.
  • Therefore, the 'Spring Initializr' makes JAR packaging default unless you tell it to do otherwise.
  • If you’re building an executable JAR file, you must choose any of the - Thymeleaf, FreeMarker, Groovy Templates, JSP Java Server Pages, Mustache.
  • Spring Boot Maven Plugin produces a manifest file in the JAR file that denotes the bootstrap class as the main class for the executable JAR.
  • After building your application with maven packages (Right-click on the project -> Run As -> Maven Build or Maven Clean), your application's '.jar' file will be created in the 'target' folder.
  • After running the '.jar' file, you can make a request e.g. - http://localhost:8080/method mapping...
  • As the jar is the default, you do not need to include the packaging tag in pom.xml (Maven build).

Example - pom.xml

XML
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0                         https://maven.apache.org/xsd/maven-4.0.0.xsd">     <modelVersion>4.0.0</modelVersion>     <parent>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-parent</artifactId>         <version>2.6.4</version>         <relativePath/> <!-- lookup parent from repository -->     </parent>     <groupId>sia</groupId>     <artifactId>Geeks</artifactId>     <version>0.0.1-SNAPSHOT</version>     <name>Geeks</name>     <description>GFG</description>     <properties>         <java.version>11</java.version>     </properties>     <dependencies>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter</artifactId>         </dependency>          <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-test</artifactId>             <scope>test</scope>         </dependency>     </dependencies>      <build>         <plugins>             <plugin>                 <groupId>org.springframework.boot</groupId>                 <artifactId>spring-boot-maven-plugin</artifactId>             </plugin>         </plugins>     </build> </project> 

Note: 

  • Your application must have a main class that will be executed when the JAR file is run.
  • You will also need a minimal amount of Spring Configuration for bootstrapping the application.

Example: GfgWebApplication.java (Main class - Bootstrapping of Spring Boot Application)

Java
package gfg;  import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;  @SpringBootApplication public class GfgWebApplication {      public static void main(String[] args) {         SpringApplication.run(GfgWebApplication.class, args);     }  } 

2. WAR Packaging

  • WAR stands for Web Archive.
  • Traditional Java web applications are packaged as WAR files.
  • The WAR files are perfectly suitable for deploying to a traditional Java application server.
  • Some cloud platforms (such as CloudFoundry) are capable of deploying and running WAR files.
  • You need to include '<packaging>war</packaging>' in pom.xml - Maven build file.
  • Web module contains servlet classes, JSP files, HTML files, JavaScripts, etc. are packaged as a JAR file with .war extension.
  • It contains a special directory called WEB-INF.
  • Java servlet containers - including embedded Tomcat and Jetty containers - usually look for JSPs somewhere under /WEB-INF.
  • But if you’re building your application as an executable JAR file, there’s no way to satisfy that requirement.
  • Therefore, while packaging with WAR, you need to work only with JSP ( Java Server Pages ) within the web pages of your application.

Example: pom.xml (Maven)

XML
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0                               https://maven.apache.org/xsd/maven-4.0.0.xsd">     <modelVersion>4.0.0</modelVersion>     <parent>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-parent</artifactId>         <version>2.6.3</version>         <relativePath/> <!-- lookup parent from repository -->     </parent>     <groupId>sia</groupId>     <artifactId>GFG</artifactId>     <version>0.0.1-SNAPSHOT</version>     <packaging>war</packaging>     <name>GFG</name>     <description>GeeksforGeeks</description>     <properties>         <java.version>11</java.version>     </properties>     <dependencies>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-thymeleaf</artifactId>         </dependency>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-web</artifactId>         </dependency>          <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-test</artifactId>             <scope>test</scope>         </dependency>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter</artifactId>         </dependency>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-devtools</artifactId>             <scope>runtime</scope>             <optional>true</optional>         </dependency>     </dependencies>      <build>         <plugins>             <plugin>                 <groupId>org.springframework.boot</groupId>                 <artifactId>spring-boot-maven-plugin</artifactId>             </plugin>         </plugins>     </build>  </project> 

Note: When you choose WAR packaging for application deployment to a traditional Java application server, you need to include a web initializer class.

Example: GfGApplication.java (Main class - Bootstrapping of application)

Java
package gfg;  import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;  @SpringBootApplication public class GfGApplication extends SpringBootServletInitializer {      public static void main(String[] args) {         SpringApplication.run(GfGApplication.class, args);     }  } 

Working with WAR packaging:

  • To build our Tomcat-deployable WAR application, we execute the mvn package (Right-click on the project -> Run As -> Maven Build or Maven Clean).
  • After refreshing, our WAR file is generated at target/________.war.
  • Copy our WAR file from target/________.war to the tomcat/webapps/
  • From a terminal navigate to tomcat/bin folder and execute - catalina.bat run (on Windows), catalina.sh run (on Unix-based systems).
  • Go to http://localhost:8080/'.war file name'/method mapping.

3. POM Packaging

  • We can use Spring Boot to make the application as the Multi-Module Project.
  • In a Multi-Module Project, an application is divided into multiple modules, where each module plays an important role in the certain functionality of an application.
  • A module can be considered as an independent project or sub-project.
  • These modules also coordinate with other modules.
  • Therefore, we need to manage them with the help of pom packaging.
  • pom packaging acts as the container for other sub-modules which are themselves are packaged in a jar or war files.
  • pom packaging can have sub-modules that have different packaging.
  • You need to include '<packaging>pom</packaging>' in pom.xml file.
  • They are declared and listed in the 'modules' tag.
Bundles the Modules

Example: pom.xml (Maven)

XML
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0                              https://maven.apache.org/xsd/maven-4.0.0.xsd">     <modelVersion>4.0.0</modelVersion>     <parent>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-parent</artifactId>         <version>2.6.3</version>         <relativePath/> <!-- lookup parent from repository -->     </parent>     <groupId>sia</groupId>     <artifactId>ParentGFG</artifactId>     <version>0.0.1-SNAPSHOT</version>     <packaging>pom</packaging>     <name>ParentGFG</name>     <description>Multi Module Project</description>     <properties>         <java.version>11</java.version>     </properties>     <dependencies>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-thymeleaf</artifactId>         </dependency>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-web</artifactId>         </dependency>          <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-devtools</artifactId>             <scope>runtime</scope>             <optional>true</optional>         </dependency>         <dependency>             <groupId>org.projectlombok</groupId>             <artifactId>lombok</artifactId>             <optional>true</optional>         </dependency>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-test</artifactId>             <scope>test</scope>         </dependency>     </dependencies>      <build>         <plugins>             <plugin>                 <groupId>org.springframework.boot</groupId>                 <artifactId>spring-boot-maven-plugin</artifactId>                 <configuration>                     <excludes>                         <exclude>                             <groupId>org.projectlombok</groupId>                             <artifactId>lombok</artifactId>                         </exclude>                     </excludes>                 </configuration>             </plugin>         </plugins>     </build>      <modules>         <module>GFG-Module1</module>         <module>GFG-Module2</module>     </modules> </project> 

4. EAR Packaging

  • EAR stands for Enterprise Archive.
  • Its file has a '.ear' extension.
  • It can contain multiple EJB - Enterprise Java Beans modules (JAR) and Web modules (WAR).
  • It is a special JAR that contains an application.xml file in the META-INF folder.

Understanding Spring Packaging in a nutshell

Spring modules are simply JAR files that package the required code for that module. After you understand the purpose of each module, we can select the modules required in our project and include them in our code. As of Spring version 5.0.0.RELEASE, Spring comes with 21 modules, packaged into 21 JAR files. The actual JAR file name is, for example, spring-aop-5.0.0.RELEASE.jar, though we have included only the specific module portion for simplicity.

Module

Description

aop

This module contains all the classes you need to use Spring’s AOP features within your application. You also need to include this JAR in your application if you plan to use other features in Spring that use AOP, such as declarative transaction management. Moreover, classes that support integration with AspectJ are packed in this module

aspects

This module contains all the classes for advanced integration with the AspectJ AOP library. For example, if you are using Java classes for your Spring configuration and need AspectJ-style annotation-driven transaction agement, you will need this module.

beans

This module contains all the classes for supporting Spring’s manipulation of Spring beans. Most of the classes here support Spring’s bean factory implementation. For example, the classes required for processing the Spring XML configuration file and Java annotations are packed into this module.

beans-groovy

This module contains Groovy classes for supporting Spring’s manipulation of Spring beans.

context

This module contains classes that provide many extensions to Spring Core. You will find that all classes need to use Spring’s ApplicationContext feature, along with classes for Enterprise JavaBeans (EJB), Java Naming and Directory Interface (JNDI), and Java Management Extensions (JMX) integration. Also contained in this module are the Spring remoting classes, classes for integration with dynamic scripting languages (for example, JRuby, Groovy, and BeanShell), JSR-303 (Bean Validation), scheduling and task execution, and so on.

context-indexer

This module contains an indexer implementation that provides access to the candidates that are defined in META-INF/spring.components. The core class CandidateComponentsIndex is not meant to be used externally.

context-support

This module contains further extensions to the spring-context module. On the user-interface side, there are classes for mail support and integration with templating engines such as Velocity, FreeMarker, and JasperReports. Also, integration with various task execution and scheduling libraries including CommonJ and Quartz are packaged here.

core

This is the main module that you will need for every Spring application. In this JAR file, you will find all the classes that are shared among all other Spring modules (for example, classes for accessing configuration files). Also, in this JAR, you will find selections of extremely useful utility classes that are used throughout the Spring codebase and that you can use in your own application.

expression

This module contains all support classes for Spring Expression Language (SpEL)

instrument

This module includes Spring’s instrumentation agent for JVM bootstrapping. This JAR file is required for using load-time weaving with AspectJ in a Spring application

dbc

This module includes all classes for JDBC support. You will need this module for all applications that require database access. Classes for supporting data sources, JDBC data types, JDBC templates, native JDBC connections, and so on, are packed in this module

jms

This module includes all classes for JMS support.

messaging

This module contains key abstractions taken from the Spring Integration project to serve as a foundation for message-based applications and adds support for STOMP messages

orm

This module extends Spring’s standard JDBC feature set with support for popular ORM tools including Hibernate, JDO, JPA, and the data mapper iBATIS. Many of the classes in this JAR depend on classes contained in the spring-jdbc JAR file, so you definitely need to include that in your application as well.

oxm

This module provides support for Object/XML Mapping (OXM). Classes for the abstraction of XML marshalling and unmarshalling and support for popular tools such as Castor, JAXB, XMLBeans, and XStream are packed into this module.

test

Spring provides a set of mock classes to aid in testing your applications, and many of these mock classes are used within the Spring test suite, so they are well tested and make testing your applications much simpler. Certainly we have found great use for the mock HttpServletRequest and HttpServletResponse classes in unit tests for our web applications. On the other hand, Spring provides a tight integration with the JUnit unit-testing framework, and many classes that support the development of JUnit test cases are provided in this module; for example, SpringJUnit4ClassRunner provides a simple way to bootstrap the Spring ApplicationContext in a unit test environment.

tx

This module provides all classes for supporting Spring’s transaction infrastructure. You will find classes from the transaction abstraction layer to support the Java Transaction API (JTA) and integration with application servers from major vendors.

web

This module contains the core classes for using Spring in your web applications, including classes for loading an ApplicationContext feature automatically, file upload support classes, and a bunch of useful classes for performing repetitive tasks such as parsing integer values from the query string.

web-reactive

This module contains core interfaces and classes for Spring Web Reactive model.

web-mvc

This module contains all the classes for Spring’s own MVC framework. If you are using a separate MVC framework for your application, you won’t need any of the classes from this JAR file.

websocket

This module provides support for JSR-356 (Java API for WebSocket).


Next Article
Spring Boot - Packaging

P

pawardarshan461
Improve
Article Tags :
  • Java
  • Geeks Premier League
  • Geeks-Premier-League-2022
  • Java-Spring-Boot
Practice Tags :
  • Java

Similar Reads

    Spring Boot - AOP(Aspect Oriented Programming)
    The Java applications are developed in multiple layers, to increase security, separate business logic, persistence logic, etc. A typical Java application has three layers namely they are Web layer, the Business layer, and the Data layer. Web layer: This layer is used to provide the services to the e
    4 min read
    Spring Boot - Cache Provider
    The Spring Framework provides support for transparently adding caching to an application. The Cache provider gives authorization to programmers to configure cache explicitly in an application. It incorporates various cache providers such as EhCache, Redis, Guava, Caffeine, etc. It keeps frequently a
    6 min read
    Spring Boot - AOP Around Advice
    Aspect-oriented programming(AOP) as the name suggests uses aspects in programming. It can be defined as the breaking of code into different modules, also known as modularisation, where the aspect is the key unit of modularity. Aspects enable the implementation of crosscutting concerns such as transa
    6 min read
    Spring Boot - Auto-configuration
    Spring Boot is heavily attracting developers toward it because of three main features as follows: Auto-configuration - such as checking for the dependencies, the presence of certain classes in the classpath, the existence of a bean, or the activation of some property.An opinionated approach to confi
    5 min read
    Spring Boot - EhCaching
    EhCache is an open-source and Java-based cache. It is used to boost performance. Its current version is 3. EhCache provides the implementation of the JSR-107 cache manager. Features of EhCache are given below: It is fast, lightweight, Flexible, and Scalable.It allows us to perform Serializable and O
    5 min read
    Difference Between Spring Boot Starter Web and Spring Boot Starter Tomcat
    Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and se
    3 min read
    Spring Boot - Starter Test
    Spring Boot is built on top of the spring and contains all the features of spring. It is becoming a favorite of developers these days because of its rapid production-ready environment, which enables the developers to directly focus on the logic instead of struggling with the configuration and setup.
    5 min read
    Exception Handling in Spring Boot
    Exception handling in Spring Boot helps deal with errors and exceptions present in APIs, delivering a robust enterprise application. This article covers various ways in which exceptions can be handled and how to return meaningful error responses to the client in a Spring Boot Project. Key Approaches
    8 min read
    Spring Boot - Project Deployment Using Tomcat
    Spring Boot is a microservice-based framework and making a production-ready application in it takes very little time. Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because it’s a rapid production-ready envir
    5 min read
    Spring Boot - Difference Between AOP and OOP
    AOP(Aspect-Oriented Programming) complements OOP by enabling modularity of cross-cutting concerns. The Key unit of Modularity(breaking of code into different modules) in Aspect-Oriented Programming is Aspect. one of the major advantages of AOP is that it allows developers to concentrate on business
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences