今天用maven+springboot的时候,写了个helloWorld程序,结果遇到了依赖包冲突,然后百度上的解决冲突的方法都试过了(声明优先 ,exclusion标签,申明包的范围为provided),发现还是没用,求指点。
- 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>titan</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>titan Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<!--springboot 父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<!-- Spring Boot web依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- resources目录加了properties文件后必须加上,不然项目报错 -->
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
</dependency>
**<!-- tomcat的支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>catalina</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 访问jsp页面必须配置 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>**
<!-- 调试单元 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<!--如果项目名创建的不一样,都要改成你自己的 -->
<finalName>titan</finalName>
<plugins>
<plugin>
<!--springframework插件配置 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2 报错的原因
2021-09-16 14:22:27.548 INFO 11476 --- [ restartedMain] com.dota2.rpg.titan.Test : Starting Test using Java 11.0.8 on DESKTOP-UCBBM4H with PID 11476 (F:\soft_mlz\titan\target\classes started by 39225 in F:\soft_mlz\titan)
2021-09-16 14:22:27.550 INFO 11476 --- [ restartedMain] com.dota2.rpg.titan.Test : No active profile set, falling back to default profiles: default
2021-09-16 14:22:27.582 INFO 11476 --- [ restartedMain] o.s.b.devtools.restart.ChangeableUrls : The Class-Path manifest attribute in H:\Tomcat10\apache-tomcat-10.0.11\lib\jakartaee-migration-1.0.0-shaded.jar referenced one or more files that do not exist: file:/H:/Tomcat10/apache-tomcat-10.0.11/lib/null
2021-09-16 14:22:27.583 INFO 11476 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2021-09-16 14:22:27.583 INFO 11476 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2021-09-16 14:22:28.237 WARN 11476 --- [ restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is java.lang.NoSuchMethodError: org.apache.catalina.Context.addServletContainerInitializer(Ljavax/servlet/ServletContainerInitializer;Ljava/util/Set;)V
2021-09-16 14:22:28.243 INFO 11476 --- [ restartedMain] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-09-16 14:22:28.254 ERROR 11476 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.configureContext(TomcatServletWebServerFactory.java:366)
The following method did not exist:
org.apache.catalina.Context.addServletContainerInitializer(Ljavax/servlet/ServletContainerInitializer;Ljava/util/Set;)V
The method's class, org.apache.catalina.Context, is available from the following locations:
jar:file:/H:/Tomcat10/apache-tomcat-10.0.11/lib/catalina.jar!/org/apache/catalina/Context.class
jar:file:/C:/Users/39225/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/9.0.52/tomcat-embed-core-9.0.52.jar!/org/apache/catalina/Context.class
The class hierarchy was loaded from the following locations:
org.apache.catalina.Context: file:/H:/Tomcat10/apache-tomcat-10.0.11/lib/catalina.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of org.apache.catalina.Context
上面的错误显示调用的是tomcat自带的catalina包里面的context类,然而这个类并没有Context.addServletContainerInitializer方法,所以报错。理论上按照pom.xml配置应该调用的是我配置的embed/tomcat-embed-core这里面的Context类呀,最关键我都加了exclusion标签还是没用。
3 附上网上查到的catalina的id标签,表示应该不是我exclusion里面的id填错了,两种都有试过
4 终于找到原因了,原来是springboot和tomcat的版本不对应导致的,我原先下的springboot:2.5.4 tomcat:10.0.11,后面换成tomcat:9.0.53 springboot:2.3.12.RELEASE,然后就好了(吐血)
附上查到的springboot2.3.12对应的tomcat版本,目前貌似最高支持的是9.0系列,后面有个update version:10.0.11,没看懂怎么用