Transitivity of Maven test dependencies

Test dependency transitivity

Article directory

  • Transitivity of test dependencies
    • 1. Transitivity of dependencies
      • ① concept
      • ②Principles of delivery
    • 2. Use compile scope to depend on spring-core
    • 3. Verify that test and provided scopes cannot be passed
    • 4. Finally: thanks

1. Transitivity of dependencies

①Concept

A depends on B, and B depends on C. If A does not configure a dependency on C, can C be used directly in A?

The answer is: yes, because Maven supports transitive dependencies between dependencies.

②Principles of delivery

Under the premise that A depends on B and B depends on C, whether C can be passed to A depends on the scope of dependency used when B depends on C.

  • B depends on C using compile scope: can be transitive
  • When B depends on C, use the test or provided scope: it cannot be passed, so when such a jar package is needed, the dependency must be explicitly configured where it is needed.

Note: The compile dependency scope is not specified by default, the default is the compile dependency scope. That is to say, if the dependency scope is: compile, it can be omitted.

Operation: The background is in: the pro02-maven-web project depends on the configuration of the pro01-maven-java project

2. Use compile scope to depend on spring-core

Test method: let the pro01-maven-java project depend on spring-core

Specific operation: Edit pom.xml under the root directory of the pro01-maven-java project

<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>4.0.0.RELEASE</version>
    <!--The dependency scope of <scope>compile</scope> is not specified here, because the dependency is not specified by default, it is compile -->
</dependency>

Use the mvn dependency:tree command to see the effect:

mvn dependency:tree # View the dependency view of the current project in a tree structure

[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ pro01-maven-java ---
[INFO] com.rainbowSea.maven:pro01-maven-java:jar:1.0-SNAPSHOT
[INFO] + - junit:junit:jar:4.12:test
[INFO] | \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] \- org.springframework:spring-core:jar:4.0.0.RELEASE:compile
[INFO] \- commons-logging:commons-logging:jar:1.1.1:compile

You can also use the mvn dependency:tree command to view the effect in the Web project (note: need to re-install pro01-maven-java (because the pro01-maven-java project has a new update, and the new dependency (spring -core)) to the local warehouse, otherwise the pro02-maven-web project of the Web project cannot find the jar information that depends on pro01-maven-java):

As follows, we have not updated: when pro01-maven-java, check the dependency structure of pr02-maven-web

mvn dependency:tree

Here we re: install pro01-maven-java to the local warehouse: execute the following command

mvn clean install # Clear first, and then install locally, so as to ensure that the latest installation is the latest

Re-install and update pro01-maven-java locally, and then you can see the project dependency structure in pro02-maven-web.

[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ pro02-maven-web ---
[INFO] com.rainbowSea.maven:pro02-maven-web:war:1.0-SNAPSHOT
[INFO] + - junit:junit:jar:4.11:test
[INFO] | \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] + - javax.servlet:javax.servlet-api:jar:3.1.0:provided
[INFO] \- com.rainbowSea.maven:pro01-maven-java:jar:1.0-SNAPSHOT:compile
[INFO] \- org.springframework:spring-core:jar:4.0.0.RELEASE:compile
[INFO] \- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] ----------------------------------------------- -------------------------

3. Verify that test and provided scopes cannot be passed

As can be seen from the above example, pro01-maven-java depends on junit, but junit is not seen when viewing the dependency tree in the pro02-maven-web project.

To verify that the provided scope cannot be passed, you can add the servlet-api dependency to the pro01-maven-java project.

<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>3.1.0</version>
  <scope>provided</scope>
</dependency>

Also first update: pro01-maven-java, and reinstall it to the local warehouse.

mvn clean install # Clear first, and then install locally to ensure that the installation is the latest.

Then, check in pro02-maven-web:

mvn dependency:tree

The effect is still the same as before:

INFO] --------------------------------[ war ]---------- -----------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ pro02-maven-web ---
[INFO] com.rainbowSea.maven:pro02-maven-web:war:1.0-SNAPSHOT
[INFO] + - junit:junit:jar:4.11:test
[INFO] | \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] + - javax.servlet:javax.servlet-api:jar:3.1.0:provided
[INFO] \- com.rainbowSea.maven:pro01-maven-java:jar:1.0-SNAPSHOT:compile
[INFO] \- org.springframework:spring-core:jar:4.0.0.RELEASE:compile
[INFO] \- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] ----------------------------------------------- -------------------------

4. Finally: Thanks

This article refers to the following blogger’s sharing. Here again, we sincerely thank the bloggers for their enthusiastic sharing of their technology.

Thanks to the following bloggers for sharing

[1]: Weapon | Code Rework