Michael Ekstrand Michael Ekstrand - 9 months ago 34
Java Question

How can I have a Maven dependency on the runtime classpath but not the test classpath?

I have a case where I want a dependency on the runtime classpath but not the test classpath. The dependency in question is Logback, an SLF4J implementation. In runtime, I want my code to (optionally) depend on logback so that it has a logging infrastructure available. At test time, however, I want to use the

implementation to black-hole the log output. With
as a runtime dependency and
as a test dependency, I get a multiple implementation warning from SLF4J when running my tests. I do not see a way to exclude
from the test classpath.

I do not want to split my tests into a separate package if it can be avoided.



I've finally found a real solution to this. Since version 2.6 of the Maven Surefire plugin, there is now a classpathDependencyExcludes configuration element that allows specific dependencies to be excluded from the classpath. This therefore works: