Gradle | Spring boot dependencies are not excludin

2019-02-16 20:58发布

问题:

I'm trying to get log4j to work in a project that I'm working on. I added the relevant log4j dependencies in build.gradle and excluded the Spring boot starter logging so it can work.

This worked fine when I used Maven as the build tool, but once I switched to Gradle it's not working at all (excepts the logging from Spring boot starter). Here are the dependencies in my 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')
}

But as you can clearly see in the dependency tree the spring-boot-starter-logging is still there. I'm guessing that this is the issue why the logging isn't working.

Here's the dependency tree:

+--- 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

Here's my log4j.properties file

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}

UPDATE

I managed to fix the exclusion of jar file dependency. But the logging is still not working, log4j.properties is also in the WAR distribution under classes.

UPDATE 02

It worked issue is with the my IDE (STS)

回答1:

All the spring-boot-starter-* projects are dependent on spring-boot-starter project, which in turn in dependent on spring-boot-starter-logging. I was able to remove this dependency by adding the following line in configurations section:

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


回答2:

You have excluded the spring-boot-starter-logging module from spring-boot-starter-web, but as you clearly can see from the dependency tree, the module spring-boot-starter-data-jpa also has a dependency to spring-boot-starter-logging, so you also have to exclude it from there (and from all other spring-boot-starter-* dependencies, since all of them depend on spring-boot-starter-logging).