我有一个使用最新版本的 Hibernate 的项目(假设是 v2.0)。我在整个项目中都使用它。但我的项目还使用了一些依赖项(比如说 MySQL Connector),它使用 Hibernate(比如说 v1.0)。所以在我的 pom.xml 中我会有类似的内容:
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>Hibernate</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.mysql</groupId>
<artifactId>MySQLConnector</artifactId>
<version>3.7</version>
</dependency>
</dependencies>
最后,当我编译项目时,下载并使用的Hibernate版本是v1.0,因为MySQLConnector需要这个版本。有没有办法指定依赖项的某个版本,该版本仅由我的依赖项之一使用,而其余代码则使用另一个版本?所以类似:
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>Hibernate</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.mysql</groupId>
<artifactId>MySQLConnector</artifactId>
<version>3.7</version>
<somemagicaltag>
<groupId>org.hibernate</groupId>
<artifactId>Hibernate</artifactId>
<version>1.0</version>
</somemagicaltag>
</dependency>
</dependencies>
因此允许 MySQLConnector 使用旧版本的 Hibernate(如果它喜欢的话),但我的其余代码使用较新、更新版本的 Hibernate?
最佳答案
Is there a way to specify some version of a dependency that will be used only by one of my dependencies and the rest of the code to use another version?
没有。只可以有一个人。因此,在您的情况下,要么 1.0 要么 2.0 (通常使用较新的版本更有意义)。使用哪个版本取决于 pom.xml 中使用此类传递依赖项的依赖项顺序: Why order of Maven dependencies matter?
您还可以通过指定此类依赖项(这会覆盖传递依赖项版本)或通过在 dependencyManagement 标记中定义此类依赖项来定义将使用哪个版本:Differences between dependencyManagement and dependencies in Maven或者使用BOM机制:Maven BOM [Bill Of Materials] Dependency
关于java - 如何在 Maven 中为我的依赖项之一指定特定依赖项版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59068100/