eclipse Maven 使用错误版本的 javax.validation

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47947293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 23:31:14  来源:igfitidea点击:

Maven using wrong version of javax.validation

eclipsehibernatemavenpersistence.xml

提问by Connor Butch

I have a dynamic web project I have been working on that uses hibernate as a jpa provider. Up until the last week, I could insert, update, query, and delete from my databases using hibernate. Recently, I began working on validation, and brought in a lot of different maven dependencies. In the course of doing this, I somehow have ended up with my project using an older, deprecated version of javax.validation jar, which throws a no such method exception. I have included the relevant lines of the stack trace below:

我有一个我一直在研究的动态 Web 项目,它使用 hibernate 作为 jpa 提供程序。直到上周,我都可以使用 hibernate 从我的数据库中插入、更新、查询和删除。最近开始做验证,引入了很多不同的maven依赖。在执行此操作的过程中,我不知何故使用 javax.validation jar 的较旧的、已弃用的版本结束了我的项目,该版本引发了没有此类方法的异常。我在下面包含了堆栈跟踪的相关行:

Caused by: java.lang.NoSuchMethodError: javax.validation.BootstrapConfiguration.getClockProviderClassName()Ljava/lang/String;
at org.hibernate.validator.internal.xml.ValidationBootstrapParameters.<init>(ValidationBootstrapParameters.java:63)

However, in my pom.xml I have the following dependency (which, when examined via javap and through eclipse, it has the method getClockProviderClassName() in the specified class):

但是,在我的 pom.xml 中,我有以下依赖项(当通过 javap 和 eclipse 检查时,它在指定的类中有 getClockProviderClassName() 方法):

<!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>2.0.0.Final</version>
    </dependency>   

I put the following exclusion tag in my pom in multiple dependencies to try and ensure that it does not pull this jar from other sources:

我将以下排除标记放在我的 pom 中的多个依赖项中,以尝试确保它不会从其他来源提取此 jar:

            <exclusion>
                <artifactId>validation-api</artifactId>
                <groupId>javax.validation</groupId>
            </exclusion>

I have also simplified my build path, so there is three elements in it: one properties folder that contains only my log4j configuration, the JRE System library (version 1.8) and maven dependencies. Is there any way to tell where the deprecated javax.validation jar is coming from? Can I force the program to use version 2.0.0 I bring in with maven? Thank you for your help.

我还简化了我的构建路径,因此其中包含三个元素:一个属性文件夹,其中仅包含我的 log4j 配置、JRE 系统库(版本 1.8)和 maven 依赖项。有什么方法可以判断已弃用的 javax.validation jar 来自哪里?我可以强制程序使用我在 maven 中引入的 2.0.0 版本吗?感谢您的帮助。

Additional Information:
full stack trace

附加信息:
完整堆栈跟踪

persistence.xml source

persistence.xml 源代码

working datasource properties

工作数据源属性

Java Build Path

Java 构建路径

Line where root exception occurs (in my code)

发生根异常的行(在我的代码中)

look inside the included jar to see it contains the method

查看包含的 jar 以查看它包含该方法

using javap to examine artifact in maven (m2) directory

使用 javap 检查 maven (m2) 目录中的工件

dependency overview from maven

来自 maven 的依赖概述

Dependencies from pom.xml

来自 pom.xml 的依赖

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.0.2.RELEASE</version>
        <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.2.2.Final</version>
        <exclusions>
            <exclusion>
                <artifactId>jboss-logging</artifactId>
                <groupId>org.jboss.logging</groupId>
            </exclusion>
            <exclusion>
                <artifactId>validation-api</artifactId>
                <groupId>javax.validation</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>6.0.3</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.10.0</version>
    </dependency>
    <!-- https://stackoverflow.com/questions/20859379/cannot-import-javax-ejb-packages -->
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>7.0</version>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.jboss.logging/jboss-logging -->
    <dependency>
        <groupId>org.jboss.logging</groupId>
        <artifactId>jboss-logging</artifactId>
        <version>3.3.0.Final</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.ejb/ejb-api -->
    <dependency>
        <groupId>javax.ejb</groupId>
        <artifactId>ejb-api</artifactId>
        <version>3.0</version>
        <scope>provided</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.0.2.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>3.0-alpha-1</version>
        <scope>provided</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/junit/junit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.fasterxml.Hymanson.core/Hymanson-databind -->
    <dependency>
        <groupId>com.fasterxml.Hymanson.core</groupId>
        <artifactId>Hymanson-databind</artifactId>
        <version>2.9.3</version>
    </dependency>
    <!-- ********************************************************************************************************** -->
    <!-- THE FOLLOWING ARE SEPERATE FROM HIBERNATE THAT IS USED TO CONNECT 
        TO DB, THESE ARE FOR USE WITH VALIDATION -->
    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>6.0.7.Final</version>
        <exclusions>
            <exclusion>
                <artifactId>validation-api</artifactId>
                <groupId>javax.validation</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator-annotation-processor -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator-annotation-processor</artifactId>
        <version>6.0.7.Final</version>
                    <exclusions>
            <exclusion>
                <artifactId>validation-api</artifactId>
                <groupId>javax.validation</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator-cdi</artifactId>
        <version>6.0.7.Final</version>
                    <exclusions>
            <exclusion>
                <artifactId>validation-api</artifactId>
                <groupId>javax.validation</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>2.0.0.Final</version>
    </dependency>       
    <!-- ********************************************************************************************************** -->
    <!-- API and reference implementation of expression language {a.b} in html 
        code -->
    <!-- https://mvnrepository.com/artifact/javax.el/javax.el-api -->
    <dependency>
        <groupId>javax.el</groupId>
        <artifactId>javax.el-api</artifactId>
        <version>3.0.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.glassfish.web/javax.el -->
    <dependency>
        <groupId>org.glassfish.web</groupId>
        <artifactId>javax.el</artifactId>
        <version>2.2.6</version>
    </dependency>
