Maven dependency within dependency with different scope
java, maven-2
Solution
This feels really odd to me, and if it's "feature", I think it is a really dangerous one. Anyway, it's not a Maven bug and it's in the maven documentation here.
Regarding best practices on this issue, I haven't heard of any, but the safest way to proceed ought to be to entirely remove xstream from your pom, relying on the transitive dependency. Doing this will result in a build failure if the dependency to mylibrary is removed. This will act as a notification to you that you need to fix something. You won't silently loose required dependencies, and you won't silently have dependencies you no longer need.
On a side note, mvn dependency:analyze can be used to check for dependencies that are included but not used.
Problem
Say I have two Maven dependencies defined in a project like below. ``` <dependency> <groupId>com.thoughtworks.xstream</groupId> <artifactId>xstream</artifactId> <version>1.3.1</version> <scope>test</scope> </dependency> <dependency> <groupId>mycompany.library</groupId> <artifactId>mylibrary</artifactId> <version>1.0.1</version> <scope>compile</scope> </dependency> ``` Then, within mylibrary, I also have a dependency defined as below. ``` <dependency> <groupId>com.thoughtworks.xstream</groupId> <artifactId>xstream</artifactId> <version>1.3.1</version> <scope>compile</scope> </dependency> ``` When I package my project, I don't see xstream packaged within it. I think the project's xstream dependency scope, 'test' is overriding the mylibrary's xstream dependency scope, 'compile'. In this kind of situation, what's the best way to include the xstream for the whole project so the submodule can have access to it when packaged within the project? I've read Apache Maven website's explanation on Transitive dependencies, but I'm struggling to understand what it means, and also to find out the best practice in this situation.