Dependency Management

Dependency Management 

In Applications, Dependencies are the libraries [Collection of JARs] which are required to compile, test and run our applications. 

In General, in application development, we will download the required libraries from the internet and we will store them in the application directory structure. 

The main Advantage of MAVEN in applications development is that not to store any Dependent JAR files in Project Directory Structure by downloading them explicitly, MAVEN has given flexibility to the developers like to specify dependent JAR files names in pom file, where MAVEN will search for them in the repositories and MAVEN will load them into the project directory structure automatically. 

If we need any Library in MAVEN based applications then we have to declare them in pom file like below.

<dependencies>
   <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>3.5.6-Final</version>
      <scope>provided</scope>
   </dependency>
</dependencies>

If we provide the dependency like above then MAVEN will search for the hibernate library with the name like

http://repo1.maven.org/maven2/org/hibernate/hibernate-core/3.5.6-Final/

MAVEN is following “Transitive Dependencies Mechanism”, that is, if our dependencies are required any other libraries then MAVEN will get them automatically without loading them explicitly by the developers.

Dependency Scopes

In Applications, some dependencies are required to all phases of the project life cycle like compile, test, run,… and some other required only some of phases of the project life cycle. In order to limit the dependencies for the life cycle phases we will use Dependency Scopes.

There are 6 scopes available in MAVEN

  1. Compile
  2. Provided
  3. Runtime
  4. Test
  5. System
  6. Import
1. Compile

It is the default scope in MAVEN . This scope will make the dependencies to avail all phases like compile, test, run, etc.

<dependencies>
   <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>3.5.6-Final</version>
      <scope>compile</scope>
   </dependency>
</dependencies>

Note

In general, hibernate-core library is required for all phases of the application.

2. Provided

This scope will make the dependency libraries to avail up to compilation and and up to testing, not for run time, because, at run time, JDKs or Containers will provide the required dependencies at run time.

In web applications, Servlet API is required explicitly to compile and test the project, but, Servlet API is provided by the container at run time automatically, so that, they are not required to be exported at run time.

<dependencies>
   <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>3.0.1</version>
      <scope>provided</scope>
   </dependency>
</dependencies>
3. Runtime

This scope indicates that the dependency is not required for compilation, but is for execution. It is in the run time and test class paths, but not the compile class path.

<dependencies>
   <dependency>
      <groupId>com.thoughtworks.xstream</groupId>
      <artifactId>xstream</artifactId>
      <version>1.4.4</version>
      <scope>runtime</scope>
   </dependency>
</dependencies>
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. This scope is not transitive.

<dependencies>
   <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
   </dependency>
</dependencies>
5. System

Dependencies with system are similar to ones with scope provided. The only difference is system dependencies are not retrieved from remote repository. They are present under project’s sub directory and are referred from there.

<dependencies>
   <dependency>
      <groupId>Explicit_Dependency</groupId>
      <artifactId>Explicit_Dependency</artifactId>
      <version>4.12</version>
      <scope>system</scope>
      <systemPath>apps\app.war\WEB-INF\lib\Explicit_Dependency.jar</systemPath>
   </dependency>
</dependencies>
6. Import

It is available in Maven 2.0.9 or later. Import scope is only supported on a dependency of type pom in the dependencyManagement section. It indicates the dependency to be replaced with the effective list of dependencies in the specified POM’s dependencyManagement section.

<dependencyManagement>
   <dependencies>
      <dependency>
         <groupId>other.pom.group.id</groupId>
         <artifactId>other-pom-artifact-id</artifactId>
         <version>SNAPSHOT</version>
         <scope>import</scope>
         <type>pom</type>
      </dependency>
   </dependencies>
</dependencyManagement>
Dependency Management


Scroll to top