WAR package deployment
Deploying Spring Boot apps as WAR packages and deploying them to external Tomcat servers requires some specific configuration and steps.
The following are detailed instructions:
1. Modify the packaging method
In the project's build tool configuration file (usuallyFor Maven, change the packaging method to
war
. Here are specific examples of the build tool:
Maven
existIn-house
<packaging>
Tags are set towar
:
<project> <!-- ... --> <packaging>war</packaging> <!-- ... --> </project>
2. Exclude built-in Tomcat
In order to prevent conflicts with external Tomcat servers, Spring Boot's embedded Tomcat dependencies need to be excluded in the build configuration.
In MavenAdd the following dependency exclusions:
<dependencies> <!-- ... --> <dependency> <groupId></groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId></groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <!-- ... --> </dependencies>
3. Configure SpringBootServletInitializer
In order for Spring Boot applications to be loaded by external Servlet containers such as Tomcat, it is necessary to create an inherited fromSpringBootServletInitializer
class and rewriteconfigure
method:
import ; import ; public class ServletInitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return (); } }
hereis your main application class, including
@SpringBootApplication
annotation.
4. Build WAR file
Use the corresponding build tool command to generate the WAR package:
Maven
mvn clean package
The generated WAR file is usually located intarget
In the directory, the file name isyour-project-name-<version>.war
。
5. Deploy to Tomcat
Copy the generated WAR file to Tomcat'swebapps
In the directory.
If you are running the Tomcat service, it will automatically detect the newly deployed WAR files and decompress and deploy them.
If you have not started Tomcat yet, it will also process the newly deployed WAR files after startup.
6. Access the application
Once the deployment is complete, you can access your Spring Boot app using the following URL:
http://localhost:8080/your-project-name
Hereyour-project-name
Usually the base name of the WAR file (without version number). If the context path is changed during deployment, the access URL should be adjusted accordingly.
Notes:
- Dependency conflict: As with JAR deployments, make sure there are no dependency conflicts, especially those related to Tomcat.
- External configuration: Avoid hard-code sensitive configurations in WAR packages, use environment variables, external configuration files, or configuration service management.
- Log configuration: Following Tomcat's log configuration specification, Spring Boot's log configuration may need to be adjusted to suit the Tomcat environment.
- Health checks and monitoring: Spring Boot Actuator can also be utilized, but the endpoint path may need to be adjusted to suit the context path of Tomcat deployment.
Follow the above steps to successfully package the Spring Boot application into a WAR file and deploy it to the Tomcat server.
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.