使用Initializr创建的Spring Boot项目在IntelliJ中无法启动Tomcat
问题:Spring Boot项目启动后直接退出,无Tomcat启动提示
我之前通过Spring Web Starter创建过多个Spring Boot项目,在IntelliJ IDEA社区版中都能正常启动并运行嵌入式Tomcat。但近期通过Initializr创建的新项目,在IntelliJ中运行时,日志未显示任何错误,却也没有Tomcat成功启动的常规提示,进程直接以退出码0结束。
运行日志如下:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v3.2.2) 2024-02-01T15:18:33.880+01:00 INFO 6100 --- [ main] o.t.t.Testspringboot2Application : Starting Testspringboot2Application using Java 21.0.2 with PID 6100 (/home/thomas/IdeaProjects/testspringboot2/target/classes started by thomas in /home/thomas/IdeaProjects/testspringboot2) 2024-02-01T15:18:33.886+01:00 INFO 6100 --- [ main] o.t.t.Testspringboot2Application : No active profile set, falling back to 1 default profile: "default" 2024-02-01T15:18:35.949+01:00 INFO 6100 --- [ main] o.t.t.Testspringboot2Application : Started Testspringboot2Application in 2.796 seconds (process running for 3.911) Process finished with exit code 0
我的pom.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.2.2</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>org.theiner</groupId> <artifactId>testspringboot2</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>testspringboot2</name> <description>Demo project for Spring Boot</description> <properties> <java.version>21</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
原因与解决办法
问题的核心在你的pom.xml配置里:
spring-boot-starter-tomcat的scope被设为provided:provided这个scope表示依赖只在编译和测试阶段有效,运行时不会被加入项目的classpath。而Spring Boot要启动嵌入式Tomcat,必须在运行时拥有这个依赖包。缺少它的情况下,Spring Boot会判定这是一个非Web项目,启动完成后直接退出,自然不会有Tomcat启动的日志。打包方式设为
war的冲突:
你把项目打包方式设为war,这通常是为了将项目部署到外部的Servlet容器(比如独立的Tomcat),此时把Tomcat依赖设为provided是合理的——因为外部容器会提供Tomcat相关类。但如果你是想本地运行嵌入式Tomcat,这个配置就和你的需求冲突了。
根据你的需求,有两种解决方向:
如果你要本地运行嵌入式Tomcat(开发阶段)
- 方案一(推荐):直接删除单独添加的
spring-boot-starter-tomcat依赖。因为spring-boot-starter-web已经默认包含了这个依赖,不需要额外声明,重复声明反而覆盖了默认的scope配置。 - 方案二:保留这个依赖,但移除
<scope>provided</scope>标签,让Tomcat依赖在运行时也能被加载。 - 可选调整:如果不需要打包成war包,把
<packaging>war</packaging>改成<packaging>jar</packaging>,这是Spring Boot项目的默认打包方式,更适合嵌入式运行场景。
如果你要打包成war部署到外部Tomcat
- 保留
<packaging>war</packaging>和spring-boot-starter-tomcat的providedscope即可。但如果需要本地临时运行嵌入式Tomcat,可以临时注释掉<scope>provided</scope>,开发完成后再恢复。
内容的提问来源于stack exchange,提问作者devnull69
相关产品推荐
相关产品推荐

