【发布时间】:2012-04-05 03:17:15
【问题描述】:
我正在使用 Maven 3 构建一个具有 3 层的 Java 应用程序 - 服务器、ejb 和 ui。 EJB项目依赖Server项目,UI项目只依赖EJB,并提供了对Server传递依赖的排除。
当 UI 项目构建为战争时,尽管它没有显示在 dependency:tree 命令中,但仍包含服务器依赖项。
这是运行mvn dependency:tree的相关输出
**project.name:UI:war:1.0 SNAPSHOT**
+- project.name:Common:jar:1.0 SNAPSHOT:compile
| + org_common:_lib:jar:16.0.006:compile
| | +- log4j:log4j:jar:1.2.16:compile
| | \- commons configuration:commons configuration:jar:1.6:compile
| | +- commons lang:commons lang:jar:2.4:compile
| | +- commons digester:commons digester:jar:1.8:compile
| | \- commons beanutils:commons beanutils core:jar:1.8.0:compile
| +- org_common:_security_lib:jar:16.0.006:compile
| \- org.springframework:spring:jar:2.0:compile
+- **project.name:EJB:ejb client:client:1.0 SNAPSHOT:compile**
| \- com.ibm.websphere.appserver:j2ee:jar:7.0.0.9:compile
+- org_common:_uicomponent:jar:16.0.006:compile
这是运行mvn clean install -X时的输出依赖树
**project.name:UI:war:1.0 SNAPSHOT**
+- project.name:Common:jar:1.0 SNAPSHOT:compile
| + org_common:_lib:jar:16.0.006:compile
| | +- log4j:log4j:jar:1.2.16:compile
| | \- commons configuration:commons configuration:jar:1.6:compile
| | +- commons lang:commons lang:jar:2.4:compile
| | +- commons digester:commons digester:jar:1.8:compile
| | \- commons beanutils:commons beanutils core:jar:1.8.0:compile
| +- org_common:_security_lib:jar:16.0.006:compile
| \- org.springframework:spring:jar:2.0:compile
+- **project.name:EJB:ejb client:client:1.0 SNAPSHOT:compile**
| +- **project.name:Server:jar:1.0 SNAPSHOT:compile**
| | +- javassist:javassist:jar:3.4.GA:compile
| | +- project.filestore:filestore_client:jar:7.0.003:compile
| | +- com.ibm.db2:db2jcc:jar:9.7.fp1.aix64.s091114:compile
| | +- com.ibm.db2:db2java:jar:9.7.fp1.aix64.s091114:compile
| | +- com.ibm.db2:db2jcc_license_cu:jar:9.7.fp1.aix64.s091114:compile
| \- com.ibm.websphere.appserver:j2ee:jar:7.0.0.9:compile
+- org_common:_uicomponent:jar:16.0.006:compile
对 Server 的依赖是两棵树之间的唯一区别。 这两个输出不应该总是相同的吗?什么可能导致包含未显示在依赖项中的库:树?
父 POM 将模块定义为:
<modules>
<module>Server</module>
<module>EJB</module>
<module>UI</module>
</modules>
EJB POM 中列出的依赖项是:
<dependencies>
<dependency>
<groupId>project.name</groupId>
<artifactId>Server</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
UI中的依赖是:
<dependencies>
<dependency>
<groupId>project.name</groupId>
<artifactId>EJB</artifactId>
<version>${project.version}</version>
<type>ejb-client</type>
<exclusions>
<exclusion>
<groupId>project.name</groupId>
<artifactId>Server</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
我知道我可以明确地将服务器 jar 排除在 WAR 中,但我更愿意解决实际问题。
【问题讨论】:
-
如果 UI 依赖于 EJB 而 EJB 依赖于 Server,那么 UI 依赖于 Server。因此,您可以通过将其范围设置为
provided来从WEB-INF/lib中排除具有传递依赖关系的EJB,或者将它们全部打包到war 中。据我所知,没有办法只将基础工件包含在战争中而没有其传递依赖。 -
你使用的是最新的依赖插件(2.4)吗?
-
@AndrewLogvinov 这就是为什么当 EJB 项目作为对 UI 的依赖项添加时,我在服务器上排除的原因。 EJB 层在 maven 中是特殊的——它分为客户端和服务器 jar——UI 所依赖的客户端 jar 不依赖于服务器。如果明确排除传递依赖,为什么要包含它?
-
@khmarbaise 我不是,但更新到 2.4 后问题是一样的。
-
您使用的是哪个 Maven 版本?如果不是 3.0.4,试试看它是否有帮助。在使用以前的 Maven 3 版本(主要是 3.0.2)时,我发现了非常非常糟糕的问题。