Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean that artifact scope is not transitive?

Tags:

scope

maven

In this document (https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html) under dependency scope "test", it says test scope is not transitive. What does it mean?

  1. Does it mean if we include an artifact with scope as test in some pom, then its dependent artifacts will not be automatically downloaded/included in classpath?
  2. Or does it mean if we include an artifact in some pom, then its dependent artifacts with scope as test will not be downloaded/included in classpath?
  3. Or something else.

As far as I know first one is not true and second one is right. Can someone please tell me what exactly does it mean?

like image 926
Harsh Yadav Avatar asked Dec 17 '16 19:12

Harsh Yadav


People also ask

What is transitive in Maven?

Transitive Dependencies. Maven avoids the need to discover and specify the libraries that your own dependencies require by including transitive dependencies automatically. This feature is facilitated by reading the project files of your dependencies from the remote repositories specified.

How does maven avoid transitive dependencies?

Exclude the transitive dependencyOpen the dependency POM and find the transitive dependency you want to exclude. Copy groupId and artifactId . In your project POM, underneath your active dependency, enter exclusions and using code completion paste the copied info of the dependency you want to exclude.

Which of the below scopes does not limit the transitivity of a dependency?

It indicates the dependency to be replaced with the effective list of dependencies in the specified POM's section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.

What does scope test mean in Maven?

Test. We use this scope to indicate that dependency isn't required at standard runtime of the application but is used only for test purposes. Test dependencies aren't transitive and are only present for test and execution classpaths.


1 Answers

To be clear, this is the passage of the documentation:

test
This scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases. This scope is not transitive.

Those two sentences handle the cases that can rise during dependency resolution: declaring a dependency in the POM, and considering transitive dependencies of dependencies declared in the POM.

The first part means that having in your POM a dependency with scope test will result in that dependency being only available in the test classpath. Put another way, it applies when you have explicitly declared a dependency in the POM:

<dependency>
  <!-- GAV coordinates -->
  <scope>test</scope>
</dependency>

Trying to use it in main Java classes (under src/main/java) will result in a compilation error, and using it in test Java classes (under src/test/java) will work fine.

The second part applies to the dependencies of dependencies that are declared in the POM. It means that test scoped dependencies of the dependencies declared in the POM will be omitted. For example, suppose you are having a compile-time dependency on a library called A, and A itself has a test-scoped dependency on B; then B will be ignored in the dependency resolution, and will not end up on your classpath. The logic is that, A needs B to run its tests, but as a consumer of A, you don't need B to be able to use it. Those test-scoped dependencies will always be omitted, whatever the scope of the declared dependency you have (whether it is compile, runtime, or even test), which is why the test scope is called not transitive.

Put another way, it all depends what is meant by "some pom". When you're declaring a test scoped dependency in your POM, it will be available on the test classpath. All of its compile and runtime transitive dependencies will be available on the test classpath as well, because the compile and runtime scope are transitive and will be inherited with a scope of test. When the POM is not your own, the test scoped dependencies will be always omitted (therefore its dependencies will be omitted as well).

like image 54
Tunaki Avatar answered Sep 17 '22 21:09

Tunaki