MAVEN如何加载“相同”依赖JAR文件?

时间:2021-04-28 17:15:00

I have following lines in a Maven POM file

我在Maven POM文件中有以下行

<dependency>
    <groupId>com.querydsl</groupId>
    <artifactId>querydsl-jpa</artifactId>
    <version>4.2.1</version>
</dependency>

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.4.0</version>
</dependency>

On MAVEN repository, on https://mvnrepository.com/artifact/com.querydsl/querydsl-jpa/4.2.1 it is indicated that querydsl-api depends of slf4j-api version 1.6.1.

在MAVEN存储库上,在https://mvnrepository.com/artifact/com.querydsl/querydsl-jpa/4.2.1上,表明querydsl-api取决于slf4j-api版本1.6.1。

When MAVEN build the application, which version of slf4j-api is used ?

当MAVEN构建应用程序时,使用哪个版本的slf4j-api?

When Java application is loaded, which version of slf4j-api is used ?

加载Java应用程序时,使用哪个版本的slf4j-api?

What happens if slf4j-api is defined before querydsl-jpa ?

如果在querydsl-jpa之前定义slf4j-api会发生什么?

What happens if slf4j-api version used in querydsl-jpa is lower than version defined in application POM file ?

如果querydsl-jpa中使用的slf4j-api版本低于应用程序POM文件中定义的版本,会发生什么?

1 个解决方案

#1


1  

This is all about Maven dependency mediation.

这是关于Maven依赖调解的全部内容。

Maven builds a tree from your dependencies and consolidates this tree into a list. In this list, each dependency may appear only once. So if there are several different versions in your tree, Maven needs to choose one.

Maven从您的依赖项构建一个树,并将此树合并到一个列表中。在此列表中,每个依赖项可能只出现一次。因此,如果树中有多个不同的版本,Maven需要选择一个版本。

Maven has a "nearest dependency" standard rule that roughly states: The direct dependencies count more than first level transitive dependencies which count more than second level transitive dependencies and so on.

Maven有一个“最接近的依赖”标准规则,大致说明:直接依赖关系比第一级传递依赖关系计数更多,它们计算的不仅仅是二级传递依赖关系等等。

It is not possible to define a different rule, but you can specify a version in the <dependencyManagement> section of your pom. This version overwrites all transitively defined versions.

无法定义不同的规则,但您可以在pom的 部分中指定版本。此版本将覆盖所有可传递定义的版本。

The hardest part is to figure out which version may be "right", i.e. compatible with all use cases. There is not automatic way to do that.

最难的部分是确定哪个版本可能“正确”,即与所有用例兼容。没有自动的方法来做到这一点。

#1


1  

This is all about Maven dependency mediation.

这是关于Maven依赖调解的全部内容。

Maven builds a tree from your dependencies and consolidates this tree into a list. In this list, each dependency may appear only once. So if there are several different versions in your tree, Maven needs to choose one.

Maven从您的依赖项构建一个树,并将此树合并到一个列表中。在此列表中,每个依赖项可能只出现一次。因此,如果树中有多个不同的版本,Maven需要选择一个版本。

Maven has a "nearest dependency" standard rule that roughly states: The direct dependencies count more than first level transitive dependencies which count more than second level transitive dependencies and so on.

Maven有一个“最接近的依赖”标准规则,大致说明:直接依赖关系比第一级传递依赖关系计数更多,它们计算的不仅仅是二级传递依赖关系等等。

It is not possible to define a different rule, but you can specify a version in the <dependencyManagement> section of your pom. This version overwrites all transitively defined versions.

无法定义不同的规则,但您可以在pom的 部分中指定版本。此版本将覆盖所有可传递定义的版本。

The hardest part is to figure out which version may be "right", i.e. compatible with all use cases. There is not automatic way to do that.

最难的部分是确定哪个版本可能“正确”,即与所有用例兼容。没有自动的方法来做到这一点。