通过一次IDEA Spring Boot 启动报错深入理解maven scope标签

起因

昨天同事突然告诉我项目无法启动了, 但是神奇的是我也在用一样的代码, 但是在我的机器上并没有出现这个问题, 所以我就开始 fuck bug了,报错信息大概是下面这个样子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Caused by: java.lang.IllegalStateException: Failed to introspect annotated methods on class 
org.springframework.boot.web.support.SpringBootServletInitializer
at org.springframework.core.type.StandardAnnotationMetadata.
getAnnotatedMethods(StandardAnnotationMetadata.java:163) ~[spring-core-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.
retrieveBeanMethodMetadata(ConfigurationClassParser.java:380) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.
doProcessConfigurationClass(ConfigurationClassParser.java:314) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.
processConfigurationClass(ConfigurationClassParser.java:245) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.
parse(ConfigurationClassParser.java:198) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.
parse(ConfigurationClassParser.java:167) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
... 17 common frames omitted
Caused by: java.lang.NoClassDefFoundError: javax/servlet/ServletContext
at java.lang.Class.getDeclaredMethods0(Native Method) ~[na:1.8.0_111]
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) ~[na:1.8.0_111]
at java.lang.Class.getDeclaredMethods(Class.java:1975) ~[na:1.8.0_111]
at org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods
(StandardAnnotationMetadata.java:152) ~[spring-core-4.3.10.RELEASE.jar:4.3.10.RELEASE]
... 22 common frames omitted
Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletContext
at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_111]
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_111]
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_111]
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_111]
... 26 common frames omitted

解决方法

通过网上一番Goolge, 答案如下

1
2
3
4
5
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>

将pom.xml文件中tomcat依赖中的 scope 注掉, 问题解决.

scope标签的作用

Maven官网中是这么介绍scope的 传送门

Dependency scope is used to limit the transitivity of a dependency, and also to affect the classpath used for various build tasks.

There are 6 scopes available:

  • compile
    This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.
  • provided
    This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.
  • runtime
    This scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.
  • test
    This scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases. This scope is not transitive.
  • system
    This scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.
  • import (only available in Maven 2.0.9 or later)
    This scope is only supported on a dependency of type pom in the <dependencyManagement> section. It indicates the dependency to be replaced with the effective list of dependencies in the specified POM’s <dependencyManagement> section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.

通过上面的解释我们可以发现provided属性是可以在编译和运行期间引入的啊, 但是为啥会报错呢, 难道是Maven的Bug? 还是别的原因, 这里我又深究了一下, 结果如下:

研究结果

现在几乎可以确认应该不是Maven的, 在William_Cheung的博客 的文章中提到这个是IDEA的一个Bug, IDEA不会将标有provided加入到classpath中, 详细信息查看Add an option to provide runtime classpath with dependencies in provided scope
那么现在问题来了…..为啥我的IDEA可以呢???
从youtrack的评论中我找到了这条评论:
Guillaume Simard的评论
在评论中提到,这个bug已经在新版中得到解决, 这里我就不搬运了, 附上连接IntelliJ IDEA 2018.1 Public Preview

结论

现在看来, 已经非常明了了, 这个异常的原因是低版本IDEA的Bug导致的, 所以你可以不修改maven的配置, 换一个新版本的IDEA一样可以结局问题, 至于不想换IDEA也不想改配置的人….. 也有解决方案William_Cheung的博客中已经很明了了,我也不在此多说了.

0%