SpringBoot打war包并配置外部Tomcat运行
    		       		warning:
    		            这篇文章距离上次修改已过419天,其中的内容可能已经有所变动。
    		        
        		                
                在Spring Boot中打war包并部署到外部Tomcat服务器需要进行以下步骤:
- 修改项目的pom.xml文件,设置打包方式为war。
- 排除Spring Boot内置的Tomcat,因为要部署到外部Tomcat。
- 添加provided作用域来排除内嵌的Servlet和Tomcat依赖。
以下是修改后的pom.xml文件的关键部分:
<packaging>war</packaging>
 
...
 
<dependencies>
    ...
    <!-- 排除内置的Tomcat依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!-- 添加provided作用域 -->
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-core</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-el</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-websocket</artifactId>
        <scope>provided</scope>
    </dependency>
    ...
</dependencies>
 
...
 
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <!-- 这个配置非常重要,防止Spring Boot内置的Tomcat启动 -->
            <configuration>
                <excludes>
                    <exclude>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-tomcat</artifactId>
                    </exclude>
                </excludes>
            </configuration>
        </plugin>
    </plugins>
</build>在完成这些配置后,你可以通过Maven命令来打包你的应用程序:
mvn clean package打包成功后,将生成的war文件部署到外部Tomcat服务器。启动Tomcat服务器,你的Spring Boot应用将作为war部署运行。
确保你的Spring Boot应用中没有包含SpringBootServletInitializer的实现。如果有,确保继承SpringBootServletInitializer并重写configure方法:
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
 
public class Application extends SpringBootServletInitializer {
 
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
 
    p
评论已关闭