【问题标题】:How to debug conflicting dependencies in Maven project如何调试 Maven 项目中的冲突依赖项
【发布时间】:2021-02-17 16:58:12
【问题描述】:

当我构建我的 Maven 管理的 Java 项目时,同一工件的两个版本被安装到构建中(一个战争文件)。我很确定这没关系,但有问题的工件是 com.fasterxml.jackson.core:jackson-annotations 版本 2.7.3 和 2.9.7 .

当我运行 mvn dependency:tree -Dverbose 时,没有出现 jackson-annotations:2.7.3,确实没有出现字符串 2.7.3 在结果输出中的任何位置。

我的问题是,我该如何继续调试这个问题?我相信 dependency:tree 目标会给我完整的传递依赖。这种信念正确吗?如果没有,会怎样?

作为记录,在我的本地机器上找到了新版本的库并且一切正常,但是当部署到另一台机器时部署被破坏,因为该机器找到了旧版本的库。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>mygroup</groupId>
    <artifactId>something</artifactId>
    <version>0.0.48</version>
    <packaging>war</packaging>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>mygroup</groupId>
        <artifactId>a</artifactId>
        <version>0.0.34</version>
    </dependency>

    <dependency>
        <groupId>mygroup</groupId>
        <artifactId>b</artifactId>
        <version>0.0.36</version>
    </dependency>

     <!-- Jersey -->

    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.1.12</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>


    <dependency>
        <groupId>org.glassfish.jersey.test-framework</groupId>
        <artifactId>jersey-test-framework-core</artifactId>
        <version>2.25.1</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.25.1</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <version>2.25.1</version>
        <scope>test</scope>
    </dependency>

    <!-- Jersey Test Framework -->

    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>2.25.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>2.25.1</version>
        <scope>test</scope>
    </dependency>

     <dependency>
        <groupId>
            org.glassfish.jersey.test-framework.providers
        </groupId>
        <artifactId>
            jersey-test-framework-provider-grizzly2
        </artifactId>
        <version>2.25.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.25.1</version>
        <scope>test</scope>
    </dependency>

</dependencies>

<repositories>
    <repository>
        <id>myrepository</id>
        <name>My repository name</name>
        <url>myrepositoryurl</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>


<build>
    <sourceDirectory>src/main</sourceDirectory>
    <testSourceDirectory>src/test</testSourceDirectory>
    <resources>
        <resource>
            <directory>.</directory>
            <includes>
                <include>pom.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <warSourceDirectory>WebContent</warSourceDirectory>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
   </plugins>
 </build>
</project>

【问题讨论】:

  • 我希望 dependency:tree 能找到这个。你是如何建立你的战争的?您可以添加一个简化的 pom 来说明您的问题吗?
  • @tgdavies 我简化了 pom 并将其添加到问题中。你在找什么?

标签: java maven dependencies conflict transitive-dependency


【解决方案1】:

嗯。

这很尴尬。

这与 Maven 或其依赖计算无关。相反,我的项目中有一个 WEB-INF/lib 文件夹,其中包含冲突的库,并且在构建时被合并到 .war 文件中。

感谢大家看到这个并试图帮助解决一个空想问题。

【讨论】:

    【解决方案2】:

    您可以运行dependency:tree goal,以确保它使用最新版本的自身内部结构(就像您可以从命令行运行的任何 Maven 插件一样):

    $ mvn org.apache.maven.plugins:maven-dependency-plugin:3.1.2:tree
    

    (在 Ancient Times™ 中,dependency:tree 目标的依赖解析算法可能与 Maven 本身实际使用的不匹配。可能是您在不知不觉中使用了旧版本的插件。)

    【讨论】:

    • 有趣的评论。如何确定maven的依赖解析算法的版本,以及与之匹配的插件版本?
    • 老实说,只需使用最新版本的maven-dependency-plugin,在撰写本文时,它是 3.1.2。
    • 谢谢。我这样做了,得到了完全相同的结果。
    • 我看到您还在使用非常非常旧的maven-war-plugin 版本。也许它也在使用旧的依赖解析算法。尝试将2.6 替换为3.3.1
    【解决方案3】:

    您可以无需调试,只需扩展所有项目依赖项并通过您正在查看的项目进行查看

    然后在该依赖项上,您可以在 pom.xml 中排除一些内部库,即:

            <dependency>
            <groupId>com.core</groupId>
            <artifactId>transactions-core</artifactId>
            <version>${utransactions.core}</version>
    <!-- this exclusion -->
            <exclusions>
                <exclusion>
                    <artifactId>log4j-api</artifactId>
                    <groupId>org.apache.logging.log4j</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>log4j-core</artifactId>
                    <groupId>org.apache.logging.log4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>
    

    【讨论】:

    • 我做到了(显然)。该观点与 dependency:tree 一致。包含在 war 文件中的库也未在树的该视图中提及。
    猜你喜欢
    • 1970-01-01
    • 2017-07-18
    • 2016-07-18
    • 1970-01-01
    • 1970-01-01
    • 2020-09-03
    • 1970-01-01
    • 2011-03-12
    相关资源
    最近更新 更多