我有 5 个 Maven 项目:simple-model
、simple-weather
、simple-persist
、simple-webapp
code> 和 简单控制台
。 (它们来自《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
中的 Main
类(调用 Hibernate 方法)在 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
And indeed, if you look at simple-persist/pom.xml
, you'll see that it has been excluded:
<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 核心(即 hibernate-core-3.3.0.GA.jar
)取决于 jta-1.1.jar
,可从中央获得。
它在 Eclipse 中工作的原因是 simple-model 依赖于 hibernate-annotations-3.3.0.ga.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
(取决于前者)
关于java - Maven 无法执行 jar 项目,因为传递依赖性困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2368853/