Apache Maven: Dependency Scopes

In this article, we will learn and understand about different scopes available in Apache Maven which affects transitivity

Dependency Scopes

There are 6 (six) scopes available

  • compile
  • provided
  • runtime
  • test
  • system
  • import (Maven version 2.0.9 or higher)

These dependency scopes limit the transitivity dependencies of any project and also affect the classpath

 

Let us understand each scope in detail

Before diving into much deeper in the sea, we will get familiar with few terms like compile-classpath, test-classpath and runtime-classpath

  • compile-classpath –> dependencies will be available for main source files
  • test-classath –> dependencies will be there for test compilation & execution
  • runtime-classpath –> dependencies will be available in runtime environment and their executions

compile

  • This tells that dependency are needed for the compilation of main source files
  • Compile dependencies are available in all three classpaths mentioned above
  • These dependencies are propagated to dependent projects
  • Means transitive dependencies are included in the projects it is used and this can be cross-checked with project’s “WEB-INF\lib” folder
  • This is default scope, if nothing is specified in pom.xml

provided

  • This is similar to “compile” dependencies with only exception that, it’s not available in runtime-classpath
  • Which assumes that runtime environment like JDK or web container provides the required/dependent JARS for their executions after deployment
  • This is available only in compile-classpath and test-classpath
  • This is not transitive
  • So, once after building/packaging the project, we can neither find direct dependencies nor transitive dependencies in project’s “WEB-INF\lib” folder which are scoped to “provided

runtime

  • This dependency is not required for compilation, but very much required for their execution at runtime
  • Not exactly, but it is just opposite of “provided” scope which means dependency aren’t available in compile-classpath but available in runtime-classpath
  • This is available only in test-classpath and runtime-classpath
  • This is transitive in nature means transitive dependencies are packaged into project
  • We can cross-check at project’s “WEB-INF\lib” folder

test

  • This dependency available for test compilation and for their executions
  • Available in only test-classpath
  • This is not transitive
  • If we cross-check, then direct & their transitive dependencies aren’t available in project’s “WEB-INF\lib” folder
  • Note: This is not for normal use of the application

system

  • This is similar to “provided” scope but we have to make sure to provide the dependent JARS from local machine(system)
  • While executing maven commands like package or install, then for dependencies this will doesn’t look up to maven local repository instead we have to make sure to provide the required JARS from our local machine(system)
  • Transitivity doesn’t comes into picture, as we are manually making sure to provide the dependent and their transitive JARS from local machine(system)

import (Maven version 2.0.9 or higher)

  • This is useful in multi-module maven based project
  • We need to configure <dependencyManagement> in parent’s pom.xml

References

http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope

 

Read Also:

 

Happy Coding !!
Happy Learning !!