In this article, we will learn and understand about different scopes available in Apache Maven which affects transitivity
1. 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
1.1 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
1.2 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”
1.3 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
1.4 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
1.5 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)
1.6 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
Useful Eclipse IDE shortcuts :
- Eclipse IDE – How to show line numbers ?
- Eclipse IDE – How to GO TO any line number directly ?
- Eclipse IDE – How to remove unused imports ?
- Eclipse IDE – How to clean project ?
- Eclipse IDE – How to build Java project automatically ?
- Eclipse IDE – How to comment and un-comment line & block ?
- Eclipse IDE – How to generate constructor using fields ?
- Eclipse IDE – How to generate getters and setters ?
- Eclipse IDE – How to search files ?
- Eclipse IDE – How to locate methods in Java file ?
- Eclipse IDE – How to open editor using CTRL + E ?
- Eclipse IDE – Java compiler compliance level issue
Related Articles:
- Apache Maven – Introduction
- Apache Maven – Install on Windows 7 OS
- Apache Maven – Settings.xml explanation
- Apache Maven – Proxy setting explanation
- Apache Maven – pom.xml explanation
- Apache Maven – Plugins explanation
- Apache Maven – Changing default Maven repository location in Windows 7 OS
- Apache Maven – Local, Central and Remote Repositories
- Apache Maven – Installing custom library into local repository
- Apache Maven – Transitive dependencies explanation
- Apache Maven – Exclusion of Transitive dependencies
- Apache Maven – Dependency Scopes
- Apache Maven – Skipping unit test using surefire plugin
- Apache Maven – Exclusions and Inclusions of unit test
- Apache Maven – offline execution
- Apache Maven – Co-ordinates explained
- Eclipse + Maven – Integration
- Eclipse + Maven – How to import Maven project with pom.xml ?
- Eclipse + Maven – Setting M2_REPO classpath variable in IDE
- Eclipse + Maven – M2_REPO is Non Modifiable
- Eclipse + Maven – Creating and exploring projects using archetypes
- Eclipse + Maven – Converting Web project to Maven project
- Eclipse + Maven – mvn eclipse:eclipse command
- Eclipse + Maven – Plugin execution not covered by lifecycle configuration
References:
Happy Coding !!
Happy Learning !!