簡體   English   中英

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

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

當我運行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

當我將依賴項添加到其他模數時會發生問題,特別是因為其他模數包含com.google.gdata:core依賴項。 我運行mvn dependency:tree我看到這個谷歌依賴關系有servlet-api它的依賴樹,所以我認為這是問題所在。 但我不知道如何解決它。

|  \- 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

這個答案建議provided servlet-api依賴,但是如何在我不擁有的依賴項中做到這一點?

您無法更改第三方依賴關系的POM。 但您可以排除其依賴項

<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> <dependency> 否則它將無效。
  2. <exclusions>適用於<dependency>的整個子樹,包括其所有嵌套依賴項。 只需在POM中找到頂級<dependency> ,它會帶來不需要的jar並在那里使用<exclusions>
  3. 同一個不受歡迎的jar可能來自多個依賴項。 在一個地方將其排除后,刷新依賴關系樹,並檢查不需要的jar是否來自其他依賴項。 如果是,那么也將其排除在其他地方。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM