maven-enforcer-plugin解决jar包冲突问题

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

jar包或者类冲突,一般日志中出现类似于: 

java.lang.NoClassDefFoundError,java.lang.NoSuchMethodError,java.lang.NoSuchFieldError,java.lang.NoSuchMethodException,java.lang.LinkageError,java.lang.ClassNotFoundException

以下内容有兴趣可接着阅读,一般无需了解。

关于NoClassDefFoundError可以参考:https://stackoverflow.com/questions/7325579/java-lang-noclassdeffounderror-could-not-initialize-class-xxx

maven enforcer插件详细自定义配置说明:

首先在maven插件中添加enforcer插件:

代码块


    org.apache.maven.plugins
    maven-enforcer-plugin
    1.4.1-dp-SNAPSHOT
    
        
            default-cli
            validate
            
                enforce
            
        
    
    
        
            org.codehaus.mojo
            extra-enforcer-rules
            1.0-beta-4
        
    
    
        
        
        
            
            
                1.6.0
            
            
                
                    javax.*
                    org.junit.*
                    junit.*
                    org.apache.xbean.recipe.*
                    org.hamcrest.*
                    org.slf4j.*
                    org.antlr.runtime.*
                    org.apache.commons.lang.*
                    org.apache.commons.codec.*
                    org.apache.commons.logging.*
                    org.codehaus.plexus.component.builder.*
                
                true
            
            
                
                true
                
                    org.apache:libthrift
                    org.jboss.netty:netty
                    org.apache.thrift:libthrift:(,0.8.0),(0.8.0,)
                    io.netty:netty:(,3.9.2.Final)
                    com.sankuai.octo:mns-invoker:(,1.7.5)
                    com.fasterxml.jackson.core:*:(,2.6.0)
                    com.google.guava:guava:(,15.0)
                
                some dependency must exclude
            
        
    

然后输入maven命令来查找重复类和存在版本限制和冲突的jar包:

mvn -U clean -Dmaven.test.skip=true enforcer:enforce -DcheckDeployRelease_skip=true

会看到相关的输出,一个个排查即可:

存在重复的类的情况:

Found in:
    org.jboss.netty:netty:jar:3.2.19:compile
    io.netty:netty:jar:3.9.2.Final:compile
  Duplicate classes:
    org/jboss/netty/handler/codec/base64/Base64Decoder.class
    org/jboss/netty/util/VirtualExecutorService.class
以下省略

不允许的依赖jar包或不允许的依赖版本号:

[WARNING] Rule 2: org.apache.maven.plugins.enforcer.BannedDependencies failed with message:
some dependency must exclude
Found Banned Dependency: org.apache:libthrift:jar:0.6.0
Found Banned Dependency: org.jboss.netty:netty:jar:3.2.19
Found Banned Dependency: org.apache.thrift:libthrift:jar:0.9.3
Use 'mvn dependency:tree' to locate the source of the banned dependencies.

最后根据插件排查下来的信息,一个个排除掉存在冲突的jar包即可。注意如果是parent pom当中的依赖冲突,要在parent pom中修改,否则无法排除。

福利:使用idea的同学,推荐使用maven helper插件排除冲突,一目了然,很好用。

enforcer插件参考文档:

https://www.cnblogs.com/f-zhao/p/6961058.html

http://www.tuicool.com/articles/RfIBfa

https://github.com/dimitri-koussa/maven-enforcer-duplicate-class-check

http://www.mojohaus.org/extra-enforcer-rules/banDuplicateClasses.html 

转载于:https://my.oschina.net/u/1421030/blog/1925799

你可能感兴趣的:(maven-enforcer-plugin解决jar包冲突问题)