【问题标题】:Maven Build different to Dependency TreeMaven Build 与依赖树不同
【发布时间】: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)时,我发现了非常非常糟糕的问题。

标签: java maven


【解决方案1】:

你说得对,两种情况下的输出都应该是一样的。然而,Maven 3 转而使用 Aether 进行依赖解析,但截至目前,dependency:tree 使用旧的依赖解析机制,这就是差异的原因。详情请查看以下链接。

https://cwiki.apache.org/MAVEN/maven-3x-compatibility-notes.html#Maven3.xCompatibilityNotes-DependencyResolution

由于这个原因,你应该只依赖 mvn clean install -X 的输出来进行依赖管理。

编辑

自 Maven 依赖插件 2.5 版以来,dependency:tree 也使用 Aether(请参阅 bug reportrelease notes

【讨论】:

  • 这就解释了为什么这两棵树是不同的。知道如何纠正传递依赖问题吗?
【解决方案2】:

正如我们在 cmets 中发现的,问题的根源是有缺陷的 Maven 3.0.3。 3.0.4 版本解决了这个问题。

我的评论:

您使用的是哪个 Maven 版本?如果不是 3.0.4,试试看它是否有帮助。在使用以前的 Maven 3 版本(主要是 3.0.2)时,我发现了非常非常糟糕的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-11
    • 2018-01-12
    • 1970-01-01
    • 2015-07-15
    • 2017-09-13
    • 2019-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多