Dependency Scopes
This lesson explains the dependency scope attribute of the dependency element and how it affects the various classpaths.
In this lesson, we’ll work with an example consisting of three related projects with the first one dependent on the second, and the second one dependent on the third.
When a dependency is specified it is also accompanied by a scope. The scope element controls two aspects: first, if a dependency should be included with the application packaging, and second, which classpath a dependency should be included in. A classpath is the location of the classes and packages that a project is dependent on. For instance, in our three-project example. Project A depends on Project B. When compiling Project A, we need to inform the compiler of where to find the artifacts of Project B so that they can be used to compile Project A. If you were to use the Java compiler javac
to compile Project A on the command line, you would specify the classpath as a command line argument using -cp
or -classpath
. The scope element determines if a particular dependency is included in the classpath for a particular build task, e.g., when the project is being compiled or tested.
These scopes are discussed below:
Get hands-on with 1200+ tech skills courses.