TimS
TimS

Reputation: 102

NoClassDefFoundError when Maven test-scoped dependency overrides transitive compile-scoped dependency

We have several deployables, and have extracted common code into several libraries. Deployable app A has a (default-scoped) Maven dependency on library B, and a test-scoped dependency on library C.

I made a change to library B, which used a method from, and so created a dependency on, library C. This caused app A to fail at run-time with NoClassDefFoundErrors. App A's unit tests all passed, of course, so we only found this in our staging environment. (Other apps either had no direct dependency on library C, or had a default-scoped dependency on it, and so continued to work.)

The obvious fix worked, of course: I edited A's pom.xml to make the dependency on C default scope.

My question is: Are we doing something wrong? Or is this how things are supposed to work? Is anyone aware of an automated way of detecting the potential problem at build-time and failing the build?

Upvotes: 3

Views: 520

Answers (1)

J Fabian Meier
J Fabian Meier

Reputation: 35805

You are not doing something wrong. Maven made a peculiar design choice here. See also

Maven: test vs. transitive compile, especially Tunakis comment on my question.

and

Maven dependency within dependency with different scope

My advice: Eliminate C from A's pom.xml. Only add test dependencies if these dependencies are not already on the dependency tree.

Upvotes: 1

Related Questions