Jar存在于本地Maven仓库中,但仍然编译错误“包不存在”

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jar存在于本地Maven仓库中,但仍然编译错误“包不存在”相关的知识,希望对你有一定的参考价值。

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有一个错误的范围。根据mvn依赖:treethe scope isruntime`和that means

运行 此范围表示编译不需要依赖项,但是用于执行。它位于运行时和测试类路径中,但不是编译类路径。

尝试更改范围以进行编译。

以上是关于Jar存在于本地Maven仓库中,但仍然编译错误“包不存在”的主要内容,如果未能解决你的问题,请参考以下文章

maven本地仓库下载jar破损导致不能使用原因?

MAVEN常见错误整理及解决方法

maven 添加自己下载的jar包到本地仓库

解决Maven依赖本地仓库eclipse报错的问题

maven修改本地仓库地址配置文件

maven package install deploy区别