Balázs Fodor-Pap
Balázs Fodor-Pap

Reputation: 608

I can't use inherited dependency in my child modules using maven

I have the following project structure:

I want to use the same dependency (let's call it: **dep-for-B-and-C**) in modules B and C.

 A
 ├── B
 │   └── pom.xml
 ├── C
 │   └── pom.xml
 │
 └── pom.xml

root 'A' pom.xml:

...
<groupId>my-group-id</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
    <module>B</module>
    <module>C</module>
</modules>
...

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>***</groupId>
            <artifactId>dep-for-B-and-C</artifactId>
            <version>***</version>
        </dependency>
    </dependencies>
</dependencyManagement>

'B' and 'C' child modules pom.xml:

  ...
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.8.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <dependency>
    <groupId>***</groupId>
    <artifactId>dep-for-B-and-C</artifactId>
  </dependency>
  ...

If I put dep-for-B-and-C as a dependency of A and B then after I run mvn clean install, I got the following error:

[ERROR]     'dependencies.dependency.version' for ***:dep-for-B-and-C-jar:jar is missing. @ line 39, column 21

If I don't put the dependency (I'm using dependencyManagement so it should place but I don't need to specify version (this is my understanding)) into my child pom.xml then, I have a compile error because I can't use my dependency.

Please note that I have spring-boot-starter-parent as a parent for B and C, because project B and C will be a Spring boot project. The reason why I don't put this in the root (A pom.xml) is that maybe I will have another module that won't be Java project.

This can be the reason? If yes, then should I create a project structure like this?

A
├───Backend
│    ├── B
│    │   └── pom.xml
│    ├── C
│    │   └── pom.xml
│    │
│    └── pom.xml (spring-boot-starter as parent)
│
├───Frontend (using: eirslett mvn plugin)
│
└──pom.xml

Upvotes: 0

Views: 992

Answers (2)

J Fabian Meier
J Fabian Meier

Reputation: 35805

If you don't use A as parent POM, then cannot use dependencyManagement from A.

Upvotes: 3

Prashant
Prashant

Reputation: 5383

Here is the problem:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.8.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

in modules B and C. Please add module A as parent of B and C

Upvotes: 3

Related Questions