If you publish to external Tomcat using SpringBoot multi-module, you may encounter a variety of problems. This article summarizes the following eight principles and the solutions to the four problems that often arise during release. With these principles and solutions, you can solve almost all SpringBoot release problems.
Eight principles of SpringBoot multi-module publishing
1 is packaged in the release module, not the parent module
For example, the following project directory:
api
Public call module, package type set to JAR format
Public modules such as Common and Model need to set packaging to JAR format in pom.xml:
<packaging>jar</packaging>
Copy the code
3 Release module package type set to WAR format
In the published module pom.xml set:
<packaging>war</packaging>
Copy the code
4 Exclude the built-in Tomcat
In the published module pom.xml set:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
Copy the code
When scope=provided is set, this JAR package does not appear in the published project, thus eliminating the built-in Tomcat.
5 Set the startup class
This step tells tomcat where to start. Add the following code to the startup class:
@SpringBootApplication
public class ApiApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(ApiApplication.class);
}
public static void main(String[] args) { SpringApplication.run(ApiApplication.class, args); }}Copy the code
Be sure to exclude static files if using interceptors
For example, if I use Swagger in my project, THEN I need to exclude the static file of Swagger. The code is as follows:
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
// Exclude static files
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
// do something
}
Copy the code
Load the public module first, then publish the project
If the published module references other public modules for this project, the public modules for this project need to be loaded into the local repository first. Operation mode: Double-click the install of the parent module. After the installation is successful, click the package of the release module to generate the WAR package and complete the packaging of the project, as shown below:
8 Deployment Items
Once you have the WAR package, you just need to put a single WAR package into tomcat’s webapps directory and restart Tomcat, as shown below:
Possible problems and solutions
Question 1: Does SpringBoot configure a port number affect program release?
A: No, the configured server.port will be overwritten by the port number of Tomcat itself, which is configured in the tomcat/config/server. XML file.
Problem 2: Publishing error, can not find other modules or public modules in the project, what to do?
A: Because the parent maven’s install operation is not executed, install simply puts the public module into a local repository and makes it available to other projects.
I can’t find the main class SpringBoot is running on.
A: Because the startup class is not set, the setting method is:
- Pom.xml config startup class, config
configuration><mainClass>com.bi.api.ApiApplication</mainClass></configuration>
。 - Start class inheritance
SpringBootServletInitializer
implementationSpringApplicationBuilder
Method, the specific code refer to the fifth part of the article.
Can’t find xxx.jar package when deploying SpringBoot project to Tomcat 7?
A: This is because the SpringBoot version is too high and the Tomcat version is too low. If you are using the latest version of SpringBoot, consider upgrading Tomcat to the latest version of Tomcat 8.x+ to solve this problem.