LeckerMaedschen
LeckerMaedschen

Reputation: 161

Maven - Using Dependency Management

We need to modularize our project structure. For that we are using from beginning the Build-Management-System Maven. The projects uses internal library. We want use the benefit of the dependency Managent of Maven and override all used internal library with one version that is definied there. My question is very simple, but not clear for me. Maybe you can help me with that.

I have two types of parent-poms.

(1) Parent.pom -> Set placeholder for the versions and define them

(2) Child.pom -> Build and copy modules to some place

Every project is a child from (2).

My question is: Where is the best place to put the dependency Management? In parent.pom or in child.pom? I found out, that using depManagement in parent.pom does not support to remove the version tag in the dependencies in the projects.

So my question, where is the best place to put the dependency Management?

Thanks.

Upvotes: 1

Views: 170

Answers (1)

Ivan Lymar
Ivan Lymar

Reputation: 2293

I would use bill of materials(BOM) for dependency managment. You could get more about BOM from:

How to use BOM file with Maven?

Maven BOM [Bill Of Materials] Dependency

The structure that I would suggest:

Parent project:

    <groupId>bom.test</groupId>
    <artifactId>parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>childA</module>           
    </modules>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-framework-bom</artifactId>
                <version>4.3.8.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

As you could see here I've used spring BOM.

Child A project:

    <parent>
        <artifactId>parent</artifactId>
        <groupId>bom.test</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>childA</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>3.0.0.RELEASE</version>
        </dependency>
    </dependencies>

As you see from child dependencies I do not specify spring-web version, it is taken from BOM. However spring-test version was specified. Let's check how maven sees it.

Checking bom content by

mvn help:effective-pom

.....
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>4.3.8.RELEASE</version>
      </dependency>   
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.3.8.RELEASE</version>
      </dependency>     
    </dependencies>
  </dependencyManagement>
.....

So BOM version is 4.3.8

Let's check dependency managment to be sure that moduleA uses this version for web and overriden for test.

mvn dependency:tree

[INFO] bom.test:childA:jar:1.0-SNAPSHOT
[INFO] +- org.springframework:spring-web:jar:4.3.8.RELEASE:compile
[INFO] |  +- org.springframework:spring-aop:jar:4.3.8.RELEASE:compile
[INFO] \- org.springframework:spring-test:jar:3.0.0.RELEASE:compile

Upvotes: 1

Related Questions