Updated April 1, 2023
Definition of Spring Boot War
Spring boot war is defined as creating packaging of application as a war file. As we know that default packaging of the application is jar is deployed on tomcat server. We can also generate war file deployment of spring boot applications by using tomcat, JBoss, and weblogic server instances. To create or convert jar file into war packaging file of spring boot application we need to declare packaging type as war.
Overview
- It uses the tomcat server which makes to run our web application easily. By default, application code and libraries are packaged in a jar file.
- To convert the executable jar file into the executable war file we need to follow the below steps are as follows.
1) First we need to disable tomcat server in the application.
2) After disabling tomcat server next step is to change the package type from jar to war file.
3) After changing package type next step is to extend the main application class of servlet initializer.
4) After initializing main class next or final step is to upload the project war file into the webapps directory of tomcat server.
- To disable the tomcat server in the application we need to tell tomcat server not to use the tomcat as servlet container. To disable the tomcat server, we need to add the starter tomcat dependency in pom.xml file of the application.
- To change the packaging type of the application from jar to war file we need to change the packaging type of application as war in pom.xml file.
- This application contains the main method which was used to run or launch the application. To extend the SpringBootServletInitializer in the main application class we need to modify the class and need to extend the SpringBootServletInitializer and after that, we need to configure override method. It will making the application is compatible while running inside into the servlet container.
- To build the war file using maven we need to execute the below commands are as follows.
# mvn clean package
- To build the war file using gradle we need to execute the below commands are as follows.
# gradle war
- To upload the war file into the tomcat server we need to upload the war file into webapps directory of tomcat server.
- In this application jar file with embedded server solution is not suitable for all the production-ready environments because deployment team wants full control on server, they are not touching any code so we need war file of the application.
- Spring boot webflux is not depends on application and servlet API which was deployed on netty server, project war file deployment is not supported by webflux applications.
- Extending the main class and configure the package war file are the main steps to create project war file in the application.
- We can generate spring boot application war file to deploy inside containers. Maven plugin is supporting to create war file of the maven project.
- The gradle plugin is supporting to create war file of gradle project. We can create a war file of our application using the gradle plugin.
Create WAR File Spring Boot
Below examples shows to create war file of the project are as follows.
1) Create project template using spring initializer and give name to project –
In the below step, we have provided project group name as com. example, artifact name as springbootWar, project name as springbootWar, and selected java version as 8.
Group – com.example Artifact name – springbootWar
Name – springbootWar Description – Project for springbootWar
Spring boot – 2.5.5 Project – Maven project
Java – 8 Package name – com.example.springbootWar Dependencies – spring web.
2) After generating project extract files and open this project by using spring tool suite –
3) After opening project using spring tool suite check the project and its files –
4) Add dependency packages –
Code:
<dependency> -- Start of dependency tag.
<groupId>org.springframework.boot</groupId> -- Start and end of groupId tag.
<artifactId>spring-boot-maven-plugin</artifactId> -- Start and end of artifactId tag.
</dependency> -- End of dependency tag.
5) Extend the main class –
Code:
@SpringBootApplication
public class warapp extends SpringBootServletInitializer{
public static void main/* main method of spring boot war app */ (String[] args)
{
SpringApplication.run (warapp.class, args);
}
}
6) Override the configure method –
Code:
@SpringBootApplication
public class warapp extends SpringBootServletInitializer
{
public static void main/* main method of spring boot war app */ (String[] args)
{
SpringApplication.run (warapp.class, args);
}
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder)
{
return builder.sources (warapp.class);
}
}
7) Configure the packaging war –
Code:
<packaging>war</packaging> -- Start and end of packaging tag.
8) Build the spring boot war project –
Spring Boot Examples
Below example shows spring boot using packaging as war file are as follows.
1) Create project template using spring initializer and give name to project –
In the below step we have provided project group name as com. example, artifact name as springboot_war, project name as springboot_war, and selected java version as 8.
Group – com.example
Artifact name – springboot_war
Name – springboot_war
Description – Project for springboot_war
Spring boot – 2.5.5
Project – Maven project
Java – 8
Package name – com.example.springboot_war
Dependencies – spring web.
2) After generating project extract files and open this project by using spring tool suite –
3) After opening project using spring tool suite check the project and its files –
4) Create simple class for spring boot war application –
Code:
public class springbootwar
{
public static void main /* main method of spring boot war application */ (String[] args)
{
SpringApplication.run (springbootwar.class, args);
System.out.println ("Simple code of spring boot war application");
}
}
5) Run the spring boot application using war file –
Conclusion
Spring boot uses the tomcat server which makes to run our web application easily. By default, application code and libraries are packaged in a jar file. Spring boot war is defined as to create packaging of application as war file.
Recommended Articles
This is a guide to Spring Boot War. Here we discuss definition, overviews, Create WAR File Spring Boot, examples with code implementation. You may also have a look at the following articles to learn more –