Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically reuse dependency versions in a multi-module Maven project?

In one module, I use spring-boot-starter-activemq:2.07.RELEASE which depends on activemq-broker:5.15.8 which depends on guava:18.0.

In another module, I would like to use guava, so I have to use:

<dependency>
  <groupId>com.google.guava</groupId>
  <artifactId>guava</artifactId>
  <version>18.0</version>
</dependency>

If I use an higher version in my pom.xml this version will be also used by activemq-broker due to the nearest definition rule of the dependency mediation (see Introduction to the Dependency Mechanism)

I don't want to provide a different version of Guava than what is asked by activemq-broker. So in order to synchronize the versions, each time there is a Spring Boot upgrade, I need to check manually the versions in order to synchronize them.

I use activemq-broker and guava as an example but my question is more general: How to automatically reuse a dependency version from one module into another?

like image 293
Ortomala Lokni Avatar asked Nov 06 '22 22:11

Ortomala Lokni


1 Answers

I would define a parent for my project where dependency management will be handled.(You probably already have this). In the parents dependendency management section, I would import dependency management of activemq-parent. This way you can just define dependencies, without explicit versions in the childs.

Also you can make your parent inherit from spring-boot-dependencies to get versions properties. (In this example activemq.version is fetched from this)

Example: Parent pom

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.1.1.RELEASE</version>
</parent>

<dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-parent</artifactId>
        <version>${activemq.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      .....
</dependencyManagement>

If Your parent doesn't inherit from spring-boot-dependencies, You would have to write specific version instead of ${activemq.version} for activemq-parent

After this in the child

<dependency>
  <groupId>com.google.guava</groupId>
  <artifactId>guava</artifactId>
</dependency>

Version of the guava will be same as activemq-parent. ( Because it is defined there)

like image 110
miskender Avatar answered Nov 14 '22 21:11

miskender