简体   繁体   English

如何在第三方依赖中“提供”maven子依赖?

[英]How to make a maven sub-dependency “provided” in 3rd party dependency?

I am getting this error, when I run mvn tomcat:run in my web modulo. 当我运行mvn tomcat:run时,我收到此错误mvn tomcat:run在我的web模块中mvn tomcat:run

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

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. 当我将依赖项添加到其他模数时会发生问题,特别是因为其他模数包含com.google.gdata:core依赖项。 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. 我运行mvn dependency:tree我看到这个谷歌依赖关系有servlet-api它的依赖树,所以我认为这是问题所在。 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

This answer suggests making servlet-api dependency provided , but how can do this inside a dependency I do not own? 这个答案建议provided servlet-api依赖,但是如何在我不拥有的依赖项中做到这一点?

You cannot change POM of a 3rd party dependency. 您无法更改第三方依赖关系的POM。 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>

Important: 重要:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM