Scope in Maven Dependencies

Scope in Maven Dependencies

I am very sure a lot of times this question might have come to your mind, what exactly the scope is in our Pom.xml file why to use and what is the purpose of this. In this blog, I will try my best to answer these questions.

As per official maven documentation, Scope Tag in maven dependency is used to use to limit the transitivity of dependency or in simpler terms to define the scope or phase in which that dependency will actually work.

The <Scope> the element can take six values in our pom.xml file:

ร˜  Compile 
ร˜  Provided
ร˜  Runtime
ร˜  Test
ร˜  System
ร˜  Import



Now Let’s look into more details for all these six Scopes.

Compile

This is the maven default scope. This dependency is needed for the purpose of build, test and runs the project.
Compile dependencies are available in all classpaths of a project. These dependencies are also transferred to dependent projects.
For e.g, we need the JAR for compiling and running the web application

Provided

Provided is quite similar to compile and is used during build and test the project. This is also required to run, but should not be exported, because the dependency will be provided at the runtime by JDK or a container.

e.g We need JAR for compiling the project, but at run time there is already a JAR provided by the environment so we don't need it to packaged again with our web application.



RunTime

This dependency is not required for build or compilation, but is required to test and run our project.

Test

Test are not needed to build and run the project. They are needed to compile and run the unit tests.

System

This is similar to provided and the only difference is system dependencies are not retrieved from remote repository. They are present under some project’s subdirectory and are referenced from there.

For e.g some dependencies we might have created by our own and wanted to use it in our project.

Import

This scope is only supported on a dependency of type pom in the dependencyManagement section. Maven 2.0.9 or later replaces pom with dependencymanagement Section.





SHARE THIS

Author:

My Name is Ankur Jain and I am currently working as Automation Test Architect.I am ISTQB Certified Test Manager,Certified UI Path RPA Developer as well as Certified Scrum Master with total 12 years of working experience with lot of big banking clients around the globe.I love to Design Automation Testing Frameworks with Selenium,Appium,Protractor,Cucumber,Rest-Assured, Katalon Studio and currently exploring lot in Dev-OPS as well. I am currently staying in Mumbai, Maharashtra. Please Connect with me through Contact Us page of this website.

Previous Post
Next Post