Maven Dependencies

In this tutorial, you'll learn how to manage dependencies in a Maven project by adding, updating, and removing them. Dependencies are external libraries or components your project requires to function correctly. Maven simplifies dependency management by automatically downloading and managing them for you.

Prerequisites

  • Ensure that you have Maven installed on your system. To check if Maven is installed correctly, run mvn -v in your command prompt or terminal, and the version information should be displayed.
  • A Maven project, either created manually or generated using an archetype.

1. Add a dependency

To add a dependency, you need to know its Maven coordinates (i.e., groupId, artifactId, and version). These coordinates can be found on the project's website, source code repository, or a public Maven repository such as Maven Central.

Once you have the Maven coordinates, add the dependency to your project's pom.xml file, within the <dependencies> section:

<dependencies>
  <dependency>
    <groupId>com.example</groupId>
    <artifactId>example-library</artifactId>
    <version>1.0.0</version>
  </dependency>
</dependencies>

Replace the groupId, artifactId, and version with the coordinates of the library you want to add.

2. Update a dependency

To update a dependency, change the version element in the <dependency> section in your pom.xml file. Maven will automatically download the new version when building your project.

For example, if you want to update the example-library to version 1.1.0, change the version element like this:

<dependency>
  <groupId>com.example</groupId>
  <artifactId>example-library</artifactId>
  <version>1.1.0</version>
</dependency>

3. Remove a dependency

To remove a dependency from your project, simply delete the corresponding <dependency> section from the pom.xml file.

For example, to remove the example-library dependency, delete the following section:

<dependency>
  <groupId>com.example</groupId>
  <artifactId>example-library</artifactId>
  <version>1.1.0</version>
</dependency>

4. Scope (optional)

Maven allows you to define the scope of a dependency, which determines when and how it's used in your project. The most commonly used scopes are compile, provided, runtime, test, and system.

  • compile (default): The dependency is required for compiling and running the project. It will be included in the final build artifact (e.g., JAR, WAR).
  • provided: The dependency is needed for compilation, but not for runtime. It's assumed that the dependency will be provided by the runtime environment (e.g., a web application server).
  • runtime: The dependency is not needed for compilation, but it's required at runtime.
  • test: The dependency is only needed for compiling and running tests. It won't be included in the final build artifact.
  • system: Similar to provided, but the dependency is expected to be available on the local system and is not fetched from a repository. The dependency's path must be specified using the <systemPath> element.
  1. How to declare dependencies in Maven:

    • Description: Dependencies are declared in the pom.xml file to specify external libraries required for the project.
    • Code:
      • Add dependencies within the <dependencies> section:
        <dependencies>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>example-artifact</artifactId>
                <version>1.0.0</version>
            </dependency>
        </dependencies>
        
  2. Managing version conflicts in Maven dependencies:

    • Description: Conflicts may arise when multiple dependencies use different versions of the same library. Maven resolves conflicts based on a set of rules.
    • Code:
      • Use the dependencyManagement section to enforce versions:
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>com.example</groupId>
                    <artifactId>conflict-artifact</artifactId>
                    <version>2.0.0</version>
                </dependency>
            </dependencies>
        </dependencyManagement>
        
  3. Scoping and packaging of Maven dependencies:

    • Description: Maven supports different scopes for dependencies (compile, runtime, test, provided, system) and allows specifying packaging types.
    • Code:
      • Define scopes in the <dependencies> section:
        <dependencies>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>example-artifact</artifactId>
                <version>1.0.0</version>
                <scope>compile</scope>
            </dependency>
        </dependencies>
        
      • Specify packaging in the <build> section.
  4. Excluding specific dependencies in Maven:

    • Description: Exclude unwanted transitive dependencies or specific artifacts.
    • Code:
      <dependency>
          <groupId>com.example</groupId>
          <artifactId>example-artifact</artifactId>
          <version>1.0.0</version>
          <exclusions>
              <exclusion>
                  <groupId>unwanted-group</groupId>
                  <artifactId>unwanted-artifact</artifactId>
              </exclusion>
          </exclusions>
      </dependency>
      
  5. Working with system and provided scope in Maven:

    • Description: system and provided scopes are used for system-specific or provided dependencies.
    • Code:
      <dependency>
          <groupId>com.example</groupId>
          <artifactId>system-artifact</artifactId>
          <version>1.0.0</version>
          <scope>system</scope>
          <systemPath>${project.basedir}/lib/system-artifact.jar</systemPath>
      </dependency>
      
      <dependency>
          <groupId>com.example</groupId>
          <artifactId>provided-artifact</artifactId>
          <version>1.0.0</version>
          <scope>provided</scope>
      </dependency>