如何在maven项目中只禁止一个可传递依赖项?

mlmc2os5  于 5个月前  发布在  Maven
关注(0)|答案(2)|浏览(83)

我目前正在尝试的是:

<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


我做错了什么?

fjnneemd

fjnneemd1#

我不知道这是在哪里记录的,但问题是在检查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>

vyu0f0g1

vyu0f0g12#

Thanx的链接到插件的源代码。
我用规则解决了挑战

<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

相关问题