当Maven依赖插件位于
問題:
我們進行了一個集成測試,該測試創建了一個Spring ClassPathXmlApplicationContext ,同時這樣做導致NoSuchMethodError爆炸。 事實證明,我們對Spring構件的依賴版本存在沖突。 這本身不是一個不尋常的問題-使用Maven依賴插件使用verbose選項解決了這些問題。 但是,當Maven插件錯誤時,您該怎么辦?
調查:
我們開始深入研究,發現AbstractAutowireCapableBeanFactory的getTypeForFactoryMethod方法嘗試訪問GenericTypeResolver resolveReturnTypeForGeneric方法,并在java.lang.NoSuchMethodError: org.springframework.core.GenericTypeResolver.resolveReturnTypeForGenericMethod(Ljava/lang/reflect/Method; 。
初步調查和谷歌搜索發現,該方法是在3.2.0中添加的,而我們應該在3.1.1中運行。 進一步的調查確定spring-data-mongodb依賴于范圍[3.0.7-4) 1的 spring框架,并且由于maven在給定范圍2的情況下采用了最新的可用版本,因此它嘗試采用3.2.2。
注意,在顯式版本依賴項和范圍依賴項之間存在沖突的情況下,上述更改有所變化,但是IINM在確定spring mongo的依賴項時沒有沖突。
該問題被兩個癥狀進一步掩蓋:
Maven依賴項:在構件中使用顯示spring-beans:3.1.1的樹輸出
>:mvn dependency:tree -Dverbose -Dincludes=org.springframework ... (omitted for clarity) ... [INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ wix-feature-toggle-administration --- [INFO] artifact org.springframework:spring-beans: checking for updates from central [INFO] artifact org.springframework:spring-beans: checking for updates from snapshots [INFO] artifact org.springframework:spring-expression: checking for updates from central [INFO] artifact org.springframework:spring-expression: checking for updates from snapshots [INFO] artifact org.springframework:spring-tx: checking for updates from central [INFO] artifact org.springframework:spring-tx: checking for updates from snapshots [INFO] com.wixpress.common:wix-feature-toggle-administration:jar:2.180.0-SNAPSHOT ... [INFO] +- org.springframework.data:spring-data-mongodb:jar:1.0.1.RELEASE:compile [INFO] | +- org.springframework:spring-beans:jar:3.1.1.RELEASE:compile [INFO] | | \- (org.springframework:spring-core:jar:3.2.2.RELEASE:compile - omitted for conflict with 3.1.1.RELEASE) [INFO] | +- org.springframework:spring-expression:jar:3.1.1.RELEASE:compile [INFO] | | \- (org.springframework:spring-core:jar:3.2.2.RELEASE:compile - omitted for conflict with 3.1.1.RELEASE) [INFO] | \- org.springframework.data:spring-data-commons-core:jar:1.2.1.RELEASE:compile [INFO] | +- (org.springframework:spring-beans:jar:3.1.1.RELEASE:compile - omitted for duplicate) [INFO] | \- (org.springframework:spring-tx:jar:3.1.1.RELEASE:compile - omitted for duplicate) [INFO] +- com.wixpress.common:wix-framework:jar:2.180.0-SNAPSHOT:compile [INFO] | +- org.springframework:spring-core:jar:3.1.1.RELEASE:compile [INFO] | | \- org.springframework:spring-asm:jar:3.1.1.RELEASE:compile ... I've removed additional outputs for clarity. The additional outputs were all 3.1.1 and were further down the tree (so irrelevant due to maven conflict resolving mechanism)工件中使用了證明spring-beans:3.2.2的測試(斷言錯誤中的jvm在說什么)
package com.wixpress.springVersionBug;import org.junit.*; import org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory; import org.springframework.core.GenericTypeResolver; import java.security.CodeSource; import static org.hamcrest.Matchers.endsWith;/*** @author ittaiz* @since 3/24/13*/public class SpringVersionTest {@Testpublic void verifySpringBeans311InClasspath(){verifyCorrectSpringVersionInClasspathFor(AbstractAutowireCapableBeanFactory.class,"spring-beans-3.1.1.RELEASE.jar");}@Testpublic void verifySpringCore311InClasspath(){verifyCorrectSpringVersionInClasspathFor(GenericTypeResolver.class,"spring-core-3.1.1.RELEASE.jar");}public void verifyCorrectSpringVersionInClasspathFor(Class springClass,String expectedJarFileName){CodeSource springClassCodeSource = springClass.getProtectionDomain().getCodeSource();Assert.assertNotNull("expecting "+expectedJarFileName+" to be loaded by non-system class loader",springClassCodeSource);Assert.assertThat(springClassCodeSource.getLocation().toString(),endsWith(expectedJarFileName));} }當spring-beans成為3.2.2時, spring-core工件出現在3.1.1中的原因是我們的框架顯式依賴于spring-core而該工件顯式依賴于框架。 這意味著來自框架的spring-core 3.1.1是2跳,比來自spring-data-mongodb的3.2.2短。
解:
依賴spring-data-mongodb同時像這樣排除spring-beans :
<dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-mongodb</artifactId><version>1.0.1.RELEASE</version><exclusions><exclusion><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId></exclusion></exclusions> </dependency>開放問號:
為什么dependency:tree(在詳細模式下)沒有顯示在3.2.2中而是在3.1.1中顯示spring-beans,同時明確指定由于沖突而刪除了spring-core 3.2.2? 我將此歸結為依賴項插件中的錯誤。
翻譯自: https://www.javacodegeeks.com/2013/04/when-maven-dependency-plugin-lies.html
總結
以上是生活随笔為你收集整理的当Maven依赖插件位于的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 软件开发备案在哪里(软件开发备案)
- 下一篇: 安卓代码颜色怎么改(安卓代码颜色)