【问题标题】:Why is Maven re-adding already existent dependencies when adding a new dependency?为什么 Maven 在添加新依赖项时会重新添加已经存在的依赖项?
【发布时间】:2016-07-25 22:00:12
【问题描述】:

在我正在进行的项目中,我的团队有以下 pom 文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         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>com.example</groupId>
    <artifactId>example-project</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>example-project</name>
    <description>example-project</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.6.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.amazonaws</groupId>
                <artifactId>aws-java-sdk-bom</artifactId>
                <version>1.11.13</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-s3</artifactId>
            <version>1.11.13</version>
        </dependency>

        <dependency>
            <groupId>com.netflix.hystrix</groupId>
            <artifactId>hystrix-core</artifactId>
            <version>1.5.3</version>
        </dependency>

        <dependency>
            <groupId>com.netflix.hystrix</groupId>
            <artifactId>hystrix-metrics-event-stream</artifactId>
            <version>1.5.3</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>


    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>2.17</version>
                <dependencies>
                    <dependency>
                        <groupId>com.puppycrawl.tools</groupId>
                        <artifactId>checkstyle</artifactId>
                        <version>7.0</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <configLocation>google_checks.xml</configLocation>
                </configuration>
                <executions>
                    <execution>
                        <id>checkstyle-check</id>
                        <phase>validate</phase>
                        <configuration>
                            <configLocation>
                                http://dummy-url.com
                            </configLocation>
                            <encoding>UTF-8</encoding>
                            <consoleOutput>true</consoleOutput>
                            <includeTestSourceDirectory>true</includeTestSourceDirectory>
                            <failsOnError>true</failsOnError>
                            <failOnViolation>true</failOnViolation>
                        </configuration>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.4.201502262128</version>
                <executions>
                    <execution>
                        <id>default-prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>default-report</id>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>default-check</id>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <excludes>
                        <exclude>**/Application.*</exclude>
                    </excludes>
                    <rules>
                        <rule>
                            <element>BUNDLE</element>
                            <limits>
                                <limit>
                                    <counter>COMPLEXITY</counter>
                                    <value>COVEREDRATIO</value>
                                    <minimum>0.80</minimum>
                                </limit>
                            </limits>
                        </rule>
                    </rules>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

我在 Intellij 提供的 Maven 工具栏中附上了一个 Maven 依赖项的屏幕截图,它来自这个 POM 文件。

请注意,红色突出显示的部分仅在我们尝试添加 spring-boot JPA 依赖项时出现。没有它,我们的项目运行良好。似乎 Maven 正在重新添加已经添加的依赖项,这很奇怪,因为例如 Hystrix 与 JPA 没有任何关系。

有人知道如何解决这个问题吗?

【问题讨论】:

  • Hysterix 是您项目的依赖项(在您发布的 Pom 中)。你为什么不期待呢?

标签: java spring maven intellij-idea


【解决方案1】:

Maven 似乎会传递地添加依赖项,因此您会获得具有相关版本的额外依赖项。

您可以根据需要尝试排除其他依赖项。 参考:Exclude all transitive dependencies of a single dependency

【讨论】:

    【解决方案2】:

    要验证是否是 jpa 依赖项再次拉入这些 jar,您可以在与 pom.xml 相同的目录中运行以下命令

    mvn dependency:tree -Dverbose
    

    这将显示依赖项来自何处的输出,只需搜索重复项。

    您可以将排除标记添加到可能会传递到这些库中的任何内容,例如:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <exclusions>
                <exclusion>
                    <groupId>*</groupId>
                    <artifactId>*</artifactId>
            </exclusion>
        </exclusions>    
    </dependency>
    

    Maven 3.2.1 开始,您可以使用通配符排除传递依赖。

    或者如果您不想使用 * 通配符,请明确说明 groupId 和 artifactId。

    【讨论】:

    • 这不起作用,它仍然会产生与问题中相同的屏幕截图。我们尝试对红色下划线的依赖项之一执行此操作,但没有任何反应。难倒我们的是 Maven 不应该将 Hystrix 依赖项导入到项目中。 Hystrix 不可能是 JPA 的传递依赖。
    • 你是否在 intellij 中对项目进行了重建?
    【解决方案3】:

    这不是真正的错误,而是警告。

    关于重复依赖项的警告。

    如果您添加 spring-boot-jpa 依赖项,您也会间接添加其他 spring-boot 依赖项。但是如果你严格添加spring-boot,spring-boot-jpa的间接依赖会被忽略,可能会解决不兼容问题。

    请删除该 spring-boot 以仅使用 spring-boot-jpa 树。

    【讨论】:

      猜你喜欢
      • 2019-08-18
      • 2020-07-01
      • 2011-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-07
      • 2016-01-16
      • 1970-01-01
      相关资源
      最近更新 更多