Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to pack SpringBoot into war package and run under tomcat or wildfly

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "SpringBoot how to play war package to run under tomcat or wildfly", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "How to make SpringBoot into a war package to run under tomcat or wildfly"!

The default packaging method of springboot is jar package. The advantage of jar package is that it does not need to change the configuration, and tomcat is embedded. If it is packaged into jar package, it can be directly thrown into docker to run tests. However, considering that the front-end code can only be printed as war packages, it is better to also print the back-end code as war packages and run them in webapps in tomcat containers.

SpringBoot Packaging Form

By default, SpringBoot projects are created packaged as jar packages. At the same time, the tomcat container is built in.

So, if you want to package springboot into a war package and deploy it to a container such as wildfly, what should you do?

We'll start by creating the project.

create a project

There is no difficulty in creating a project. Use the idea tool to create a simple maven project. If your project is already a springboot project and you want to modify it to a war package, you can modify it accordingly.

Modify pom.xml configuration

After creating the project, modify the pom.xml file to add the parent tag.

org.springframework.boot spring-boot-starter-parent 2.1.5.RELEASE

If the original is a springboot project, it is already included and does not need to be modified.

Set the packaging method to war in pom.xml.

war

Add or modify dependency configuration as follows:

org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat javax.servlet servlet-api 2.5 org.springframework.boot spring-boot-starter-test test

In spring-boot-starter-web, tomcat dependencies need to be excluded. At the same time, add a dependency on servlet-api. Note that the dependency added here is Servlet 2.5.

Use the following plug-ins to package in build.

org.apache.maven.plugins maven-war-plugin 2.6 false

Start main method

If there is already a main method to start, let the main method inherit SpringBootServletInitializer and implement its methods. If it is a newly created project, create SpringBootApp directly in the top-level directory and launch diam as follows.

@SpringBootApplicationpublic class SpringBootApp extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(SpringBootApp.class, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(SpringBootApp.class); }}

packaged

The maven command executes package packaging, and the war package can be generated under the target directory.

As for tomcat and wildfly deployment, you can place the war package in the corresponding directory to start the service.

abnormal situation

If an exception similar to the following occurs at startup (prompt missing web.xml):

Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)

Note that the pom file packaging plug-ins and whether the tomcat problem is eliminated, check whether the packaging plug-ins above are correct.

The reason for the problem is that the springboot project references the dependency package spring-boot-starter-web. The spring-boot-starter-tomcat referenced in this package contains the tomcat embedded servlet container, and different versions of it implement different servlet version specifications.

Servlet/JSP version 2.5/2.1 supports Tomcat version 6.0.x with minimum java version 1.5. Servlet/JSP version 3.0/2.2 supports Tomcat version 7.0.x with minimum java version 1.6.

Because Servlet 2.5 was used above, the exception above occurs when there is no web.xml file.

Workaround 1: When Servlet version is less than 3, use Create/src/main/webapp/WEB-INF/web.xml.

Solution 2: Add maven-war-plugin under pom.xml build. And set failOnMissingWebXml to false.

At this point, I believe that everyone has a deeper understanding of "how SpringBoot can be run as a war package under tomcat or wildfly". Let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report