aop gradle jar包_如何正确配置gradle构建以避免在生成的jar中包含log4j和slf4j?

I'm having an issue with my spring-boot application: I'm able to run it within Eclipse, but unable to run the jar file (built with gradle).

I run the following command to build my project:

gradle buid

The build is successful:

gradle build

:compileJava UP-TO-DATE

:processResources UP-TO-DATE

:classes UP-TO-DATE

:findMainClass

:jar

:bootRepackage

:assemble

:compileTestJava UP-TO-DATE

:processTestResources UP-TO-DATE

:testClasses UP-TO-DATE

:test UP-TO-DATE

:check UP-TO-DATE

:build

BUILD SUCCESSFUL

Total time: 1.214 secs

This is a fragment of the resulting jar file:

jar -tvf build/libs/springboot-receiver-api-0.1.0.jar | grep log

9988 Mon Apr 04 20:37:48 CDT 2016 BOOT-INF/lib/slf4j-log4j12-1.7.21.jar

2308 Wed Sep 21 07:11:50 CDT 2016 BOOT-INF/lib/spring-boot-starter-logging-1.4.1.RELEASE.jar

66802 Thu May 28 09:49:34 CDT 2015 BOOT-INF/lib/jboss-logging-3.3.0.Final.jar

304075 Tue Mar 29 22:24:50 CDT 2016 BOOT-INF/lib/logback-classic-1.1.7.jar

23646 Mon Apr 04 20:39:02 CDT 2016 BOOT-INF/lib/log4j-over-slf4j-1.7.21.jar

470782 Tue Mar 29 22:23:42 CDT 2016 BOOT-INF/lib/logback-core-1.1.7.jar

489884 Sun May 06 13:24:48 CDT 2012 BOOT-INF/lib/log4j-1.2.17.jar

when I attempt to run this jar file, I get this error:

java -jar build/libs/springboot-receiver-api-0.1.0.jar

SLF4J: Class path contains multiple SLF4J bindings.

SLF4J: Found binding in [jar:file:/Users/eugene/.Trash/springboot-receiverapi/build/libs/springboot-receiver-api-0.1.0.jar!/BOOT-INF/lib/slf4j-log4j12-1.7.21.jar!/org/slf4j/impl/StaticLoggerBinder.class]

SLF4J: Found binding in [jar:file:/Users/eugene/.Trash/springboot-receiverapi/build/libs/springboot-receiver-api-0.1.0.jar!/BOOT-INF/lib/logback-classic-1.1.7.jar!/org/slf4j/impl/StaticLoggerBinder.class]

SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.

SLF4J: Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting StackOverflowError.

Exception in thread "main" java.lang.reflect.InvocationTargetException

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)

at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)

at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)

at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:58)

Caused by: java.lang.ExceptionInInitializerError

at org.slf4j.impl.StaticLoggerBinder.(StaticLoggerBinder.java:72)

Caused by: java.lang.IllegalStateException: Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting StackOverflowError. See also http://www.slf4j.org/codes.html#log4jDelegationLoop for more details.

at org.slf4j.impl.Log4jLoggerFactory.(Log4jLoggerFactory.java:54)

... 19 more

Here is my build.gradle file:

apply plugin: 'java'

apply plugin: 'maven'

group = 'uptake'

version = '0.0.1-snapshot'

description = """Spring-Boot-ReceiverAPI"""

sourceCompatibility = 1.8

targetCompatibility = 1.8

tasks.withType(JavaCompile) {

options.encoding = 'UTF-8'

}

repositories {

maven { url "http://repo.maven.apache.org/maven2" }

}

