Maven中mysql-connector-java的provided scope不生效问题求助
provided Scope Hey there, I know how frustrating it is when you've tried everything to keep a dependency out of your WAR but it keeps sneaking in—especially when it's causing Tomcat memory leaks on redeployment. Let's walk through targeted fixes for this issue:
Step 1: Hunt for Duplicate or Misconfigured Dependencies
First, run this precision dependency tree command to track down every instance of mysql-connector-java in your project (way more reliable than broad filtering):
mvn dependency:tree -Dverbose -Dincludes=mysql:mysql-connector-java
Scan the output for any entries where the scope isn’t provided—sometimes a duplicate declaration (in a module pom or inherited parent pom) can override your intended scope.
Step 2: Audit Your Maven War Plugin Setup
Old versions of the maven-war-plugin have bugs handling provided scopes, or you might have accidentally configured it to include excluded dependencies. Check your pom’s plugin section:
- Use a recent stable version of the plugin (3.3.2+ is recommended)
- Ensure no
<packagingIncludes>or<webResources>rules explicitly includemysql-connector-java*.jar - Add this configuration to force-exclude the jar if needed:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.4.0</version> <configuration> <packagingExcludes>WEB-INF/lib/mysql-connector-java*.jar</packagingExcludes> <warSourceExcludes>WEB-INF/lib/mysql-connector-java*.jar</warSourceExcludes> </configuration> </plugin>
Step 3: Validate Your Effective POM
Parent poms or inherited dependency management can silently override your scope settings. Generate your effective pom to see the final resolved dependencies:
mvn help:effective-pom
Search for mysql-connector-java in the output—if the scope isn’t provided, explicitly override it in your project’s pom with a <scope>provided</scope> declaration.
Step 4: Clean Your Maven Cache
Corrupted local repository artifacts can cause weird dependency resolution glitches. Delete the mysql directory from your local repo:
rm -rf ~/.m2/repository/mysql
Then run mvn clean install to re-download fresh, uncorrupted copies of the dependency.
Step 5: Temporary Tomcat Workaround (Immediate Relief)
While fixing the root packaging issue is ideal, you can mitigate the memory leak temporarily by telling Tomcat to skip scanning the connector jar. Add this line to your Tomcat’s conf/catalina.properties:
tomcat.util.scan.StandardJarScanFilter.jarsToSkip=mysql-connector-java*.jar
If you can share the full effective pom output or exact results from the precision dependency tree command, we can narrow this down even further!
内容的提问来源于stack exchange,提问作者Guilherme Gusman

