I have Java project build with maven that has some dependencies. One of such dependencies is another project (I have Eclipse workspace in mind) and it is only in test
scope.
<dependency>
<groupId>my.group.id</groupId>
<artifactId>artifactid</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>
I build my projects using following command inside project root dir
mvn clean package -Dmaven.test.skip=true
what I expected to happen it that maven would build project leaving test dependencies behind. However unresolved dependency exception is thrown (unresolved to my test
scoped dependency). As I ran build command with -X
option, I spoted following report in my project build plan:
[DEBUG] Dependencies (collect): []
[DEBUG] Dependencies (resolve): [compile, runtime, test]
[DEBUG] Repositories (dependencies): [central (http://repo.maven.apache.org/maven2, releases)]
[DEBUG] Repositories (plugins) : [central (http://repo.maven.apache.org/maven2, releases)]
[DEBUG] -----------------------------------------------------------------------
So despite skipping tests and even compiling them (mvn clean compile
runs fine) maven tries to resolve test dependencies.
How to exclude test dependencies from build plan dependency resolving step when skipping test executions?
Conclusion – By using the <scope>provided</scope> for a dependency you can globally exclude the dependency from getting packaged in the application lib instead it's loaded by the server classloader thus avoiding any LinkageError or ClassCastExceptions from arising due to jar conflicts.
Exclusions are set on a specific dependency in your POM, and are targeted at a specific groupId and artifactId. When you build your project, that artifact will not be added to your project's classpath by way of the dependency in which the exclusion was declared.
Open the pom file and click on dependency Hierarchy. Select the the jar you want to delete. Right click and click on Exclude Maven Artifact.
In short: you can't. At least not easily. maven.test.skip
does not prevent the surefire:test mojo from running, it only configures the plugin to do nothing. (same for compile:testcompile).
Both mojos need dependency resolving of scope test (part of the mojo definition).
So, the only options of preventing those dependencies from being resolved would be either:
However, both options are rather dirty.
So your best bet would be to install your dependent artifacts first (using mvn install
) and simply accept the way maven resolves dependencies.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With