dependencies {

compile(group: 'org.springframework.boot', name: 'spring-boot-starter-web', version:'1.2.3.RELEASE') {

exclude(module: 'log4j-over-slf4j')

}

compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version:'1.2.3.RELEASE'

compile group: 'org.postgresql', name: 'postgresql', version:'9.3-1102-jdbc41'

compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-hibernate4', version:'2.8.1'

compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version:'2.8.3'

compile group: 'org.apache.kafka', name: 'kafka_2.10', version:'0.9.0.0'

compile group: 'org.springframework.integration', name: 'spring-integration-kafka', version:'2.0.1.RELEASE'

compile group: 'org.apache.zookeeper', name: 'zookeeper', version:'3.4.5'

compile(group: 'commons-beanutils', name: 'commons-beanutils', version:'1.9.2') {

exclude(module: 'commons-logging')

}

compile group: 'org.json', name: 'json', version:'20090211'

compile group: 'org.codehaus.jackson', name: 'jackson-mapper-asl', version:'1.5.0'

compile(group: 'org.springframework', name: 'spring-core', version:'4.3.3.RELEASE') {

exclude(module: 'commons-logging')

}

compile group: 'com.spotify', name: 'docker-maven-plugin', version:'0.4.13'

}

configurations.all {

exclude group: "org.slf4j", module: "slf4j-log4j12"

exclude group: "log4j", module: "log4j"

}

What can I do, to avoid this clash of logging libraries, and to be able to run my jar file stand-alone?

解决方案

Couple things, it seems to me that your build.gradle should declare :

spring boot gradle plugin

apply spring boot plugin

let spring boot manage most of your dependency versions

This script works for me:

apply plugin: 'java'

apply plugin: 'maven'

apply plugin: 'spring-boot'

group = 'uptake'

version = '0.0.1-snapshot'

description = """Spring-Boot-ReceiverAPI"""

sourceCompatibility = 1.8

targetCompatibility = 1.8

tasks.withType(JavaCompile) {

options.encoding = 'UTF-8'

}

buildscript {

repositories {

mavenCentral()

}

dependencies {

classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.4.1.RELEASE'

}

}

repositories {

maven { url "http://repo.maven.apache.org/maven2" }

}

dependencies {

compile 'org.springframework.boot:spring-boot-starter'

compile 'org.springframework.boot:spring-boot-starter-logging'

compile 'org.springframework.boot:spring-boot-starter-web'

compile 'org.springframework.boot:spring-boot-starter-data-jpa'

compile 'org.postgresql:postgresql'

compile 'com.fasterxml.jackson.datatype:jackson-datatype-hibernate4'

compile 'com.fasterxml.jackson.core:jackson-core'

compile 'org.apache.kafka:kafka_2.10:0.9.0.0'

compile 'org.springframework.integration:spring-integration-kafka:2.0.1.RELEASE'

compile 'org.apache.zookeeper:zookeeper:3.4.5'

compile 'commons-beanutils:commons-beanutils'

compile 'org.json:json'

compile 'org.codehaus.jackson:jackson-mapper-asl:1.5.0'

compile 'org.springframework:spring-core'

compile 'com.spotify:docker-maven-plugin:0.4.13'

}

configurations.all {

exclude module: 'slf4j-log4j12'

exclude module: 'jms'

exclude module: 'jmxtools'

exclude module: 'jmxri'

}

Now, if you check both modules with gradle dependencyInsight, only log4j-over-slf4j will be found:

$ gradle dependencyInsight --dependency slf4j-log4j12

$ No dependencies matching given input were found...

$ gradle dependencyInsight --dependency log4j-over-slf4j

:dependencyInsight

org.slf4j:log4j-over-slf4j:1.7.21 (selected by rule)

\--- org.springframework.boot:spring-boot-starter-logging:1.4.1.RELEASE

+--- compile

\--- org.springframework.boot:spring-boot-starter:1.4.1.RELEASE

+--- compile

+--- org.springframework.boot:spring-boot-starter-web:1.4.1.RELEASE

| \--- compile

+--- org.springframework.boot:spring-boot-starter-data-jpa:1.4.1.RELEASE

| \--- compile

+--- org.springframework.boot:spring-boot-starter-aop:1.4.1.RELEASE

| \--- org.springframework.boot:spring-boot-starter-data-jpa:1.4.1.RELEASE (*)

\--- org.springframework.boot:spring-boot-starter-jdbc:1.4.1.RELEASE

\--- org.springframework.boot:spring-boot-starter-data-jpa:1.4.1.RELEASE (*)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值