Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare Maven dependency as test runtime only

What is the best way to declare a Maven dependency as only being used for the test runtime (but not test compilation) class path?

Specifically, I want slf4j-api (a logging facade) as a typical, compile-scope dependency, but I want slf4j-simple (the barebones implementation suitable for unit tests) only on the test runtime class path (it's not needed for test compilation). I've been doing this:

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-simple</artifactId>
  <scope>test</scope>
</dependency>

However, the downside of this is that dependency:analyze reports slf4j-simple as unused, presumably because it's not needed for compilation:

[WARNING] Unused declared dependencies found:
[WARNING]    org.slf4j:slf4j-simple:jar:1.7.7:test

I can't use a runtime dependency because I don't want that dependency transitively inherited (e.g. so downstream dependencies can use log4j, etc. instead). I tried runtime with optional=true, but that results in the same warning.

(Note that I could also set ignoreNonCompile for the dependency plugin, but that seems like a very blunt instrument that would hide other potential problems.)

like image 815
Trevor Robinson Avatar asked Dec 31 '14 22:12

Trevor Robinson


People also ask

What is Maven runtime dependency?

Maven dependency scope attribute is used to specify the visibility of a dependency, relative to the different lifecycle phases (build, test, runtime etc). Maven provides six scopes i.e. compile , provided , runtime , test , system , and import .

What is test dependency in Maven?

A test -scoped dependency is a dependency that is available on the classpath only during test compilation and test execution. If your project has war or ear packaging, a test -scoped dependency would not be included in the project's output archive.

What is dependencyManagement in POM xml?

It's like you said; dependencyManagement is used to pull all the dependency information into a common POM file, simplifying the references in the child POM file. It becomes useful when you have multiple attributes that you don't want to retype in under multiple children projects.

What is BOM Maven?

What Is Maven BOM? BOM stands for Bill Of Materials. A BOM is a special kind of POM that is used to control the versions of a project's dependencies and provide a central place to define and update those versions.


3 Answers

There is no scope that does exactly what you want here; test is the best available option.

A test-runtime scope has been requested before (Re: Need for a test-runtime scope?) and the suggested workaround is exactly the ignoreNonCompile configuration you've already discovered.

dependency:analyze already has some limitations ("some cases are not detected (constants, annotations with source-only retention, links in javadoc)"). You may have to accept that any test-scope dependencies that it warns against are false positives.

(You could split the definition of your tests into a separate module, which would have no slf4j implementation dependencies, then run them in another module. I don't think that would be worth it.)

like image 130
Joe Avatar answered Oct 07 '22 07:10

Joe


There is no concept of test-runtime in maven. The only real downside is the dependency analysis identifying these runtime test dependencies as unused. Since they are only test dependencies, however, this is pretty benign and cannot cause issues to other projects transitively dependent on this project.

like image 42
Brett Okken Avatar answered Oct 07 '22 07:10

Brett Okken


Since maven-dependency-plugin 2.10 (revision 1649454, Jan 2015), you can also add to the configuration a list of ignoredDependencies, ignoredUnusedDeclaredDependencies and ignoredUsedUndeclaredDependencies.

like image 4
Alessio Santacroce Avatar answered Oct 07 '22 08:10

Alessio Santacroce