gpt4 book ai didi

java - Jar 存在于本地 Maven 存储库中,但仍然编译错误,错误为 "Package does not exist"

转载 作者:行者123 更新时间:2023-12-02 11:34:11 25 4
gpt4 key购买 nike

hibernate-validator 肯定作为依赖项存在于 pom.xml 中:

<dependencies>
......
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
</dependencies>


$ mvn dependency:tree | grep hibernate
[INFO] +- org.hibernate:hibernate-validator:jar:5.2.4.Final:runtime

并且该 jar 肯定存在于本地文件系统存储库中:

$ find /home/stewart/.m2/repository/ -name "hibernate-validator*"
/home/stewart/.m2/repository/org/hibernate/hibernate-validator-parent
/home/stewart/.m2/repository/org/hibernate/hibernate-validator-parent/5.2.4.Final/hibernate-validator-parent-5.2.4.Final.pom.sha1
/home/stewart/.m2/repository/org/hibernate/hibernate-validator-parent/5.2.4.Final/hibernate-validator-parent-5.2.4.Final.pom
/home/stewart/.m2/repository/org/hibernate/hibernate-validator
/home/stewart/.m2/repository/org/hibernate/hibernate-validator/5.2.4.Final/hibernate-validator-5.2.4.Final.pom.sha1
/home/stewart/.m2/repository/org/hibernate/hibernate-validator/5.2.4.Final/hibernate-validator-5.2.4.Final.pom
/home/stewart/.m2/repository/org/hibernate/hibernate-validator/5.2.4.Final/hibernate-validator-5.2.4.Final.jar
/home/stewart/.m2/repository/org/hibernate/hibernate-validator/5.2.4.Final/hibernate-validator-5.2.4.Final.jar.sha1

但是编译仍然失败:

$ mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Project 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ project ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/stewart/workspace/project/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.3:compile (default-compile) @ project ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 15 source files to /home/stewart/workspace/project/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /home/stewart/workspace/project/src/main/java/project/entity/Entity.java:[3,43] package org.hibernate.validator.constraints does not exist
[ERROR] /home/stewart/workspace/project/src/main/java/project/entity/Entity.java:[15,10] cannot find symbol
symbol: class Email
location: class project.entity.Entity
[INFO] 2 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

我都尝试过:

 mvn -U clean compile

还有

 mvn dependency:purge-local-repository clean compile

.jar 文件是新下载的:

$ ll /home/stewart/.m2/repository/org/hibernate/hibernate-validator/5.2.4.Final/hibernate-validator-5.2.4.Final.jar 
-rw-rw-r-- 1 stewart stewart 704465 Mar 2 11:10 /home/stewart/.m2/repository/org/hibernate/hibernate-validator/5.2.4.Final/hibernate-validator-5.2.4.Final.jar

并且 .jar 确实包含电子邮件类:

$ unzip -l /home/stewart/.m2/repository/org/hibernate/hibernate-validator/5.2.4.Final/hibernate-validator-5.2.4.Final.jar | grep Email.class
1377 2016-02-17 16:15 org/hibernate/validator/constraints/Email.class

为什么Maven找不到类?

最佳答案

似乎 hibernate-validator 的范围错误。根据 mvn 依赖项:tree范围是runtime` 和 that means :

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.

尝试更改编译范围。

关于java - Jar 存在于本地 Maven 存储库中,但仍然编译错误,错误为 "Package does not exist",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49068154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com