Today I want to share with you how do I use Springboot to create a war package that can be deployed in an external tomcat? The internet is a lot of talk, but I still want to record:
Springboot official documentation is already clear (85.1 Create a deployable war File)
1, the main class inherits Springbootservletinitializer and overrides its configure Method.
@SpringBootApplication
public class application extends springbootservletinitializer {
@Override
Protected springapplicationbuilder configure (springapplicationbuilder application) {
Return application.sources ( application.class);
}
public static void main (string[] args) throws exception {
springapplication.run (application.class, args);
}
}
2, Modify your build configuration so that your project generates a war file instead of a jar file. If you use Maven and use spring-boot-starter-parent This class, you only need to modify pom.xml
packaging is a war. (students unfamiliar with Maven can learn about maven First)
<packaging>war</packaging>
If you use Gradle, you need to modify the build.gradle App war plugin
Apply Plugin: ' War '
3. The final step in the process is to ensure that the embedded servlet container does not interfere with the servlet container that will deploy the war File. To do this, you need to tag the provided inline servlet container dependency as Provided.
MAVEN Configuration
<dependencies> <!--...--> <dependency> <groupid>org.springframework.boot</groupid& Gt <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </depende Ncy> <!--...--></dependencies>
Gradle Configuration
Dependencies {//... providedruntime ' Org.springframework.boot:spring-boot-starter-tomcat '//...}
Springboot Creating a war package deployed on external Tomcat