【问题标题】:Tomcat loaded the lower version spring libraries when debug in Intelli j ideaTomcat在Intellij IDEA中调试时加载了低版本的spring库
【发布时间】:2019-06-03 06:50:10
【问题描述】:

我有一个用目标war构建的maven spring web(3.2.*)项目,我想用tomcat在intellij idea中调试它(web:war exploded artifact),但是web应用程序总是无法加载,

[ERROR]..|上下文初始化失败 java.lang.NoSuchMethodError: org.springframework.web.context.ConfigurableWebApplicationContext.getEnvironment()Lorg/springframework/core/env/ConfigurableEnvironment; 在 org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:404)

因为在某些 pom 依赖项中,它们引用了较低版本的 spring(3.0.7),我可以使用 maven-war-plugin(2.3):packagingExcludes 在 ware 中排除它们,但是如何在 web:war 中排除它们爆炸.

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.3</version>
    <configuration>
        <packagingExcludes>
            WEB-INF/lib/org.springframework.core-3.0.7.RELEASE.jar,
            // others springframework jars
        </packagingExcludes>
    </configuration>
</plugin>

【问题讨论】:

    标签: spring maven tomcat intellij-idea war


    【解决方案1】:

    Maven 解决依赖冲突的方式并不容易理解。 它被称为nearest-wins strategy,这可能会导致使用旧版本。

    如果您没有明确定义依赖版本,请尝试将Maven Dependency ManagementMaven Enforcer Plugin 结合使用来固定版本并使构建失败。

    简单的方法是analyse the dependency graph with

    mvn dependency:tree
    

    检查哪个依赖项依赖于旧的 spring 库,并排除 @alexandar-petrov 已经在他的答案中发布的那些。

    从您正在构建的工件中排除依赖项不会对 Intellij 或任何其他 IDE 将使用的类路径产生任何影响。

    【讨论】:

      【解决方案2】:

      它不是你应该排除与 IDE 相关的依赖关系的插件,它是依赖关系图。使用 mvn dependency:tree 查看哪个模块,依赖包含了旧的 spring 版本。当您弄清楚时,将其从构建中排除。

      这是一个如何使用直接来自 maven documentation 的依赖排除的示例。

      <dependency>
            <groupId>sample.ProjectA</groupId>
            <artifactId>Project-A</artifactId>
            <version>1.0</version>
            <scope>compile</scope>
            <exclusions>
              <exclusion>  <!-- declare the exclusion here -->
                <groupId>sample.ProjectB</groupId>
                <artifactId>Project-B</artifactId>
              </exclusion>
            </exclusions> 
          </dependency>
      

      【讨论】:

      • 我用spring bom管理依赖,没看到和maven helper冲突,但是老版本spring jars下载复制到WEB-INF/lib文件夹
      猜你喜欢
      • 2014-03-29
      • 2013-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多