Open In App

Spring Boot – Packaging

Last Updated : 09 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
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"?>
         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"?>
         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"?>
         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).



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads