0

I understand that ...

  1. in Java, classpaths are environment variables that JVM needs during compilation to locate used classes, packages, and other resources
  2. in Maven, phases are processes that are constituents of Maven's build lifecycles
  3. in Maven, a dependency’s dependency is referred to as a transitive dependency
  4. in Maven, a dependency's scope "refers to the classpath of the task at hand and sets the dependency’s transitivity"

I don't really understand the meaning of the third statement. Is it that the scope defines the classpaths in which the dependency is available?

More importantly, ...

  1. how do I relate a dependency's scope (classpaths) to its transitivity?
  2. how do I relate a dependency's scope (classpaths) to the project's phases and their execution?
Omar Shazly
  • 93
  • 3
  • 10

1 Answers1

0

First in Java an environment variable CLASSPATH can be defined but you can also use the command line option java -cp .. instead.

In general you have two scopes compile and test. The compile scope is the one used by default if no scope is defined. That usually defines the dependencies which are needed to build your parts (compiling!) for production code (src/main/java).

The test scope defines the dependencies which are only used for running/compiling your tests.

This means while building with Maven there are two different classpaths one for production code and one for testing. If you build via mvn verify -X and take a deep look into the debugging output you can see those different parts something like this:

[DEBUG] test classpath:  /Users/khm/ws-git-soebes/katas/fraction/target/test-classes
/Users/khm/ws-git-soebes/katas/fraction/target/classes  
/Users/khm/.m2/repository/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar  
/Users/khm/.m2/repository/org/junit/jupiter/junit-jupiter-api/5.8.2/junit-jupiter-api-5.8.2.jar  

The first entry contains the target/test-classes that means everything what is in your src/test/java directory is first on your test classpath. Afterwards you will see target/class which contains the production code (src/main/java) and afterwards you see apiguardian-api-1.1.2.jar which is compile scope dependency and after that you will see all test scope dependencies.

You can see also the for the compiling of the production code the output looks like this:

[DEBUG] Classpath:
[DEBUG]  /Users/khm/ws-git-soebes/katas/fraction/target/classes
[DEBUG]  /Users/khm/.m2/repository/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar
[DEBUG] Source roots:
[DEBUG]  /Users/khm/ws-git-soebes/katas/fraction/src/main/java
[DEBUG]  /Users/khm/ws-git-soebes/katas/fraction/target/generated-sources/annotations
[DEBUG] Command line options:
[DEBUG] -d /Users/khm/ws-git-soebes/katas/fraction/target/classes -classpath /Users/khm/ws-git-soebes/katas/fraction/target/classes:/Users/khm/.m2/repository/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar: -sourcepath /Users/khm/ws-git-soebes/katas/fraction/src/main/java:/Users/khm/ws-git-soebes/katas/fraction/target/generated-sources/annotations: -s /Users/khm/ws-git-soebes/katas/fraction/target/generated-sources/annotations -g -nowarn --release 11 -encoding UTF-8 -Xlint:deprecation -Xlint:unchecked

This shows target/classes which contains the compiled class of your own parts from src/main/java and and entry apiguardian-api-1.1.2.jar which is a dependency scope: compile (apiguardian-api-1.1.2.jar).

So the scope defines where a dependency can be seen (or more accurate to be used).

how do I relate a dependency's scope (classpaths) to its transitivity?

A compile scope dependency of your own project would be propageted to another project which is using your project. A test scope dependency in contradiction is not propagated to other projects.

More detailed in pom explanations

how do I relate a dependency's scope (classpaths) to the project's phases and their execution?

Usually a compile scope is related to the compile phase (+testing of course) and the test scope is related to the test phase (unit- testing via maven-surefire-plugin or integration testing via maven-failsafe-plugin).

khmarbaise
  • 92,914
  • 28
  • 189
  • 235