SpringBoot解决jar包冲突的问题,简单有效
⽬录
SpringBoot解决jar包冲突
今天SpringBoot项⽬打包发现⼀直在报错
我查到的⼀个解决⽅案,可使⽤所有jar包冲突问题
springbootjar冲突问题集锦
1、⽇志jar包冲突
2、本地ok,测试环境失败之mainstay
3、本地ok,测试环境失败之servlet
4、本地ok,测试环境失败之tomcat
5、本地ok,测试环境失败之springasm
6、万恶的测试环境字节码验证失败
7、⽇志不能正常输出问题
8、本地打包正常
SpringBoot解决jar包冲突
今天SpringBoot项⽬打包发现⼀直在报错
包jar包冲突,了好久才到结果,期间遇到⼀⽚博客很讨⼈厌,我这⾥想评论⼀下他(原因:我没有博客园不能直接评论,所以这⾥写⼀篇博客记录⼀下,并提供正确的解决⽅案)上图:
说⼀下:⾸先,你给的⽅法很泛(不是烦没打错字)只是市⾯上⼤多数可以解决的套路,
再者,你给的解决⽅案并没有真正的解决问题。所以我留下⼀句话:不喜勿喷。
SpringBoot事实上是很好的,jar包冲突的时候,它是有提醒你了的,启动第⼀⾏就说了有多个类路径jar包存在:Class path contains multiple SLF4J bindings.
SpringBoot适合于⼩团队及个⼈开发。是⽐较⽅便的。
为什么我要写这篇博客呢,因为我看到jar冲突的真正原因后,很⽣⽓,因为原因并不⼀定在SpringBoot 我这⾥查出的是腾讯云的cos-api也引⼊的了⽇志jar,所以导致冲突,这⾥是SpringBoot引⼊cos-api,你就说SpringBoot,如果SpringBoot是被被⼈引⼊的,你是不是也要说其他的⼯具不好呢再者不好你为什么要⽤,要记录这只不过是想⽐较⽽⾔,技术没有⽼旧,没有好坏,只有喜欢,不喜欢,很多时候有些公司⽤的还是⼀⼆⼗年前的技术,可能是有些原因不能更换,但是我相信,它能存在在这个世上并风靡⼀时,说明它⼀定有它的独特之处。
接下来就是,
我查到的⼀个解决⽅案,可使⽤所有jar包冲突问题
以eclipse为例:
这样的开发⼯具都会有⼀个功能:就是查询jar包直接引⼊/jar包间接引⼊的功能
如果你不知道你使⽤的⼯具怎么到这个查页⾯:百度搜索:eclipse 看引⼊jar 包的联系(idea同理)
说实话idea的更清晰⼀些,它是图形化界⾯
到冲突的jar包,去除冲突的包即可
代码如下:
<dependency>
<groupId>com.qcloud</groupId>
<artifactId>cos_api</artifactId>
<version>5.5.3</version>
<exclusions>
<!-- <exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion> -->
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
声明:我只是对SpirngBoot报不平,没有对任何⼈做⼈⾝攻击。不喜勿喷
spring boot jar冲突问题集锦
总结下spring boot项⽬搭建过程中,冲突解决的⼀些⼩经验
1、⽇志jar包冲突
1.1、⽇志主要是spring boot⾃带spring-boot-starter-logging的排除
⼀是要⽤log4j2,⼆是xdcs不排除这个也会有冲突。(隐秘程度:低;重要程度:⾼)
<exclusion>
<artifactId>spring-boot-starter-logging</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
1.2、引的⽇志jar颇多
可能会有别的jar⾥也带了冲突的class,但是不影响启动,属于可排可不排。可能会导致测试环境起不来。(隐秘程度:低;重要程度:⾼)
//这是举个例⼦
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/xmly/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.11.2/log4j-slf4j-impl-2.11.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/xmly/.m2/repository/org/slf4j/slf4j-log4j12/1.7.5/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See /codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
//这是启动时对应的报错,经检验,这个不影响启动
2、本地ok,测试环境失败之mainstay
发测试时,出现过mainstay的yml解析失败的问题,maven helper排jar包并未显⽰有冲突,tree搜索mainstay,发现了⼀些猫腻。passport-sso-api(0.0.14-M3,下⾯⽤到的也是这个版本)这个jar包⾥
包含了这个mainstay的jar,最终导致了解析失败。后续登录验证可以接下⽹关鉴权等⽅式,这个jar冲突应该就不会出现了。(隐秘程度:⾼;重要程度:中)
<exclusion>
<artifactId>mainstay-rpc-thrift</artifactId>
<groupId>com.ximalaya.mainstay</groupId>
</exclusion>
3、本地ok,测试环境失败之servlet
发测试时,出现过servlet2和spring boot内置tomcat class重名冲突的问题,⼀般主要passport-sso-api和xdcs默认会带这个,排掉即可(隐秘程度:中;重要程度:⾼)
<exclusion>
<artifactId>servlet-api</artifactId>
<groupId>javax.servlet</groupId>
</exclusion>
4、本地ok,测试环境失败之tomcat
发测试时,也出现过passport-sso-api这个jar包⾥包含的spring-instrument-tomcat和spring boot内置tomcat冲突,排掉即可。后续登录验证可以接下⽹关鉴权等⽅式,这个jar冲突应该就不会出现了。(隐秘程度:⾼;重要程度:中)
<exclusion>
<artifactId>spring-instrument-tomcat</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
5、本地ok,测试环境失败之spring asm
测试环境出现过passport-sso-api⾥spring asm与spring-boot-test-start冲突的情况,排掉排掉。passport-sso-api这个jar包含的jar⽐较多,后⾯建议⽤注解或者⽹关鉴权来做登录控制。(隐秘程度:⾼;重要程度:中)
springboot aop
<exclusion>
<artifactId>spring-asm</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
6、万恶的测试环境字节码验证失败
之前发过⼀个前项⽬改造的spring boot项⽬就出现过,当时不以为意,让jvm参数加了-noverify就没管,后来发现很多项⽬都有这个问题,开始排查。之前看过jdk和aspectj有冲突的例⼦,就开始tree⾥搜aspectj,发现spring-boot-start-aop⾥引⼊了1.9.4版本的aspectjweaver,但是当时因为maven helper提⽰它冲突,就把它排了,后重新排掉所有1.7版本的aspectj,引⼊1.9.4版本的,解决。(隐秘程度:⾼;重要程度:⾼)
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<exclusion>
<artifactId>*</artifactId>
<groupId>org.aspectj</groupId>
</exclusion>
7、⽇志不能正常输出问题
排除,⽇志桥接混乱,后台配置的⽇志格式不⽀持了
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
Configuration:
status: info
Properties:
Property:
- name: sole
value: info
- name: log.path
value: /var/XXX/weXXX
- name: project.name
value: weXXX
- name: log.pattern
value: "%-d{yyyy-MM-dd HH:mm:ss SS} [%c]-[%p] %m%n"
Appenders:
Console:
name: CONSOLE
target: SYSTEM_OUT
PatternLayout:
pattern: ${log.pattern}
RollingRandomAccessFile:
- name: APP_FILE
fileName: ${log.path}/${project.name}.log
filePattern: "${log.path}/${project.name}-%d{yyyy-MM-dd}.log"
PatternLayout:
pattern: ${log.pattern}
Filters:
ThresholdFilter:
- level: info
onMatch: ACCEPT
onMismatch: DENY
Policies:
TimeBasedTriggeringPolicy:
modulate: true
interval: 1
DefaultRolloverStrategy:
max: 30
Loggers:
Root:
level: info
AppenderRef:
- ref: CONSOLE
- ref: APP_FILE
Logger:
- name: app
level: info
additivity: false
AppenderRef:
- ref: CONSOLE
- ref: APP_FILE
8、本地打包正常
测试或者线上环境打包失败,查看wrap.log⽇志,本地配置⽂件使⽤线上,打包测试;spring-boot-maven-plugin插件放在靠后位置,不然打包失败;
<profile>
<id>uat</id>
<properties>
<profileActive>UatXXX</profileActive>
</properties>
<build>
<plugins>
<plugin>
<groupId&jo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>2.0.0</version>
<configuration>
<configurationDirectory>conf</configurationDirectory>
<repositoryLayout>flat</repositoryLayout>
<useWildcardClassPath>true</useWildcardClassPath>
<daemons>
<daemon>
<id>${project.artifactId}</id>
<mainClass>com.XXXX.Application</mainClass>
<commandLineArguments>
<commandLineArgument>--spring.profiles.active=${profileActive}
</commandLineArgument>
</commandLineArguments>
<platforms>
<platform>jsw</platform>
</platforms>
<jvmSettings>
<!-- 启动内存配置 -->
<initialMemorySize>2048M</initialMemorySize>
<maxMemorySize>2048M</maxMemorySize>
<maxStackSize>128</maxStackSize>
<systemProperties>
<systemProperty&=.</systemProperty>
<systemProperty>spring.application.name=${project.artifactId}
</systemProperty>
<systemProperty&fig.location=./conf/${profileActive}/
</systemProperty>
</systemProperties>
<extraArguments>
<extraArgument>-XX:MetaspaceSize=256M</extraArgument>
<extraArgument>-XX:MaxMetaspaceSize=256M</extraArgument>
<extraArgument>-XX:+UseG1GC</extraArgument>
<extraArgument>-XX:-OmitStackTraceInFastThrow</extraArgument>
<extraArgument>-XX:MaxGCPauseMillis=100</extraArgument>
<extraArgument>-XX:+ParallelRefProcEnabled</extraArgument>
<extraArgument>-XX:+HeapDumpOnOutOfMemoryError</extraArgument>
<extraArgument>-XX:+PrintCommandLineFlags</extraArgument>
<extraArgument>-XX:+PrintGCDetails</extraArgument>
<extraArgument>-XX:+PrintGCDateStamps</extraArgument>
<extraArgument>-verbose:class</extraArgument>
<extraArgument>-XX:+PrintClassHistogramBeforeFullGC</extraArgument>
<extraArgument>-XX:+PrintClassHistogramAfterFullGC</extraArgument>
<extraArgument>-XX:+PrintTenuringDistribution</extraArgument>
<extraArgument>-XX:+PrintHeapAtGC</extraArgument>
<extraArgument>-XX:+PrintGCApplicationStoppedTime</extraArgument>
<extraArgument>-XX:+PrintGCApplicationConcurrentTime</extraArgument>
<extraArgument>-Xloggc:/var/log/${project.artifactId}/gc-%t</extraArgument>                                    <extraArgument>-XX:+UseGCLogFileRotation</extraArgument>
<extraArgument>-XX:GCLogFileSize=10M</extraArgument>
<extraArgument>-XX:NumberOfGCLogFiles=10</extraArgument>
<extraArgument>-javaagent:/opt/jars/aspectjweaver-1.8.9.jar</extraArgument>                                </extraArguments>
</jvmSettings>
<generatorConfigurations>
<generatorConfiguration>
<generator>jsw</generator>
<includes>
<include>linux-x86-64</include>
<include>macosx-universal-64</include>
</includes>
<configuration>
<property>
<name>configuration.directory.in.classpath.first</name>
<value>conf</value>
</property>
<property>
<name>wrapper.ping.timeout</name>
<value>60</value>
</property>
<property>
<name>set.default.REPO_DIR</name>
<value>lib</value>
</property>
<property>
<name>wrapper.logfile</name>
<value>/var/XXXX/${project.artifactId}/wrapper.YYYYMMDD.log</value>
</property>
<property>
<name>llmode</name>
<value>DATE</value>
</property>
<property>
<name>wrapper.logfile.maxfiles</name>
<value>10</value>
</property>
<property>
<name>wrapper.pidfile</name>
<value>/var/XXXX//${project.artifactId}</value>
</property>
<property>
<name>wrapper.javamand</name>
<value>/usr/local/jdk8/bin/java</value>
</property>
<property>
<name>wrapper.disable_restarts</name>
<value>TRUE</value>
</property>
<property>
<name>wrapper.jvm_exit.timeout</name>
<value>5</value>
</property>
<property>
<name>wrapper.shutdown.timeout</name>
<value>5</value>
</property>
<property>
<name>wrapper.cpu.timeout</name>
<value>0</value>
</property>
</configuration>
</generatorConfiguration>
</generatorConfigurations>
</daemon>
</daemons>
</configuration>
<executions>
<execution>
<id>generate-jsw</id>
<phase>package</phase>
<goals>
<goal>generate-daemons</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>src/main/l</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
<configuration>
<additionalProperties>
<encoding.source>UTF-8</encoding.source>
&porting>UTF-8</porting>
<java.source>${java.version}</java.source>
<java.target>${java.version}</java.target>
</additionalProperties>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
tips:
1、不要太相信maven helper提⽰的jar冲突,具体还是要具体分析
2、有些jar冲突⽹上容易查到,有些基本查不到,可以先去tree搜个短名看看,看看有没有相似的jar,再去查是否真的会有冲突
3、有些是class冲突,到对应的jar排掉即可,尽量别排spring⾃带的,容易出现兼容性问题
4、启动时,我已经将⼀些不必须的autoConfigure排掉了,真要⽤到,记得放出来,平时⽤不到的尽量排掉,这样就不会去加载了,不然很多都是默认加载的。以上为个⼈经验,希望能给⼤家⼀个参考,
也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。