Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude {test} scoped dependencies from build?

Tags:

maven

maven-3

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?

like image 872
Antoniossss Avatar asked Mar 24 '14 12:03

Antoniossss


People also ask

Is there a way to exclude Maven dependency globally?

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.

What is exclusions in POM XML?

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.

How do I delete a dependency in Maven?

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.


1 Answers

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:

  • Use a profile and rebind compiler:testCompile as well as surefire:test to non existing phases
  • put all your test dependencies in a separate profile

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.

like image 191
blackbuild Avatar answered Sep 21 '22 07:09

blackbuild