【问题标题】:Can child poms inherit dependency exclusions defined in the parent pom?子 pom 可以继承父 pom 中定义的依赖排除吗?
【发布时间】:2016-10-03 15:39:42
【问题描述】:

我不确定这是否得到 Maven 的支持。我很感激我能得到的任何帮助。

我有一个定义依赖项和排除项的父 pom。我无法更改父 pom:

<dependency>
    <groupId>foo</groupId>
    <artifactId>bar</artifactId>
    <version>1.0</version>
    <exclusions>
        <!-- this exclusion needs to be inherited by all children -->
        <exclusion>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </exclusion>
    </exclusions>
</dependency>

然后在子 pom 中,我需要从父级中的相同依赖项中排除 不同 依赖项。像这样

<dependency>
    <groupId>foo</groupId>
    <artifactId>bar</artifactId>
    <version>1.0</version>
    <exclusions>
        <!-- this exclusion is just for the child -->
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
    </exclusions>
</dependency>

但如果我这样做,孩子将排除 slf4j jar(正确),但不会排除 spring-cloud-config-server jar,除非我在子声明中重申父排除依赖。

我意识到我可以直接复制它,但这很麻烦,而且我意识到将孩子的排除权推给父母很容易,但我会将这种排除权强加给所有其他孩子。

我希望Maven在父子项中声明相同的依赖项时合并依赖项排除信息。

这可能吗?

【问题讨论】:

  • 这可以通过在父项中使用dependencyManagement 来正确处理,但是您说您不能更改父项...在您的情况下,您必须复制排除项....此外,原始项目仅使用 slf4j 而不是仅使用 slf4j-api 会出错,让用户决定应该使用什么真正的实现......
  • 这可能行得通。我说我不能改变它,但我真的不能把 log4j2 强加给父母。我也许可以改变父母以减少干扰。我试试看。
  • 你能澄清一下它如何与dependencyManagement一起工作吗?你是说我会将排除项放在 dependencyManagement 部分中并且会向下传播?

标签: maven


【解决方案1】:

在你的父 pom 中:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>foo</groupId>
            <artifactId>bar</artifactId>
            <version>1.0</version>
            <exclusions>
                <!-- this exclusion needs to be inherited by all children -->
                <exclusion>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-config-server</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
</dependencyManagement>

在你的孩子 pom 中:

<dependencies>
    <dependency>
        <groupId>foo</groupId>
        <artifactId>bar</artifactId>
        <exclusions>
            <!-- this exclusion is just for the child -->
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

【讨论】:

    猜你喜欢
    • 2016-12-17
    • 2018-04-12
    • 1970-01-01
    • 2019-10-23
    • 1970-01-01
    • 2017-02-22
    • 2012-03-17
    • 2013-08-20
    • 2021-07-12
    相关资源
    最近更新 更多