Maven 2 - 如何从传递依赖项版本定义依赖项版本

5

我将用我的实际情况来解释这个问题。

我使用logback 1.0.1进行日志记录,它包含SLF4J 1.6.4作为依赖项。我还使用了SLF4J API桥接器来支持旧的日志API(如java.util.logging、log4j和commons-logging),这些API并不是显式的依赖项。这些桥接器也必须(最好)是版本1.6.4。

为了使我的pom.xml尽可能整洁和无错误,我想强制要求这些API桥接器与SLF4J的版本相同。我所知道的唯一方法是在我的pom.xml中手动定义它们,并使用版本1.6.4。如果我更新logback并且所需的SLF4J版本提高了,我需要记得更改桥接器API到正确的版本。

我能否以某种方式将旧的API版本与传递依赖项SLF4J的版本挂钩?

当前的pom.xml:

    <properties>
    <org.slf4j.version>1.6.4</org.slf4j.version>
</properties>

<dependencies>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.0.1</version>
        <!-- requires SLF4J 1.6.4 -->
    </dependency>
    <!-- ... -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>log4j-over-slf4j</artifactId>
        <version>${org.slf4j.version}</version>
        <!-- here, how to bind this version value to SLF4J's version? -->
        <scope>runtime</scope>
    </dependency>
    <!-- the other two bridge API's go here -->
</dependencies>

https://stackoverflow.com/questions/23891067/maven-access-version-of-transitive-dependency-via-property - Rinke
3个回答

1

依赖收敛可能会对您有所帮助。感谢@wemu!

我有一个相同的问题

您可以克隆https://gist.github.com/f35db1ac6dc8b6f45393.git

<dependencies>
  <dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.1.3</version>
    <scope>runtime</scope> <!-- depends on slf4j-api:1.7.7 -->
  </dependency>
  <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>log4j-over-slf4j</artifactId>
    <version>${unified.slf4j-api.version}</version>
    <scope>runtime</scope>
  </dependency>
  <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>${unified.slf4j-api.version}</version>
  </dependency>
</dependencies>
<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <unified.slf4j-api.version>1.7.7</unified.slf4j-api.version>
</properties>
<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-enforcer-plugin</artifactId>
       <version>1.4.1</version>
       <executions>
         <execution>
           <id>enforce</id>
           <configuration>
             <rules>
               <dependencyConvergence/>
             </rules>
           </configuration>
           <goals>
             <goal>enforce</goal>
           </goals>
         </execution>
       </executions>
     </plugin>
  </plugins>
</build>

此时,所有事情都正常工作。
$ mvn -Dverbose=true dependency:tree verify
...
[INFO] test:enforcer-dependency-convergence-test:jar:0.1-SNAPSHOT
[INFO] +- ch.qos.logback:logback-classic:jar:1.1.3:runtime
[INFO] |  +- ch.qos.logback:logback-core:jar:1.1.3:runtime
[INFO] |  \- (org.slf4j:slf4j-api:jar:1.7.7:runtime - omitted for duplicate)
[INFO] +- org.slf4j:log4j-over-slf4j:jar:1.7.7:runtime
[INFO] |  \- (org.slf4j:slf4j-api:jar:1.7.7:runtime - omitted for duplicate)
[INFO] \- org.slf4j:slf4j-api:jar:1.7.7:compile
[INFO]
[INFO] --- maven-enforcer-plugin:1.4.1:enforce (default) @ enforcer-dependency-convergence-test ---
[INFO]
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
...

$

现在让我们更改slf4j-api的版本。
$ mvn -Dunified.slf4j-api.version=1.7.13 -Dverbose=true dependency:tree verify
...
[INFO] test:enforcer-dependency-convergence-test:jar:0.1-SNAPSHOT
[INFO] test:enforcer-dependency-convergence-test:jar:0.1-SNAPSHOT
[INFO] +- ch.qos.logback:logback-classic:jar:1.1.3:runtime
[INFO] |  +- ch.qos.logback:logback-core:jar:1.1.3:runtime
[INFO] |  \- (org.slf4j:slf4j-api:jar:1.7.7:runtime - omitted for conflict with 1.7.13)
[INFO] +- org.slf4j:log4j-over-slf4j:jar:1.7.13:runtime
[INFO] |  \- (org.slf4j:slf4j-api:jar:1.7.13:runtime - omitted for conflict with 1.7.7)
[INFO] \- org.slf4j:slf4j-api:jar:1.7.13:compile
[INFO]
[INFO] --- maven-enforcer-plugin:1.4.1:enforce (default) @ enforcer-dependency-convergence-test ---
[WARNING]
Dependency convergence error for org.slf4j:slf4j-api:1.7.7 paths to dependency are:
+-test:enforcer-dependency-convergence-test:0.1-SNAPSHOT
  +-ch.qos.logback:logback-classic:1.1.3
    +-org.slf4j:slf4j-api:1.7.7
and
+-test:enforcer-dependency-convergence-test:0.1-SNAPSHOT
  +-org.slf4j:log4j-over-slf4j:1.7.13
    +-org.slf4j:slf4j-api:1.7.13
and
+-test:enforcer-dependency-convergence-test:0.1-SNAPSHOT
  +-org.slf4j:slf4j-api:1.7.13

[WARNING] Rule 0: org.apache.maven.plugins.enforcer.DependencyConvergence failed with message:
Failed while enforcing releasability the error(s) are [
Dependency convergence error for org.slf4j:slf4j-api:1.7.7 paths to dependency are:
+-test:enforcer-dependency-convergence-test:0.1-SNAPSHOT
  +-ch.qos.logback:logback-classic:1.1.3
    +-org.slf4j:slf4j-api:1.7.7
and
+-test:enforcer-dependency-convergence-test:0.1-SNAPSHOT
  +-org.slf4j:log4j-over-slf4j:1.7.13
    +-org.slf4j:slf4j-api:1.7.13
and
+-test:enforcer-dependency-convergence-test:0.1-SNAPSHOT
  +-org.slf4j:slf4j-api:1.7.13
]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
...

$

1

我看了一下enforcer,但它并不是我需要的。如果我只是在我的pom中定义SLF4J,我会得到相同的结果。这样做是可行的,但是不能与logback升级“自动”配合,我还需要更改SLF4J版本。这种方法是有效的,但不是一种优美的方式...;-) - user1183250
没错!问题在于这并不能防止人们通过直接添加它们来更改传递依赖项的版本。如果你禁止这些,那么你可以确保没有人能够这样做。但这可能不是一个问题 :) - wemu

0

在你的顶层pom中,不要直接依赖于slf4j。


我需要桥接API,可以直接依赖于SLF4J,但版本不匹配的可能性仍然存在,因为我需要手动定义两个版本。如果目前还没有人编写这样的代码来绑定两个版本/库,则无法同时绑定两个版本/库,因此我必须应对这种情况。 - user1183250

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接