user149408
user149408

Reputation: 5881

Eclipse ignoring Maven test-scoped dependencies

I have a Maven project which uses SLF4J. For the purpose of running it from Eclipse, I want log output to go to the console, so I have configured dependencies as follows:

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

That is, I am pulling in slf4j-simple with a test scope.

The project is a library. In its test folder there is a class with a main method which runs some tests and diagnostic functions. When I run it that way from Eclipse, SLF4J logs to System.err as expected.

When I use the library project in another (whose main class is in the project’s src folder), SLF4J complains with the following error:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

I have tried adding the same dependencies to the parent project, to no avail. However, if I change the scope for slf4j-simple to compile or runtime in the parent project, logging works as expected.

What is happening here? Does this behavior have anything to do with the source tree in which the main class resides (test implying I am testing, thus test-scoped dependencies are pulled in, src implies regular runtime and test-scoped dependencies are not available)? If so, how do I tell my toolchain that I want slf4j-simple when running in Eclipse, but some other backend when building a JAR for deployment?

Upvotes: 1

Views: 678

Answers (1)

user149408
user149408

Reputation: 5881

Whether Eclipse uses test or runtime dependencies is governed by a project setting. To change it, go to Project > Properties > Java Build Path > Source. Contains test sources (a child of your project) is the setting you are looking for. If set to Yes, Eclipse will use test-scoped dependencies, else it will not.

Yes seems to be the default for a project which has code in the test tree, No for all others.

Change the setting as needed, apply, close and run—Eclipse will give you the desired set of dependencies.

There may be other relevant settings I have overlooked—feel free to drop a comment in this case.

Upvotes: 1

Related Questions