如何在我的 Maven 项目中只禁止一个传递依赖?

问题描述 投票:0回答:2

我目前正在尝试的是:

<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

我做错了什么?

java maven dependencies
2个回答
4
投票

我没有看到这个记录在哪里,但问题是在检查

banTransitiveDependencies
规则时也会考虑当前的构建工件。然后,查看代码,由于此工件已被排除,因此它不会检查其依赖项。因此,当您为排除模式指定
*
时,主要工件会匹配该模式,而其余的包含规则将被忽略。所以以下方法可行:

<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>

0
投票

感谢插件源代码的链接。

我用规则解决了挑战

<rules>
  <bannedDependencies>
    <excludes>
      <exclude>groupId:artifactId</exclude>
    </excludes>
  </bannedDependencies>
</rules>

输出样本:

Rule 0: org.apache.maven.enforcer.rules.dependency.BannedDependencies failed with message:
SOME_GAV:36.0.39-SNAPSHOT
   SOME_GAV:30.000.00.41
      groupId:artifactId:jar:7.1.11 <--- banned via the exclude/include list

使用的插件版本:3.4.1

© www.soinside.com 2019 - 2024. All rights reserved.