【问题标题】:How can I ban only one transitive dependency in my maven project?如何在我的 Maven 项目中只禁止一个传递依赖?
【发布时间】:2016-05-06 06:53:22
【问题描述】:

我目前正在尝试的是:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <version>1.4.1</version>
    <executions>
        <execution>
            <id>enforce-banned-dependencies</id>
            <goals>
                <goal>enforce</goal>
            </goals>
            <configuration>
                <rules>
                    <banTransitiveDependencies>
                        <excludes>
                            <exclude>*:*:*</exclude>
                        </excludes>
                        <includes>
                            <include>commons-lang:commons-lang:2.4</include>
                        </includes>
                    </banTransitiveDependencies>
                </rules>
            </configuration>
        </execution>
    </executions>
</plugin>

我上面尝试的意图是:

排除禁止所有传递依赖,除了 commons-lang:2.4

当我尝试时

mvn verify

我会得到

[INFO] --- maven-enforcer-plugin:1.4.1:enforce (enforce-banned-dependencies) @ ebtam-core ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

这不好。因为我知道我的项目中有以下依赖项:

[INFO] +- org.apache.velocity:velocity:jar:1.6.2:compile
[INFO] |  +- commons-lang:commons-lang:jar:2.4:compile

我做错了什么?

【问题讨论】:

  • 我不能肯定地说(这就是为什么这是评论,而不是答案),但不是反过来吗?您想从禁令规则中排除 commons-lang,但强制禁止其他所有内容。所以你的 ::* 被包括在内,而 commons-lang:commons-lang:2.4 被排除在外。这就是我从maven.apache.org/enforcer/enforcer-rules/…理解它的方式@
  • @Creperum 不,我想排除所有内容,包括 commons-lang。
  • 如果你想禁止 commons-lang 依赖,你只需要像你已经做的那样定义包含,但你必须删除排除。
  • @khmarbaise 但是包含只是覆盖排除,不是吗?
  • 排除优先....通常您排除特定工件/版本等...

标签: java maven dependencies


【解决方案1】:

我没有看到记录在哪里,但问题是在检查 banTransitiveDependencies 规则时也会考虑当前的构建工件。然后,looking at the code,因为这个工件被排除在外,它不会检查它的依赖关系。因此,当您为排除模式指定 * 时,主要工件与该模式匹配,其余包含规则将被忽略。所以下面会起作用:

<rules>
    <banTransitiveDependencies>
        <excludes>
            <exclude>commons-lang</exclude>
        </excludes>
        <includes>
            <include>commons-lang:commons-lang:2.4</include>
        </includes>
    </banTransitiveDependencies>
</rules>

但它没有回答您“排除禁止所有传递依赖项,commons-lang:2.4 除外”的问题。

问题是,您为什么首先使用此规则?它是在MENFORCER-138 中引入的,其目标是强制开发人员不依赖被继承的传递依赖,并强制在 POM 中声明它们。

如果依赖项commons-lang:commons-lang:2.4 在类路径中,您的目标是使构建失败。因此,您应该使用bannedDependencies 规则。默认情况下,它会传递搜索依赖项。以下将做你想做的,即只禁止commons-lang:commons-lang:2.4

<rules>
    <bannedDependencies>
        <excludes>
            <exclude>commons-lang:commons-lang:2.4</exclude>
        </excludes>
    </bannedDependencies>
</rules>

【讨论】:

  • 好吧,我把情况简化了一点,但是谢谢,这对我很有帮助。如果它对我有用,我会尝试接受答案(我相信它会)。
猜你喜欢
  • 2016-09-14
  • 2022-11-30
  • 2013-02-17
  • 1970-01-01
  • 1970-01-01
  • 2010-09-07
  • 2021-09-30
  • 2020-08-04
  • 1970-01-01
相关资源
最近更新 更多