gradle排除依赖
我正在使用Spring Boot。 默认情况下,Spring Boot带有Logback。 我想使用log4j(出于任何原因..)
为了做到这一点,我不得不排除logback并添加新的log4j依赖项:
在此软件包中“隐藏”了logback:
compile("org.springframework.boot:spring-boot-starter:$project.ext.springBootVersion"){exclude module: 'org.springframework.boot:spring-boot-starter-logging'
}compile("org.springframework.boot:spring-boot-starter-log4j:$project.ext.springBatchVersion")
现在,当您尝试运行应用程序时,会出现以下异常:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/dev/caches/modules-2/files-2.1/org.slf4j/slf4j-log4j12/1.7.10/b3eeae7d1765f988a1f45ea81517191315c69c9e/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/dev/caches/modules-2/files-2.1/ch.qos.logback/logback-classic/1.1.2/b316e9737eea25e9ddd6d88eaeee76878045c6b2/logback-classic-1.1.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
现在,我们必须查看Gradle的依赖关系树,以查看隐藏了logback的位置以消除它。
查看gradle依赖关系树的简单命令:
gradle -q dependencies web:dependencies --configuration compile
* web代表您的模块名称。
输出的快照视图:
Project :web - web------------------------------------------------------------compile - Compile classpath for source set 'main'.+--- org.springframework.boot:spring-boot-starter-actuator:1.2.2.RELEASE| +--- org.springframework.boot:spring-boot-starter:1.2.2.RELEASE| | +--- org.springframework.boot:spring-boot:1.2.2.RELEASE| | | +--- org.springframework:spring-core:4.1.5.RELEASE| | | | \--- commons-logging:commons-logging:1.2| | | \--- org.springframework:spring-context:4.1.5.RELEASE| | | +--- org.springframework:spring-aop:4.1.5.RELEASE| | | | +--- aopalliance:aopalliance:1.0| | | | +--- org.springframework:spring-beans:4.1.5.RELEASE| | | | | \--- org.springframework:spring-core:4.1.5.RELEASE (*)| | | | \--- org.springframework:spring-core:4.1.5.RELEASE (*)| | | +--- org.springframework:spring-beans:4.1.5.RELEASE (*)| | | +--- org.springframework:spring-core:4.1.5.RELEASE (*)| | | \--- org.springframework:spring-expression:4.1.5.RELEASE| | | \--- org.springframework:spring-core:4.1.5.RELEASE (*)| | +--- org.springframework.boot:spring-boot-autoconfigure:1.2.2.RELEASE| | | +--- org.springframework.boot:spring-boot:1.2.2.RELEASE (*)| | | \--- org.yaml:snakeyaml:1.14| | +--- org.springframework.boot:spring-boot-starter-logging:1.2.2.RELEASE| | | +--- org.slf4j:jcl-over-slf4j:1.7.10| | | | \--- org.slf4j:slf4j-api:1.7.10| | | +--- org.slf4j:jul-to-slf4j:1.7.10| | | | \--- org.slf4j:slf4j-api:1.7.10| | | +--- org.slf4j:log4j-over-slf4j:1.7.10| | | | \--- org.slf4j:slf4j-api:1.7.10| | | \--- mycompany:logback-classic:1.1.2| | | +--- mycompany:logback-core:1.1.2| | | \--- org.slf4j:slf4j-api:1.7.6 -> 1.7.10| | +--- org.springframework:spring-core:4.1.5.RELEASE (*)| | \--- org.yaml:snakeyaml:1.14| +--- org.springframework.boot:spring-boot-actuator:1.2.2.RELEASE
我们可以从我们的一个依赖项中找到一个弹出的logback实例:
mycompany:logback-core:1.1.2
(我发现logback show在其他依赖项中)。
现在我们有两个选择:
- 在Indecency树中排除每个Logback的路由
- 使用配置范围内的排除(更简便的方法)
因此,转到您的build.gradle并添加以下内容:
configurations {compile.exclude group:'ch.qos.logback'
}
而已。 你的噩梦结束了。 如果再次检查依赖关系树,您将不再看到任何logback。
翻译自: https://www.javacodegeeks.com/2015/03/how-to-exclude-libraries-from-all-dependencies-in-gradle.html
gradle排除依赖