java – Maven 2 – 从传递依赖版本定义依赖版本

我会用我的实际情况来解释这个问题.

我使用logback 1.0.1进行日志记录,它包含SLF4J 1.6.4作为依赖项.我还将SLF4J API桥用于遗留日志API(java.util.logging,log4j和commons-logging),它们不是显式依赖项.这些也必须(最好)是版本1.6.4.

为了使我的pom.xml尽可能整洁无误,我想强制要求这些API桥与SLF4J版本相同.我知道的唯一方法是使用版本1.6.4在我的pom.xml中手动将它们定义为依赖项.如果我更新了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>

最佳答案 不是很漂亮的方式:/

有maven enforcer插件:http://maven.apache.org/enforcer/enforcer-rules/

所以你可以禁止传递依赖,并包括你想要的版本:http://maven.apache.org/enforcer/enforcer-rules/bannedDependencies.html

如果你使用一个属性的好版本,你不需要在enforcer插件版本中乱七八糟.

点赞