【问题标题】:Maven is picking dependency of old versionMaven 正在选择旧版本的依赖项
【发布时间】:2018-04-16 16:10:58
【问题描述】:

我正在尝试集成 Maven - Spring MVC - Rest ,但我面临的问题是 maven 没有选择“spring-web/aop/beans/context/core”(版本 4.3.12)依赖项。

maven 选择了 "spring-web/aop/beans/context/core" 2.5.6.SEC03 依赖,而不是 spring-web/aop/beans/context/core 4.3.12.RELEASE 。因为这个“org.springframework.web.bind.annotation.RequestMapping;”没有得到解决,因为它在 spring web 2.5.6.SEC03 中不存在

Maven 版本 -> apache-maven-3.5.2

Maven 环境变量

pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
<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>org.portal</groupId>
    <artifactId>HumanResourcePortal</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <warName>${project.artifactId}-${project.version}</warName>
                    <outputDirectory>${project.basedir}/target</outputDirectory>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <version>2.6.1</version>
                <configuration>
                    <filesets>
                        <fileset>
                            <directory>
                                /home/shivam/apache-tomcat-7.0.81/webapps/${project.artifactId}-${project.version}
                            </directory>
                        </fileset>
                        <fileset>
                            <directory>
                                /home/shivam/apache-tomcat-7.0.81/webapps
                            </directory>
                            <includes>
                                <include>
                                    ${project.artifactId}-${project.version}.{packaging}
                                </include>
                            </includes>
                       </fileset>
                    </filesets>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.0.2</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>/home/shivam/apache-tomcat-7.0.81/webapps</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${project.basedir}/target</directory>
                                    <includes>
                                        <include>${project.artifactId}-${project.version}.${packaging}</include>
                                        <include>${project.artifactId}-${project.version}</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.12.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.12.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.3.12.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.12.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.12.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.3.12.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.thoughtworks.xstream</groupId>
            <artifactId>xstream</artifactId>
            <version>1.4.9</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-bundle</artifactId>
            <version>1.8</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-core</artifactId>
            <version>1.8</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>1.8</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-client</artifactId>
            <version>1.8</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey.contribs</groupId>
            <artifactId>jersey-spring</artifactId>
            <version>1.8</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.11.Final</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.1.0.7.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.3.12.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.12.RELEASE</version>
        </dependency>
    </dependencies>
</project>

运行maven依赖树后

运行“mvn help:effectivePom”后

【问题讨论】:

  • mvn help:effectivePom 长什么样子?
  • @Makoto , 添加 mvn help:effectivePom 的输出
  • 应该是effective-pom,而不是effectivePom

标签: java spring maven spring-mvc


【解决方案1】:

很可能,过时的 Spring 版本来自 jersey-spring 瞬态依赖项。尝试使用最新版本之一。

更好的选择?

嗯,首先,您可以使用&lt;dependencyManagement&gt; 部分。它用于锁定项目中依赖项的版本。 &lt;dependencyManagement&gt; 可以处理任何工件。

其次,从 3.x 版本开始 Spring provides a Bill of Materials。 BOM 是 Maven 中的一个特殊概念,例如 &lt;dependencyManagement&gt;,但它允许通过一个 BOM 导入来锁定多个依赖项。为一些流行的库(如 Spring 或 Camel)准备和发布 BOM。

【讨论】:

  • 是的,那是因为 jersey spring 1.8 。现在我删除了那个依赖。
  • 你能建议我使用哪个版本的球衣弹簧吗?如 "mvnrepository.com/artifact/com.sun.jersey.contribs/…" 最新版本是 1.9,如果我使用它,它会再次开始选择版本为 3.0.0.RC3 的弹簧罐
  • 最新版的泽西其实是1.19.4:repo1.maven.org/maven2/com/sun/jersey/jersey-core/1.19.4。我不确定它依赖于哪个 Spring 版本。只需使用 dependencyManagement 强制一个您需要的版本,并祈祷 Jersey 可以使用它:)
  • 哦,对不起。没注意到 1.19.4 ..我在想 1.1.9.4
  • 再次使用 spring-jersey 1.19 jar 时,它正在选择 spring-jar 3.0.0.RC3。
【解决方案2】:

选择更接近项目根目录的依赖版本是 Maven 依赖查找的已知行为。假设你有以下依赖树:

Your Project
- library-A-1.0.jar
-- library-B-1.5.jar (dependency of library-A)
- library-B-1.2.jar

maven 将为您选择的库集将是 library-A-1.0.jarlibrary-B-1.2.jar(因为与距离为 2 的 library-B-1.5.jar 相比,此版本更接近距离 1 的项目根目录)。

解决此类问题的常用方法是

  1. Maven Dependency Plug-in分析你的依赖树
  2. 要么定义更接近项目根目录的所需库版本
  3. 或在包含的工件中排除不需要的依赖项
  4. 或在 pom.xml 文件中在相同距离时将依赖关系移到更高(将使用第一个)

【讨论】:

  • 添加了maven依赖树插件的输出。但也失败了
  • 现在我可以成功运行 maven 依赖树了。那是因为 jersey spring 1.8 。现在我删除了该依赖项,现在它正在加载 sping jar 4.3.12。 “但它无法找到 oracle ojdbc jars。”你能告诉我应该用哪一个吗? ojdbc6 或 ojdbc14 ...它们之间有什么区别
  • 你能建议我使用哪个版本的球衣弹簧吗?如 "mvnrepository.com/artifact/com.sun.jersey.contribs/…" 最新版本是 1.9,如果我使用它,它会再次开始挑选版本为 3.0.0.RC3 的弹簧罐
  • @shivam,它可能看起来像ojdbc14ojdbc6 的更新版本,你应该使用它,但它不是!看看this answer,它解释了疯狂的 Oracle 命名。并使用ojdbc6
猜你喜欢
  • 2023-04-01
  • 2011-08-27
  • 2012-12-15
  • 2010-12-23
  • 1970-01-01
  • 2017-07-17
  • 2020-03-29
  • 2016-09-18
  • 2020-07-26
相关资源
最近更新 更多