Gradle | 不会排除Spring启动依赖项

Swi*_*tch 9 spring log4j gradle build.gradle spring-boot

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

当我使用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')
}
Run Code Online (Sandbox Code Playgroud)

但是你可以在依赖树中清楚地看到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
Run Code Online (Sandbox Code Playgroud)

这是我的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}
Run Code Online (Sandbox Code Playgroud)

UPDATE

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

更新02

它的问题在于我的IDE(STS)

Mit*_*hun 43

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

configurations {
    compile.exclude module: 'spring-boot-starter-logging'
}
Run Code Online (Sandbox Code Playgroud)

  • 如果您使用另一个名为 spring-boot-start-logging 的模块,这种更明确的语法也适用:configuration { compile.exclude group:"org.springframework.boot", module: 'spring-boot-starter-记录'} (2认同)