Spring BOM,commons-logging,Log4j2和Maven依赖管理

时间:2015-08-23 11:10:44

标签: spring maven log4j2 apache-commons-logging

我为几个模块构建父POM。他们使用Spring和Log4j2,所以我想在父POM的部分中定义相关配置,并使用新的Spring BOM功能来避免混淆Spring组件的版本。

根据documentation,这是正确使用Spring BOM:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-framework-bom</artifactId>
            <version>4.2.0.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
    .......
</dependencyManagement>

现在,我需要从commons-logging中排除对spring-core的依赖。根据{{​​3}}:

,这是正确的方法
<dependencyManagement>
    .......
    <!-- Remove JCL from Spring deps -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.2.0.RELEASE</version>
        <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!-- Add Log4j2 with JCL bridge -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>${log4j2.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>${log4j2.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-jcl</artifactId>
        <version>${log4j2.version}</version>
    </dependency>
    .......
</dependencyManagement>

spring-core依赖关系的定义中删除版本号会导致编译阶段(spring-core)缺少'dependencies.dependency.version' for org.springframework:spring-core:jar is missing版本错误。实际上,它需要我重新指定Spring版本号 - 这是Spring BOM应该解决的问题。

您是否知道根据BOM版本进行此项工作的任何方法?

2 个答案:

答案 0 :(得分:0)

为什么不直接将排除列入bom声明:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-framework-bom</artifactId>
            <version>${spring.version}</version>
            <type>pom</type>
            <scope>import</scope>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
</dependencyManagement>

如果您想直接控制其版本,也可以将commons-logging添加到dependencyManagement。这就是我在最近的一个项目中所做的:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-framework-bom</artifactId>
            <version>${spring.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>${commons-logging.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

答案 1 :(得分:0)

log4j jcl桥不会替换commons-logging jar。保留它。它只是在commons logging和log4j2之间创建一个绑定。