Tuesday, 9 July 2019

Maven: Dependency Scopes


In any project, you may not require all the dependencies throughout its life cycle.

For example, you no need to ship junit library in the final artifact. You may require some libraries only at compile time, some libraries may be provided by the run time environment (For ex: servlet apis can be provided by application server at run time).
 By considering all the above requirements, maven come up with 5 dependency scopes.
a.   Compile
b.    Provided
c.     Runtime
d.    Test and
e.    system
f.   import

compile: It is the default scope. If the dependency do not mention the scope, it is compile by default. All the compile scope dependencies will be available in class path, and they are packaged while building artifacts.

provided: You should use this scope, when the environment provides the dependency at run time. For example, servlet apis will be provided by tomcat container at run time. provided dependencies are available on the compilation class path, but not packaged in final artifact.

runtime: These dependencies are required at run time, but not required at compile time. This may typically be dynamically loaded code, such as JDBC drivers, which are not directly referenced in the program code.

test: These dependencies are required in testing phase. Not packaged in final artifact.

system: If any dependencies are available in local system, you need to use this dependency and provide the path to the jar file in the system. If you declare the scope to be system, you must also provide the systemPath element.
 
import: import the dependencies of a pom. This is used to standardize the dependencies versions across your projects.
Example
<dependency>
      <groupId>com.google.cloud</groupId>
      <artifactId>spring-cloud-gcp-dependencies</artifactId>
      <version>2.0.7</version>
      <type>pom</type>
      <scope>import</scope>
</dependency>



Previous                                                    Next                                                    Home

No comments:

Post a Comment