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). But 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 is 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, 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 Types in Spring Boot
- jar
- war
- pom
- ear
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 mvn 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 eg – 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" ?> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 < 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" ?> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 < 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 >Lazyroar</ 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.
Example – pom.xml (Maven)
XML
<? xml version = "1.0" encoding = "UTF-8" ?> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 < 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.