本文介绍了如何使maven“从属"“提供"在第三方的依赖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我以Web模数运行mvn tomcat:run时,出现此错误.

I am getting this error, when I run mvn tomcat:run in my web modulo.

SEVERE: Servlet /web threw load() exception
java.lang.ClassCastException: org.springframework.web.servlet.DispatcherServlet cannot be cast to javax.servlet.Servlet

当我将依赖项添加到我拥有的其他模数时,就会发生问题,特别是因为其他模数包含com.google.gdata:core依赖项.我运行mvn dependency:tree,我发现此google依赖项在其依赖项树中已有servlet-api,因此我认为这是问题所在.但是我不知道如何解决它.

The problem happens when I add a dependency to one other modulo I have, specifically because that other modulo contains com.google.gdata:core dependency. I ran mvn dependency:tree I saw that this google dependency has servlet-api down its dependency tree, and so I think this is the problem. But I dont know how to fix it.

|  \- com.google.gdata:core:jar:1.47.1:compile
|     +- com.google.guava:guava:jar:13.0.1:compile
|     +- com.google.oauth-client:google-oauth-client-jetty:jar:1.11.0-beta:compile
|     |  +- com.google.oauth-client:google-oauth-client-java6:jar:1.11.0-beta:compile
|     |  |  \- com.google.oauth-client:google-oauth-client:jar:1.11.0-beta:compile
|     |  |     \- com.google.http-client:google-http-client:jar:1.11.0-beta:compile
|     |  |        +- org.apache.httpcomponents:httpclient:jar:4.0.3:compile
|     |  |        |  \- org.apache.httpcomponents:httpcore:jar:4.0.1:compile
|     |  |        \- xpp3:xpp3:jar:1.1.4c:compile
|     |  \- org.mortbay.jetty:jetty:jar:6.1.26:compile
|     |     +- org.mortbay.jetty:jetty-util:jar:6.1.26:compile
|     |     \- org.mortbay.jetty:servlet-api:jar:2.5-20081211:compile
|     +- com.google.code.findbugs:jsr305:jar:1.3.7:compile
|     \- javax.mail:mail:jar:1.4:compile
|        \- javax.activation:activation:jar:1.1:compile

answer 建议将servlet-api依赖项设为provided,但是如何在我不拥有的依赖项中做到这一点?

This answer suggests making servlet-api dependency provided, but how can do this inside a dependency I do not own?

推荐答案

您不能更改第三方依赖项的POM.但是您可以排除其依赖性.

You cannot change POM of a 3rd party dependency. But you can exclude its dependencies.

<dependency>
  <groupId>.....</groupId>
  <artifactId>.....</artifactId>
  <version>.....</version>
  <scope>compile</scope>
  <exclusions>
    <exclusion>
      <groupId>org.mortbay.jetty</groupId>
      <artifactId>servlet-api</artifactId>
    </exclusion>
  </exclusions> 
</dependency>

重要:

  1. 在正确的<dependency>中使用<exclusions>.否则它将无效.
  2. <exclusions>适用于<dependency>的整个子树,包括其所有嵌套依赖项.只需在您的POM中找到导致不想要的jar的顶层<dependency>,然后在其中使用<exclusions>.
  3. 相同的jar可能来自多个依赖项.在一个位置排除它之后,刷新依赖关系树,并检查是否不需要的jar通过其他依赖关系来了.如果是,则在其他地方也将其排除.
  1. Use <exclusions> in the correct <dependency>. Otherwise it will have no effect.
  2. <exclusions> works for the whole sub-tree of <dependency>, including all its nested dependencies. Just find the top level <dependency> in your POM that brings undesired jar and use <exclusions> there.
  3. The same undesired jar may come via multiple dependencies. After you excluded it in one place, refresh the dependency tree, and check if undesired jar comes via some other dependency. If yes, then exclude it also on other places.

这篇关于如何使maven“从属"“提供"在第三方的依赖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 06:01