2013-09-29 146 views
2

當我運行mvn tomcat:run時,出現此錯誤。如何使第三方依賴項中的「提供」maven子依賴項?

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 

answer建議作出servlet-api依賴provided,但如何能做到這一點的依賴我沒有自己裏面?

回答

2

您無法更改第三方依賴關係的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. 使用<exclusions>在正確<dependency>。否則它將不起作用。
  2. <exclusions>適用於<dependency>的整個子樹,包括其所有嵌套的依賴關係。只需在您的POM中找到頂級<dependency>,這會帶來不希望的瓶子並在那裏使用<exclusions>
  3. 相同的不需要的jar可能通過多個依賴關係來實現。將它排除在一個地方之後,刷新依賴關係樹,並檢查是否有不需要的jar通過其他依賴關係來。如果是,則將其排除在其他地方。