Dependency Management In Java

Dependency Management In Java

Maven dependency management in the Java programming language is a detailed process or mechanism for declaring, grouping, organizing, and controlling essential external libraries for a Java software source code project.

Dependency Management In Java

For example, instead of manually downloading project files such as JUnit or Mockito testing framework JARs into a Java program, Java users declare them in the pom.xml file, and the Maven dependency management framework automatically downloads and manages them in their project.

Declaring a Maven dependency management framework in a Java program.

Example of Maven dependency management.

<dependencies>

<dependency>

<groupId>org.junit.jupiter</groupId>

<artifactId>junit-jupiter</artifactId>

<version>5.12.2</version>

<scope>test</scope>

</dependency>

</dependencies>

Maven dependency management has some essential fields.

  • groupId — This identifies your Maven dependency organization/project.
  • artifactId — This identifies your Maven dependency library.
  • version — This indicates the current version of your Maven dependency and which dependency version to use.
  • scope — This determines the scope of your Maven dependency and where it is available within the dependency.

Java Dependency Scopes all Elements Details.

Common Java program source code Maven dependency scopes primarily include these scopes.

#Dependency ScopeAvailable During ScopeTypical Use of Scope
1.Compile scopeIts present at compile, test, runtime environmentIt used in normal application dependencies management concept
2.Provided scopeIts present at compile, test timeIt used in dependencies supplied by the runtime/server method
3.Runtime scopeIts present at test, runtime environmentIt used in libraries needed at runtime environment
4.Test scopeIts present at test only modeIt used in junit, mockito java testing library framework
5.System scopeIts present at compile, test modeIt used in local system jar; generally discouraged mode

Example of a Java dependency scope.

<dependency>

<groupId>org.mockito</groupId>

<artifactId>mockito-core</artifactId>

<version>5.17.0</version>

<scope>test</scope>

</dependency>

The Java Mockito testing framework is only needed for source code testing, so it requires the Java user to have a proper test scope set.

Java Transitive Dependency Management.

One of the most important features of Java Maven dependency management is the transitive dependency management technique.

Let’s assume, in your Java dependency management concept, that.

Your Java application dependency management application

               ↓

Library class X

               ↓

Library class Y

In this case, if class library X requires library Y, Maven dependency management can automatically download the file element of library Y.

This doesn’t require the Java user to declare every indirect Java dependency themselves.

<dependency>

<groupId>com.example</groupId>

<artifactId>library-a</artifactId>

<version>1.0</version>

</dependency>

Here, Maven dependency management resolves and manages library X and its required dependencies.

Java Dependency Conflicts Conditions.

It’s assumed that a single dependency in a Java program may sometimes have conflicts.

User Application

├── Library X → commons-lib 1.0

└── Library Y → commons-lib 2.0

Here, two versions of the same library exist in the dependency graph.

Java users apply dependency mediation rules to select the Maven dependency management version for their programs, with the closest dependency being a necessary rule.

Java users can display a dependency tree in their programs like this.

mvn dependency:tree

Example of Conflicts Conditions.

[INFO] +- org.example:library-a:1.0

[INFO] | \- org.example:common-lib:1.0

[INFO] \- org.example:library-b:2.0

[INFO] \- org.example:common-lib:2.0

This is a very useful concept when troubleshooting dependency conflicts in Java programs.

Removing dependencies in Java programs.

Sometimes transitive dependencies are not needed in Java programs.

Java users can exclude it this way.

<dependency>

<groupId>org.example</groupId>

<artifactId>library-x</artifactId>

<version>1.0</version>

<exclusions>

<exclusion>

<groupId>org.example</groupId>

<artifactId>common-lib</artifactId>

</exclusion>

</exclusions>

</dependency>

This method tells the Java user not to invoke common-lib via library-x in Maven dependency management.

The <dependencies> vs. <dependencyManagement> concept in Java dependency management.

This distinction is important here.

<dependencies>

In reality, it adds a dependency to a project.

<dependencies>

<dependency>

<groupId>org.example</groupId>

<artifactId>common-lib</artifactId>

<version>1.0</version>

</dependency>

</dependencies>

<dependencyManagement>

This primarily indicates to the Java user the versions and configurations that can be inherited or reused. It does not in itself add the dependency to the project’s classpath.

Example of <dependencies> vs. <dependencyManagement>.

<dependencyManagement>

<dependencies>

<dependency>

<groupId>org.example</groupId>

<artifactId>common-lib</artifactId>

<version>1.0</version>

</dependency>

</dependencyManagement>

Also, it can rewrite a child module.

<dependency>

<groupId>org.example</groupId>

<artifactId>common-lib</artifactId>

</dependency>

without specifying the version again.

Why dependency management is important in Java.

Good dependency management in Java programs helps Java users.

  • It allows Java users to avoid manually downloading JAR files.
  • It helps you keep Java library versions consistent.
  • Automatically resolves transitive dependencies.
  • Finds and resolves multiple Java version conflicts.
  • It isolates production and test dependencies.
  • Makes test dependency builds reproducible.
  • It simplifies the multi-module project concept.

Special commands for Maven dependency management.

mvn dependency:tree

This Java command represents the dependency hierarchy tree view structure for program testing.

mvn dependency:analyze

This command helps identify unused or undeclared Java dependencies.

mvn clean install

This Java command creates a dependency project in the program and installs the resulting artifacts into the local Maven repository.

Conclusion of Dependency Management In Java.

In short, Java Maven dependency management helps Java developers declare what your project needs, while Maven dependencies handle where to get it from, what else it requires, and how these dependencies fit together.

Leave a Reply