I want to create a war package and then run it externally on tomcat for a project written with boot recently.
- pom add dependencies
<!--外置tomcat启动-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
- Modify the pom packaging method packaging to war
<groupId>com.lyjn</groupId>
<artifactId>lyjn-back</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>lyjn-back</name>
- Transform the startup item, inherit SpringBootServletInitializer; rewrite the configure method. Because springboot can recognize its own startup items, but external tomcat does not, so you have to inherit it yourself and read the configuration
@MapperScan("com.lyjn.dao")
@SpringBootApplication
public class LyjnBackApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(LyjnBackApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(LyjnBackApplication.class, args);
}
}
At this point you can use the maven command to package
An error was encountered when it was packaged into a war package and placed in an external tomcat
For versions below tomcat8, put the war package directly in the webapp directory of tomcat, start and check the catania.out log:
**Important: **Start tomcat, you may encounter an error:
Caused by: java.lang.NoClassDefFoundError: javax/el/ELManager
is because: the el-api provided by tomcat conflicts with the el-api. Go to the lib directory of tomcat and overwrite the original jar package. But I did not find el-api.jar
Solution: Directly use tomcat8 version