Performing multi-module dependency management

Dependency management is a mechanism to centralize dependency information. When there are a set of projects (or modules) that inherit a common parent, all information about the dependency can be put in the parent pom and the projects can have simpler references to them. This makes it easy to maintain the dependencies across multiple projects and reduces the issues that typically arise due to multiple versions of the same dependencies.

How to do it...

  1. Open a multi-module project (simple-multi-module).
  2. Add a dependency for junit in the dependencyManagement section:
    <dependencyManagement>
        <dependencies>
          <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
          </dependency>
        </dependencies>
    </dependencyManagement>
  3. Update the dependencies section of the child project as follows:
    <dependencies>
         <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
         </dependency>
    </dependencies>
  4. Run the following command:
    mvn clean test
    

    Ensure that the build completes successfully.

  5. Run the Maven command to check the dependency:
    mvn dependency:tree
    
  6. Observe the results:
    [INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ simple-multi-module
    ---
    [INFO] com.packt.cookbook:simple-multi-module:pom:1.0-SNAPSHOT
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] Building Child Project 1.0-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ child ---
    [INFO] com.packt.cookbook:child:jar:1.0-SNAPSHOT
    [INFO] - junit:junit:jar:3.8.1:test
    

How it works...

Dependencies that are specified within the dependencyManagement section of the parent pom are available for use to all the child projects. The child project needs to choose the dependencies by explicitly specifying the required dependencies in the dependencies section. While doing this, the child projects can omit the version and scope information so that they are inherited from the parent.

You may ask, "Why have the dependencyManagement section when child projects inherit dependencies defined in the parent pom anyway?" The reason is, the parent centralizes dependencies across several projects. A child project typically needs only some of the dependencies that the parent defines and not all of them. The dependencyManagement section allows child projects to selectively choose these.

There's more...

The dependencyManagement section also helps address any surprises of Maven's dependency mediation. Dependency mediation is what determines what version of dependency will be used when multiple versions of an artifact are encountered. However, dependencyManagement takes precedence over dependency mediation and ensures that dependency mediation does not pick a version over the one specified in dependencyManagement.

It should be noted that dependencies on different versions are error prone and dependencyManagement cannot always save them from library version incompatibilities.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.117.191