问题
How do I exclude a dependency in provided scope when running in Maven test scope? I have an unusual use case where I need to exclude a particular provided implementation and replace it with another in the test cases. It seems that Maven tests always include other scopes as well but in my case I want to make some exception. How do I do this?
回答1:
There are situations where you need to run your tests in a different module. That may be what you need here. It allows your tests to use different dependencies, properties and version of Java etc.
回答2:
I think, the simplest way to do this is to create 2 profiles for each dependency you want to use. You can activateByDefault the one with provided scope.
It may look like this :
<profiles>
<profile>
<id>providedDependency</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>[provided dependency information]</dependency>
</dependencies>
</profile>
<profile>
<id>testDependency</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<dependencies>
<dependency>[test dependency information]</dependency>
</dependencies>
</profile>
</profiles>
When passing into test mode, unactivate the provided dependency and activate the other
mvn test -P!providedDependency,testDependency
来源:https://stackoverflow.com/questions/12314397/how-do-i-exclude-a-dependency-in-provided-scope-when-running-in-maven-test-scope