【问题标题】:Maven: child module can not Inheritance parent module's dependenciesMaven:子模块不能继承父模块的依赖
【发布时间】:2019-09-21 02:01:42
【问题描述】:

这是我的父母 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>

这是我的子模块的 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>

我希望子模块可以继承父模块的依赖,但是失败了。

我不能在我的孩子 pom 中使用 lombok 或 junit。

这是我的文件树

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

我认为应该有一种方法可以使一些依赖项成为所有模块的分片,但我找不到解决方案。

【问题讨论】:

  • lombok 项目没有 bom,这意味着您对 lombok 项目的使用根本无法以这种方式工作。因此,只需从您的父母中删除 &lt;scope&gt;import&lt;/scope&gt;。

标签: maven maven-3 dependency-management


【解决方案1】:

在父POM中,&lt;dependencies&gt;和&lt;dependencyManagement&gt;的主要区别如下:

&lt;dependencies&gt; 部分中指定的工件将始终作为子模块的依赖项包含在内。

&lt;dependencyManagement&gt; 部分中指定的工件只有在子模块本身的 &lt;dependencies&gt; 部分中也指定时才会包含在子模块中。

请在以下链接中找到更多信息:

Differences between dependencyManagement and dependencies in Maven

【讨论】:

    【解决方案2】:

    您导入了一个 lombok BOM

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

    然后您尝试将其用作依赖项。但是 BOM 只是一个依赖管理条目的列表。不能是子项目的依赖。

    【讨论】:

      猜你喜欢
      • 2015-06-23
      • 2019-12-30
      • 1970-01-01
      • 2020-12-19
      • 2018-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多