1. 程式人生 > >【工作記錄】Found Banned Dependency: commons-logging:commons-logging 依賴衝突解決

【工作記錄】Found Banned Dependency: commons-logging:commons-logging 依賴衝突解決

【問題概述】

     在使用maven構建專案的時候,偶爾會遇到jar包依賴衝突的問題,比如:

[WARNING] Rule 2: org.apache.maven.plugins.enforcer.BannedDependencies failed with message:
Found Banned Dependency: commons-logging:commons-logging:jar:1.1.3
Use 'mvn dependency:tree' to locate the source of the banned dependencies.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 45.682 s
[INFO] Finished at: 2018-07-11T19:31:20+08:00
[INFO] Final Memory: 54M/487M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-enforcer-plugin:1.1.1:enforce (enforce-banned-dependencies) on project fcarXXX: Some Enforcer rules have failed. Look above for specific messages explaining why the rule failed. -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

     從這句“Found Banned Dependency: commons-logging:commons-logging:jar:1.1.3”可知,衝突的包是commons-logging。


【解決思路】

     1.遇到maven依賴衝突,首先想到的是通過依賴樹來檢視衝突,命令為“mvn dependency:tree”。

       

     如圖,dos命令下檢視的專案依賴太多了,直接依賴和傳遞依賴看的眼花繚亂,專案較大的話,排查版本衝突的jar很困難。幸好IDEA可以通過視覺化工具來檢查專案jar包的依賴關係。


    2.IDEA視覺化檢視jar包依賴關係

    點選“Maven Project” -> “Show Dependencies”按鈕:

      

       其中的紅線代表有衝突,最左側代表專案,如圖,雖然可視化了,但是依賴的jar太多了,紅線也很多,還是沒有定位到控制檯中提示的“Commons-logging:1.1.3”,試著選中可疑的幾個jar,右擊“Exclude”,之後重新Compile,仍然報錯。不太好用。


      3."-Dverbose -Dincludes"附加條件定位問題

      嘗試了上述2中方案,都不是很理想能解決我的問題,之後在文件中看到“mvn dependency:tree”命令可以做模糊或者精確的匹配:

mvn dependency:tree -Dverbose -Dincludes=“xxx”

       重新嘗試了之後,發現對"Commons-logging 1.1.3"的傳遞依賴,如圖:

      

      發現這個傳遞依賴是由於我的pom中dependency了“base-dao-es”這個jar,之後對它做exclude操作:

<dependency>
	<groupId>com.XXX.XXX</groupId>
	<artifactId>base-dao-es</artifactId>
	<version>${framework.version}</version>
	<exclusions>
		<exclusion>
			<artifactId>commons-logging</artifactId>
			<groupId>commons-logging</groupId>
		</exclusion>
	</exclusions>
</dependency>
        之後重新編譯,成功了。