Maven中的许多依赖项也依赖于其他库的功能。当包含像hibernate-core
这样的依赖关系时,它还会自动将其依赖关系导入到您的项目中,其中一个是hibernate-annotations
库。结果是有两个不同版本的库,这是Maven中的一个常见问题,它们提供了一个很好的方式来找到这些依赖项从哪里导入。
在这种情况下,只需从您的项目中删除hibernate-annotations
依赖项,并让hibernate-core
为您导入。
如何为自己找到这个重复的库冲突?一个好办法是使用Maven提供给我们的依赖树插件:mvn dependency:tree -Dverbose -Dincludes=org.hibernate.*
。这将显示项目中org.hibernate的组或子组中的所有库依赖关系。 还可以考虑通过在呼叫中附加> fileOutputName.txt
来输出到文件,以便更容易查看,尤其是在使用Windows命令行时。
下面是从一个项目我有了冬眠输出样本:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.0.1.Final</version>
</dependency>
而且mvn dependency:tree
电话:
[INFO] +- org.hibernate:hibernate-core:jar:4.0.1.Final:compile
[INFO] | +- (org.hibernate.javax.persistence:hibernate-jpa-2.0-api:jar:1.0.1.Final:compile - omitted for duplicate)
[INFO] | \- org.hibernate.common:hibernate-commons-annotations:jar:4.0.1.Final:compile
[INFO] +- org.hibernate.common:hibernate-commons-annotations:jar:tests:4.0.1.Final:compile
[INFO] +- org.hibernate.javax.persistence:hibernate-jpa-2.0-api:jar:1.0.1.Final:compile
[INFO] \- org.hibernate:hibernate-entitymanager:jar:4.0.1.Final:compile
[INFO] +- (org.hibernate.javax.persistence:hibernate-jpa-2.0-api:jar:1.0.1.Final:compile - omitted for duplicate)
[INFO] \- (org.hibernate.common:hibernate-commons-annotations:jar:4.0.1.Final:compile - omitted for duplicate)
正如你可以看到hibernate-core
自动导入hibernate-commons-annotations
。如果该版本与您在pom中包含的版本不同,Maven将包含这两个问题。除非你有充分的理由去改变注释版本,否则最好的办法是让核心导入去做它的事情。
如果你真的想从核心库,你可以通过添加<exclusions>
标签这样做删除的依赖。但要小心,因为还有其他部分的hibernate套件会尝试导入注释包,所以最终可能会从很多依赖关系中排除注释。
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.0.1.Final</version>
<exclusions>
<exclusion> <!-- remove annotations inclusion -->
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
</exclusion>
</exclusions>
</dependency>
我觉得休眠核心默认包含注释的依赖。因此,在使用maven时包括注释会导致项目中有2个注释包。您可以从hibernate-core中排除annotations包,或者从项目中删除注释依赖项。 –