5

This is my parent pom

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.github.fish56</groupId>
    <artifactId>MavenModules</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>dao</module>
    </modules>
    <packaging>pom</packaging>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.6</version>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

this is my child module's pom

    <parent>
        <artifactId>MavenModules</artifactId>
        <groupId>com.github.fish56</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dao</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
    </dependencies>

I wish child module can inheritance parent's dependencies, but it failed.

I can not use lombok or junit in my child pom.

And this is my file tree

.
├── dao
│   ├── pom.xml
│   ├── src
│   └── target
├── pom.xml

I think there should a way the make some dependencies to be shard ammon all modules, But I can not find the solution.

Shuai Li
  • 2,426
  • 4
  • 24
  • 43
  • The lombok project has not bom which means your usage of the lombok project is simply not working that way. So just remove `import` from your parent. – khmarbaise May 02 '19 at 18:41

2 Answers2

11

In the parent POM, the main difference between the <dependencies> and <dependencyManagement> is as follows:

Artifacts specified in the <dependencies> section will ALWAYS be included as dependencies of the child module(s).

Artifacts specified in the <dependencyManagement> section, will only be included in the child module if they were also specified in the <dependencies> section of the child module itself.

Please find more at following link:

Differences between dependencyManagement and dependencies in Maven

Anshul Singhal
  • 1,983
  • 20
  • 25
  • If I agree to this , I have similar kind of setup and really wonder how mvn clean install is successful but IDE (Intellij IDEA ) asks to add dependency in child module – Rajeev Akotkar Mar 22 '22 at 11:50
1

You import a lombok BOM

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.6</version>
            <scope>import</scope>
        </dependency>

and then you try to use it as a dependency. But a BOM is just a list of dependencyManagement entries. It cannot be a dependency of the child project.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142