Maven Dependency Scopes
Contents
Maven dependency scopes define the availability of dependencies in different stages of the project lifecycle and configurations. Common scopes include compile, provided, runtime, test, import, and system.
Introduction to Dependency Scopes
- COMPILE
- Description: This is the default scope. Dependencies with this scope are available in the classpath for compilation, testing, and execution of the project.
- Typical Use: Libraries needed in the compile classpath of the project, such as libraries used to write and compile application code.
- PROVIDED
- Description: Dependencies with this scope are available in the compile and test classpaths, but not at runtime. They are typically provided by the JDK or the container at runtime.
- Typical Use: For example, Servlet API and JSP API dependencies, which are needed during compilation and testing, but are provided by the container (e.g., Tomcat) at runtime.
- RUNTIME
- Description: Dependencies with this scope are needed at runtime and testing, but not for compiling the main code.
- Typical Use: For example, JDBC drivers, which are not needed during compilation but are necessary at runtime.
- TEST
- Description: Dependencies with this scope are only used for compiling and running tests.
- Typical Use: For example, JUnit dependencies, which are only needed for writing and executing tests.
- IMPORT
- Description: Used only in the dependencyManagement section. This scope is not used to limit the use of dependencies but to import the dependencyManagement section of other projects.
- Typical Use: In a multi-module project, the import scope ensures that all modules use the same version of dependencies.
- SYSTEM
- Description: Similar to provided, but you must provide and specify the path to the JAR file manually. Dependencies with this scope are not resolved from the Maven repository.
- Typical Use: When dependencies are not available in the Maven repository and you do not intend to deploy them to any repository, such as certain proprietary or restricted JARs.
In summary, each scope defines the visibility and usage of dependencies at different stages of the project lifecycle. Choosing the appropriate scope can help you better manage your project dependencies, ensuring that specific dependencies are only introduced when needed, thereby reducing the size and complexity of the project.