0

I am now building a spring application which consists of multiple spring projects as micro-services. I want to put all these spring projects into a parent module and then convert it into a multi-module maven project. But I have met a problem as each sub-module needs to have a parent reference to spring-cloud-starter-parent, so natually, I am moving this into the parent module. But now the problem happened, each submodule is using different version of spring-cloud-starter-parent. Let's see below.

part of POM for parent module:

<groupId>com.demo</groupId>
    <artifactId>spring-cloud-demo</artifactId>
    <version>2.0</version>
    <packaging>pom</packaging>

    <name>Spring Cloud Demo</name>

    <parent>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-parent</artifactId>
        <version>Angel.SR3</version>
    </parent>

And in sub-module, part of POM is:

<parent>
        <groupId>com.demo</groupId>
        <artifactId>spring-cloud-demo</artifactId>
        <version>2.0</version>
        <relativePath>../pom.xml</relativePath>
      </parent>

I though this is working, as the spring-cloud-starter-parent is already introduced in the parent module, so it will be available in the sub-module. BUT I suddenly met a problem, some child sub-module needs to have different spring-cloud-starter-parent version, for example, 1.0.0-SNAPBUILD.

In this case, how can I still use multi-module maven project structure? How can I use different parent version in the sub-module? Or I have to separate this into completely two different projects?

Thanks.

user3006967
  • 3,291
  • 10
  • 47
  • 72

1 Answers1

0

you should be able to override the parent dependency in the sub module pom to point at the specific dependencies that you need.

Dependency mediation - this determines what version of a dependency will be used when multiple versions of an artifact are encountered. Currently, Maven 2.0 only supports using the "nearest definition" which means that it will use the version of the closest dependency to your project in the tree of dependencies. You can always guarantee a version by declaring it explicitly in your project's POM. Note that if two dependency versions are at the same depth in the dependency tree, until Maven 2.0.8 it was not defined which one would win, but since Maven 2.0.9 it's the order in the declaration that counts: the first declaration wins.

"nearest definition" means that the version used will be the closest one to your project in the tree of dependencies, eg. if dependencies for A, B, and C are defined as A -> B -> C -> D 2.0 and A -> E -> D 1.0, then D 1.0 will be used when building A because the path from A to D through E is shorter. You could explicitly add a dependency to D 2.0 in A to force the use of D 2.0

http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

Paul John
  • 1,626
  • 1
  • 13
  • 15