为什么我们需要在 pom 中排除

Why do we need exclude in pom

Pom.xml 有这些元素,我很好奇为什么我们需要从 spring-core 中排除 commons-logging?它是如何工作的?

<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>3.0.5.RELEASE</version>
        <type>jar</type>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <artifactId>commons-logging</artifactId>
                <groupId>commons-logging</groupId>
            </exclusion>
        </exclusions>
    </dependency>
<!-- next is log4j dependencie -->

我以前见过也做过类似的事情,原因很简单:

要排除 commons-logging 库,因为我想改用 log4j

所以你告诉 maven 不要包含不必要的库,并使用 < exclusion >.

您可能在程序中使用的 commons-logging 版本与 spring-core 3.0.5.RELEASE 定义的版本不同。检查你的程序的依赖关系,看看是否有另一个 commons-logging 定义。

在上面的示例中,spring-core 本身依赖于 commons-logging,因此它将把那个 jar 拉到项目中。不管出于什么原因,用户不想要 spring-core 附带的 commons-logging,他们可能想使用另一个版本。

可能有 2 个依赖项的子依赖项相互冲突,尽管我认为这种情况很少见。通常排除可以谨慎使用。