问题描述
在springboot的gradle项目中,需要引入hibernate-validator包进行一些参数校验。本地开发环境运行的时候没有报错,打包成war包到tomcat运行的时候报错,项目启动不起来。
报错信息
Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.validator.internal.engine.ConfigurationImpl
解题思路
本地开发环境运行不会报错,打包成war包到tomcat运行的时候报错,那么会不会是某个包没打进去。所以第一步先检查gradle的文件,看下有没有做排包的动作。
果然
在gradle包文件里找到了一个
providedRuntime 'org.springframework.boot:spring-boot-starter-undertow'
providedRuntime指令在gradle的官方文档里面的解释如下:
The War plugin adds two dependency configurations:
providedCompile
providedRuntime
These two configurations have the same scope as the respective
compile
andruntime
configurations, except that they are not added to the WAR archive.It is important to note that these
provided
configurations work transitively. Let’s say you addcommons-httpclient:commons-httpclient:3.0
to any of the provided configurations. This dependency has a dependency oncommons-codec
. Because this is a “provided” configuration, this means that neither of these dependencies will be added to your WAR, even if thecommons-codec
library is an explicit dependency of yourcompile
configuration. If you don’t want this transitive behavior, simply declare yourprovided
dependencies likecommons-httpclient:commons-httpclient:3.0@jar
.
也就是说providedRuntime
修饰的包,打包成war包的时候,会把这个包底下的包全部排掉,会导致其他包的一些引用被排掉。
那么,hibernate-validator是什么包被排掉了呢?,对比了下两个包的引用,是jboss-logging包被排掉了
image-20210208142400072.png
问题解决
-
修改
providedRuntime
为compile -
我们来一波负负得正。
providedRuntime('org.springframework.boot:spring-boot-starter-undertow') { exclude group: 'org.jboss.logging', module: "jboss-logging" }
网友评论