如何在Eclipse中移除Maven依赖的旧版servlet-api jar包?
Got it, let's break this down and fix your issue step by step.
First, the root cause is clear:
- The
getPart()method was added in Servlet 3.0, so your code needs Servlet API 3.0+ to compile and run. - The unwanted
servlet-api:2.5jar is coming from transitive dependencies—one of the libraries you’ve added in yourpom.xmlis pulling in this older version automatically, without you explicitly declaring it.
Step 1: Find which dependency is bringing in servlet-api 2.5
Run this Maven command in your project directory to generate a full dependency tree. This will show exactly where the old Servlet API is coming from:
mvn dependency:tree
Look for a line like this in the output:
[INFO] +- com.google.appengine.tools:appengine-gcs-client:jar:0.8:compile [INFO] | +- javax.servlet:servlet-api:jar:2.5:compile
In your case, it’s almost certainly appengine-gcs-client (a common culprit for App Engine projects), but the command will confirm this for sure.
Step 2: Fix your Servlet API dependency and exclude the old version
First, update your explicit javax.servlet-api dependency to mark it as provided. App Engine’s runtime environment already provides this jar, so you don’t need to package it into your WAR file—this also prevents conflicts with the runtime’s version:
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> <!-- Add this line --> </dependency>
Next, add an <exclusions> block to the dependency that’s pulling in servlet-api:2.5. For appengine-gcs-client, modify that dependency like this:
<dependency> <groupId>com.google.appengine.tools</groupId> <artifactId>appengine-gcs-client</artifactId> <version>0.8</version> <exclusions> <!-- Exclude the old servlet-api transitive dependency --> <exclusion> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> </exclusion> </exclusions> </dependency>
Step 3: Verify and rebuild
After making these changes, run this command to clean old build artifacts and pull in the correct dependencies:
mvn clean install
Your getPart() method should now resolve correctly, as the project will only use the Servlet API 3.1.0 you specified.
Bonus Tip
If you’re still unsure which dependency is causing the issue, you can use your IDE’s Maven dependency viewer (like IntelliJ’s "Maven Projects" tab or Eclipse’s "Dependency Hierarchy" view) to find the old servlet-api jar and trace its parent dependency visually.
内容的提问来源于stack exchange,提问作者JSmith

