Maven 依赖管理问题小计

刚学Maven,遇到点小问题,记录一下。
https://maven.apache.org/

问题的起因是项目中使用了 Hibernate Validator ,但是运行起来后总是不能按照设置的注解校验字段数据。
查看日志发现"""
Caused by: javax.validation.ValidationException: Unable to create a Configuration, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath.
"""

黑人问号脸??!

我在pom.xml里添加了依赖关系了啊。
查看target/wms/WEB-INF/lib目录下也确实没有hibernate-validator的jar包。

继续问号脸??!

编译项目啥的也没报错啊。
查看Maven面板下,Dependencies下的hibernate-validator包标了红色波浪线。

看来是真的缺jar包。
但,这是为什么呢?

看一下我的配置项
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.4.Final</version>
</dependency>

这些配置是什么意思呢?
https://maven.apache.org/guides/introduction/introduction-to-the-pom.html#Minimal_POM

groupId - the id of the project's group.
artifactId - the id of the artifact (project)
version - the version of the artifact under the specified group

A POM requires that its groupId, artifactId, and version be configured. These three values form the project's fully qualified artifact name.

简单来说,这3项是用来确定文件在remote仓库中的位置的,每一项都不能有错误。

Hibernate Validator Documentation
https://hibernate.org/validator/documentation/

我们来看一下文档里怎么说的

6.0里写的是
1.1. Project set up
In order to use Hibernate Validator within a Maven project, simply add the following dependency to your pom.xml:

Example 1.1: Hibernate Validator Maven dependency
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.15.Final</version>
</dependency>
This transitively pulls in the dependency to the Bean Validation API (javax.validation:validation-api:2.0.1.Final).

5.4里写的是
1.1. Project set up
In order to use Hibernate Validator within a Maven project, simply add the following dependency to your pom.xml:

Example 1. Hibernate Validator Maven dependency
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.4.3.Final</version>
</dependency>
This transitively pulls in the dependency to the Bean Validation API (javax.validation:validation-api:1.1.0.Final).

通过对比发现,除了version不同外,groupId也不一样。

实际上这是6.0的一个变化
* HV-1224 - build - Change group id to "org.hibernate.validator"

而我混用了groupId ,maven自然就找不到正确的文件。
所以就缺少jar包了。

但是,Maven为啥都不给报个错,就给正常打包了呢??!

猜你喜欢

转载自www.cnblogs.com/LikeVirgo/p/10412607.html