Maven: child module can not Inheritance parent module's dependencies

╄→尐↘猪︶ㄣ 提交于 2021-01-28 12:30:24

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/55954293/maven-child-module-can-not-inheritance-parent-modules-dependencies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!