Search code examples
javamavengradlemaven-2maven-3

What scope can be used to make a dependency non-transitive by default?


If I want project A to compile and tests to run but when I place it as a dependency into project B then project A's dependencies should not be available to project B.

For example:

  1. Add org.example.foo as a dependency into project A (not B)

  2. Add project A as a dependency inside project B

  3. Add this statement in a class inside project B: import org.example.foo.*

  4. You should get a compilation error on this line: import org.example.foo.*


Solution

  • You should specifiy the dependencies in project "A" with <scope>provided</scope>. This way the respective dependency is used for compilation & testing, but is not transitively used in project "B".

    Here you find the different scopes on maven dependencies: https://maven.apache.org/pom.html#Dependencies