Maven Dependency Scopes

Dependency scope is used to limit the transitivity of a dependency, and also to affect the classpath used for various build tasks. Maven has 6 default dependency scopes

1) Compile: This is the default scope when no scope is provided

Example:

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.141.59</version>
</dependency>

2) Provided: It is used to mark dependencies that should be provided at runtime by JDK.

Example:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>provided</scope>
</dependency>

3) Runtime: This scope indicates that the dependency is not required for compilation, but is for execution.

Example: 

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.19</version>
</dependency>

4) Test: This scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases.

Example:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13</version>
    <scope>test</scope>
</dependency>

5) System: It requires us to directly point to specific jar on the system. The artifact is always available and is not looked up in a repository.

Example: 

<dependency>
     <groupId>htmlunit-core</groupId>
     <artifactId>htmlunit-core</artifactId>
     <scope>system</scope>
     <version>1.0</version>
     <systemPath>${basedir}/src/main/lib/htmlunit-core-js-2.13.jar</systemPath>
</dependency>

6) Import: It indicates that this dependency should be replaced with all effective dependencies declared in it’s POM.

Example:

 <dependency>
      <groupId>com.qascript</groupId>
      <artifactId>sample</artifactId>
      <version>1.0-Snapshot</version>
      <type>pom</type>
      <scope>import</scope>
 </dependency>

 

Share

Bijan Patel

Founder & Creator of QAScript | 12+ years of IT Experience | Full Stack Automation Engineer | Blogger | Trainer

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!