</dependencies>

回答by Amit Nijhawan

Try adding this dependency to your pom.xml

尝试将此依赖项添加到您的 pom.xml

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>2.0.1.Final</version>
</dependency>

回答by Lasneyx

The situation is provoked by having javaee-web-api 7.0as dependency:

这种情况是由javaee-web-api 7.0依赖引起的:

<dependency>
  <groupId>javax</groupId>
  <artifactId>javaee-web-api</artifactId>
  <version>7.0</version>
  <scope>provided</scope>
</dependency>

This dependency provided by your servlet or application container at runtime is giving you an older api for javax.validation.

您的 servlet 或应用程序容器在运行时提供的这种依赖关系为您提供了一个较旧的javax.validation.

You have two things to try:

你有两件事要尝试:

Change the version of javaee-web-apito 8.0

将 的版本更改javaee-web-api为 8.0

This is the fastest solution if you are deploying your web app to a servlet or application container that supports Java EE 8.0 (for example: it seems to work in Tomcat 8.5.X but reading the documentation, only Tomcat 9 provides support for Java EE 8).

如果您将 Web 应用程序部署到支持 Java EE 8.0 的 servlet 或应用程序容器,这是最快的解决方案(例如:它似乎在 Tomcat 8.5.X 中工作但阅读文档,只有 Tomcat 9 提供对 Java EE 8 的支持)。

Keep javaee-web-apias 7.0 and downgrade hibernate-validator, validation-apidependencies:

保持javaee-web-api7.0 并降级hibernate-validatorvalidation-api依赖项:

<dependency>
  <groupId>javax.validation</groupId>
  <artifactId>validation-api</artifactId>
  <version>1.1.0.Final</version>
</dependency>

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-validator</artifactId>
  <version>5.4.2.Final</version>
</dependency>

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-validator-annotation-processor</artifactId>
  <version>5.4.2.Final</version>
</dependency>    

You have to change also some imports such as @Emailor @NotEmptyfrom javax.validationpackage to org.hibernate.validator.constraints(those annotations are Deprecated in 6.0.X hibernate-validator versions because they're included inside javax.validationapi).

您还必须更改一些导入,例如@Email@NotEmptyjavax.validation包更改为org.hibernate.validator.constraints(这些注释在 6.0.X hibernate-validator 版本中已弃用,因为它们包含在javax.validationapi 中)。

回答by Alireza Alallah

Do mvn dependency:tree, May you have another dependencies that use hibernate-annotations, and conflict with your spring version, also you can find them and exclude from class path, as below:

mvn dependency:tree,可能您有另一个使用的依赖项hibernate-annotations,并且与您的 spring 版本冲突,您也可以找到它们并从类路径中排除,如下所示:

 <exclusions>
    <exclusion>
       <groupId>org.hibernate</groupId>
        <artifactId>hibernate-annotations</artifactId>
    </exclusion>
  </exclusions>

I hopefully help you.

我希望能帮到你。

回答by jekho

Had the same problem with Springboot 2.1.4.RELEASE and some GeoTools dependencies.

Springboot 2.1.4.RELEASE 和一些 GeoTools 依赖项也有同样的问题。

Validation-api was specified from javax.validation and javaee-api jars.

Validation-api 是从 javax.validation 和 javaee-api jars 指定的。

Put them as dependencies like following:

将它们作为依赖项,如下所示:

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
</dependency>

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>7.0</version>
    <scope>provided</scope>
</dependency>

回答by kastmaster

Had the same issue as jekho, but solved it with:

有与 jekho 相同的问题,但解决了它:

<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-referencing</artifactId>
    <version>22.3</version>
    <exclusions>
        <exclusion>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>

I found which dependency was using javaee-api by using maven's dependency:tree on my project which points out which packages are being used by which dependency.

我通过在我的项目中使用 maven 的 dependency:tree 发现哪个依赖项正在使用 javaee-api,它指出哪个依赖项正在使用哪些包。