美文网首页
使用hibernate-validator打包报错

使用hibernate-validator打包报错

作者: 西城丶 | 来源:发表于2021-02-08 14:31 被阅读0次
    问题描述

    在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 and runtime 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 add commons-httpclient:commons-httpclient:3.0 to any of the provided configurations. This dependency has a dependency on commons-codec. Because this is a “provided” configuration, this means that neither of these dependencies will be added to your WAR, even if the commons-codec library is an explicit dependency of your compile configuration. If you don’t want this transitive behavior, simply declare your provided dependencies like commons-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"
      }
      

    相关文章

      网友评论

          本文标题:使用hibernate-validator打包报错

          本文链接:https://www.haomeiwen.com/subject/yyoxxltx.html