[英]Maven cannot exec a jar project because the mess with transitive dependency
我有5個maven項目: simple-model
, simple-weather
, simple-persist
, simple-webapp
和simple-console
。 (它們來自Maven權威指南書)。 simple-console
取決於simple-weather
和simple-persist
。 simple-persist
和simple-weather
都取決於simple-model
:
simple-console ---> simple-weather ----> simple-model
\ /
\---> simple-persist--/
simple-console
依賴於spring
和hibernate
,實際上取決於包含javax.transaction.TransactionManager
的jar。 我為這5個項目生成了eclipse項目。 simple-console
(調用Hibernate方法)中的Main
類在eclipse中運行良好。 但是,當我嘗試使用Maven
的exec:java
目標或java -cp
(使用jar-with-dependencies
組裝)執行它時,我總是遇到此錯誤: Caused by: java.lang.NoClassDefFoundError: javax/transaction/TransactionManager
它真的讓我很困惑,因為雖然eclipse知道如何在執行之前包含必要的jar,maven插件卻沒有意義。 為什么?
P / S:我已經在這里上傳了這些項目的源代碼: http : //seamoo.com/simple-parent.tar.gz
如果在simple-console中運行mvn dependency:tree
,則不會看到JTA工件:
[INFO] org.seamoo:simple-console:jar:1.0 [INFO] +- junit:junit:jar:3.8.1:test [INFO] +- org.seamoo:simple-persist:jar:1.0:compile [INFO] | +- org.seamoo:simple-model:jar:1.0:compile [INFO] | +- org.hibernate:hibernate:jar:3.2.5.ga:compile [INFO] | | +- net.sf.ehcache:ehcache:jar:1.2.3:compile [INFO] | | +- asm:asm-attrs:jar:1.5.3:compile [INFO] | | +- antlr:antlr:jar:2.7.6:compile [INFO] | | +- cglib:cglib:jar:2.1_3:compile [INFO] | | +- asm:asm:jar:1.5.3:compile [INFO] | | \- commons-collections:commons-collections:jar:2.1.1:compile [INFO] | +- org.hibernate:hibernate-annotations:jar:3.3.0.ga:compile [INFO] | | \- javax.persistence:persistence-api:jar:1.0:compile [INFO] | \- org.hibernate:hibernate-commons-annotations:jar:3.3.0.ga:compile [INFO] +- org.seamoo:simple-weather:jar:1.0:compile [INFO] | +- log4j:log4j:jar:1.2.14:compile [INFO] | +- dom4j:dom4j:jar:1.6.1:compile [INFO] | | \- xml-apis:xml-apis:jar:1.0.b2:compile [INFO] | +- jaxen:jaxen:jar:1.1.1:compile [INFO] | | +- jdom:jdom:jar:1.0:compile [INFO] | | +- xerces:xercesImpl:jar:2.6.2:compile [INFO] | | \- xom:xom:jar:1.0:compile [INFO] | | +- xerces:xmlParserAPIs:jar:2.6.2:compile [INFO] | | +- xalan:xalan:jar:2.6.0:compile [INFO] | | \- com.ibm.icu:icu4j:jar:2.6.1:compile [INFO] | \- velocity:velocity:jar:1.5:compile [INFO] | +- commons-lang:commons-lang:jar:2.1:compile [INFO] | \- oro:oro:jar:2.0.8:compile [INFO] +- org.springframework:spring:jar:2.0.7:compile [INFO] | \- commons-logging:commons-logging:jar:1.1:compile [INFO] | +- logkit:logkit:jar:1.0.1:compile [INFO] | +- avalon-framework:avalon-framework:jar:4.1.3:compile [INFO] | \- javax.servlet:servlet-api:jar:2.3:compile [INFO] \- hsqldb:hsqldb:jar:1.8.0.7:compile
事實上,如果你看一下simple-persist/pom.xml
,你會發現它已被排除:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.2.5.ga</version>
<exclusions>
<exclusion>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
</exclusion>
</exclusions>
</dependency>
我的猜測是,JTA神器被排除在外,因為完整的Hibernate JAR( hibernate-3.2.5.ga.jar
這里)依賴於jta-1.0.1B.jar
這是不是在中央回購可由於授權問題,同時休眠核心(即hibernate-core-3.3.0.GA.jar
)依賴於jta-1.1.jar
,它可從中心獲得。
它在Eclipse中工作的原因是簡單模型依賴於hibernate-annotations-3.3.0.ga.jar
,它依賴於依賴於jta-1.0.1B.jar
hibernate-3.2.1.GA.jar
jta-1.0.1B.jar
(即你很可能在你的本地存儲庫中)。 因此,因為整個hibernate依賴項在這個項目中是一個很大的混亂,因為存在大的依賴性收斂問題,Eclipse“看到”了JTA jar(這只是一個幸運或不幸的副作用)。 但是Maven沒有。
要解決這個問題,請刪除simple-persist/pom.xml
中的JTA排除(這是快速且非常臟的修復)或修復hibernate依賴項以使它們收斂(這將是正確的修復):
hibernate-core-3.3.0.SP1.jar
hibernate-annotations-3.4.0.GA.jar
(取決於前者)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.