【问题标题】:Gradle | Spring boot dependencies are not excluding摇篮 | Spring boot 依赖不排除
【发布时间】:2015-04-28 08:51:48
【问题描述】:

我正在尝试让 log4j 在我正在处理的项目中工作。我在 build.gradle 中添加了相关的 log4j 依赖项,并排除了 Spring boot starter 日志记录,以便它可以工作。

当我使用 Maven 作为构建工具时,这工作得很好,但是一旦我切换到 Gradle,它就完全不工作了(除了来自 Spring 引导启动器的日志记录)。这是我的 build.gradle 中的依赖项

dependencies {
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-web'){
        exclude module: 'org.springframework.boot:spring-boot-starter-logging'
    }
    compile('org.springframework.boot:spring-boot-starter-log4j')
    compile('org.springframework.boot:spring-boot-starter-data-rest')
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.postgresql:postgresql:9.3-1101-jdbc41')
    compile('org.scala-lang:scala-library:2.10.4')
    testCompile('org.springframework.boot:spring-boot-starter-test') {
        exclude module: 'commons-logging'
    }
    providedCompile('org.springframework.boot:spring-boot-starter-tomcat')
}

但正如您在依赖关系树中清楚地看到的那样,spring-boot-starter-logging 仍然存在。我猜这就是日志记录不起作用的问题。

这是依赖树:

+--- org.springframework.boot:spring-boot-starter-data-jpa: -> 1.2.1.RELEASE
|    +--- org.springframework.boot:spring-boot-starter:1.2.1.RELEASE
|    |    +--- org.springframework.boot:spring-boot:1.2.1.RELEASE
|    |    |    +--- org.springframework:spring-core:4.1.4.RELEASE
|    |    |    \--- org.springframework:spring-context:4.1.4.RELEASE
|    |    |         +--- org.springframework:spring-aop:4.1.4.RELEASE
|    |    |         |    +--- aopalliance:aopalliance:1.0
|    |    |         |    +--- org.springframework:spring-beans:4.1.4.RELEASE
|    |    |         |    |    \--- org.springframework:spring-core:4.1.4.RELEASE
|    |    |         |    \--- org.springframework:spring-core:4.1.4.RELEASE
|    |    |         +--- org.springframework:spring-beans:4.1.4.RELEASE (*)
|    |    |         +--- org.springframework:spring-core:4.1.4.RELEASE
|    |    |         \--- org.springframework:spring-expression:4.1.4.RELEASE
|    |    |              \--- org.springframework:spring-core:4.1.4.RELEASE
|    |    +--- org.springframework.boot:spring-boot-autoconfigure:1.2.1.RELEASE
|    |    |    \--- org.springframework.boot:spring-boot:1.2.1.RELEASE (*)
|    |    +--- org.springframework.boot:spring-boot-starter-logging:1.2.1.RELEASE

这是我的 log4j.properties 文件

log4j.rootLogger=INFO, fileout, CONSOLE
PID=????
LOG_PATTERN=[%d{yyyy-MM-dd HH:mm:ss.SSS}] log4j%X{context} - ${PID} %5p [%t] --- %c{1}: %m%n


# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=${LOG_PATTERN}

# Log4j configurations for with file appender
log4j.category.org.hibernate.validator.internal.util.Version=WARN
log4j.category.org.apache.coyote.http11.Http11NioProtocol=WARN
log4j.category.org.apache.tomcat.util.net.NioSelectorPool=WARN
log4j.category.org.apache.catalina.startup.DigesterFactory=ERROR

log4j.appender.fileout=org.apache.log4j.RollingFileAppender
log4j.appender.fileout.File=sampleLog.log
log4j.appender.fileout.MaxFileSize=1024KB
log4j.appender.fileout.MaxBackupIndex=1
log4j.appender.fileout.layout=org.apache.log4j.PatternLayout
log4j.appender.fileout.layout.conversionPattern=${LOG_PATTERN}

更新

我设法修复了 jar 文件依赖项的排除。但是日志仍然不起作用,log4j.properties 也在 WAR 分布中的 classes 下。

更新 02

问题在于我的 IDE (STS)

【问题讨论】:

    标签: spring gradle log4j spring-boot build.gradle


    【解决方案1】:

    我已使用以下代码删除了所有日志记录依赖项

    configurations {
        all*.exclude module : 'spring-boot-starter-logging'
    }
    

    【讨论】:

      【解决方案2】:

      您已经从 spring-boot-starter-web 中排除了 spring-boot-starter-logging 模块,但是从依赖关系树中可以清楚地看到,模块 spring-boot-starter-data-jpa 也有一个依赖项到 spring-boot-starter-logging,所以你还必须从那里排除它(以及所有其他 spring-boot-starter-* 依赖项,因为它们都依赖于 spring-boot-starter-logging)。

      【讨论】:

        【解决方案3】:

        所有spring-boot-starter-* 项目都依赖于spring-boot-starter 项目,而spring-boot-starter-logging 又依赖于spring-boot-starter-logging。我可以通过在配置部分添加以下行来删除此依赖项:

        configurations {
            compile.exclude module: 'spring-boot-starter-logging'
        }
        

        【讨论】:

        • 成功了,spring boot 的日志依赖已经移除,但是日志还是不行。
        • 生成的sampleLog.log文件是没有内容还是文件本身没有创建?
        • 如果您使用另一个名为 spring-boot-start-logging 的模块,这种更明确的语法也适用:configurations { compile.exclude group:"org.springframework.boot", module : 'spring-boot-starter-logging' }
        猜你喜欢
        • 2016-08-19
        • 2016-10-24
        • 2017-11-11
        • 2019-11-25
        • 2017-07-05
        • 2020-09-10
        • 2021-12-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多