【问题标题】:Jar exists in local Maven repo, but still compile error with "Package does not exist"Jar 存在于本地 Maven 存储库中,但仍然编译错误并显示“包不存在”
【发布时间】:2018-03-02 11:24:59
【问题描述】:

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 肯定存在于本地文件系统 repo 中:

$ 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 肯定包含在依赖项中?你能显示pom.xmlmvn dependency:tree吗?
  • @lexicore 好电话。将其添加到问题的顶部。

标签: java maven compiler-errors dependencies


【解决方案1】:

似乎hibernate-validator 的范围有误。根据mvn依赖:treethe scope isruntime`和that means

运行时
此范围表示依赖项不是编译所必需的,而是执行所必需的。它在运行时和测试类路径中,但不在编译类路径中。

尝试改变编译范围。

【讨论】:

  • 是的,刚刚看到!嗬!谢谢!
猜你喜欢
  • 1970-01-01
  • 2022-01-21
  • 2017-08-30
  • 2019-01-21
  • 2013-12-19
  • 2013-05-31
  • 1970-01-01
  • 2015-10-23
  • 1970-01-01
相关资源
最近更新 更多