maven-enforcer-plugin解決jar包衝突問題

jar包或者類衝突,通常日誌中出現相似於: 

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

如下內容有興趣可接着閱讀,通常無需瞭解。

關於NoClassDefFoundError能夠參考:https://stackoverflow.com/questions/7325579/java-lang-noclassdeffounderror-could-not-initialize-class-xxxjava

maven enforcer插件詳細自定義配置說明:

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

代碼塊github

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <version>1.4.1-dp-SNAPSHOT</version>
    <executions>
        <execution>
            <id>default-cli</id>
            <phase>validate</phase>
            <goals>
                <goal>enforce</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>extra-enforcer-rules</artifactId>
            <version>1.0-beta-4</version>
        </dependency>
    </dependencies>
    <configuration>
        <!--規則檢查不經過就構建失敗;Default:false. -->
        <!--<failFast>true</failFast>-->
        <rules>
            <!--<requireMavenVersion>
                <version>3.0.4</version>
            </requireMavenVersion>-->
            <requireJavaVersion>
                <version>1.6.0</version>
            </requireJavaVersion>
            <banDuplicateClasses>
                <ignoreClasses>
                    <ignoreClass>javax.*</ignoreClass>
                    <ignoreClass>org.junit.*</ignoreClass>
                    <ignoreClass>junit.*</ignoreClass>
                    <ignoreClass>org.apache.xbean.recipe.*</ignoreClass>
                    <ignoreClass>org.hamcrest.*</ignoreClass>
                    <ignoreClass>org.slf4j.*</ignoreClass>
                    <ignoreClass>org.antlr.runtime.*</ignoreClass>
                    <ignoreClass>org.apache.commons.lang.*</ignoreClass>
                    <ignoreClass>org.apache.commons.codec.*</ignoreClass>
                    <ignoreClass>org.apache.commons.logging.*</ignoreClass>
                    <ignoreClass>org.codehaus.plexus.component.builder.*</ignoreClass>
                </ignoreClasses>
                <findAllDuplicates>true</findAllDuplicates>
            </banDuplicateClasses>
            <bannedDependencies>
                <!--是否檢查傳遞性依賴(間接依賴)-->
                <searchTransitive>true</searchTransitive>
                <excludes>
                    <exclude>org.apache:libthrift</exclude>
                    <exclude>org.jboss.netty:netty</exclude>
                    <exclude>org.apache.thrift:libthrift:(,0.8.0),(0.8.0,)</exclude>
                    <exclude>io.netty:netty:(,3.9.2.Final)</exclude>
                    <exclude>com.sankuai.octo:mns-invoker:(,1.7.5)</exclude>
                    <exclude>com.fasterxml.jackson.core:*:(,2.6.0)</exclude>
                    <exclude>com.google.guava:guava:(,15.0)</exclude>
                </excludes>
                <message>some dependency must exclude</message>
            </bannedDependencies>
        </rules>
    </configuration>
</plugin>

而後輸入maven命令來查找重複類和存在版本限制和衝突的jar包:apache

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

會看到相關的輸出,一個個排查便可:ide

存在重複的類的狀況:ui

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包或不容許的依賴版本號:google

[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

福利:使用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 

相關文章
相關標籤/搜索