【问题标题】:How can a maven build fail when certain dependencies are present?当存在某些依赖项时,maven 构建如何失败?
【发布时间】:2021-06-03 07:24:14
【问题描述】:

在我的一个 POM 中,依赖项的范围有误。 在运行应用程序时,这个(测试)依赖是 OutOfMemoryError 的原因。

我想通过添加一个迭代所有(包括传递)依赖项并验证特定模式的出现的插件来防止这种情况,例如包含“test”的 artifactId。

当出现某种模式时,如何过滤所有依赖项并导致构建失败?

在 dan1st 回答之后,这个解决方案符合我目前的需求:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <executions>
        <execution>
            <id>enforce-banned-dependencies</id>
            <goals>
                <goal>enforce</goal>
            </goals>
            <configuration>
                <rules>
                    <bannedDependencies>
                        <excludes>
                            <exclude>*:*-test</exclude>
                            <exclude>*:test-*</exclude>
                        </excludes>
                    </bannedDependencies>
                </rules>
                <fail>true</fail>
            </configuration>
        </execution>
    </executions>
</plugin>

【问题讨论】:

  • 你应该小心做这样的事情。这听起来合乎逻辑,但祝你在构建一个依赖于 artifact-id "attest-verifier" 库的项目时好运
  • 公平点,当它发生时必须处理这个问题

标签: java maven


【解决方案1】:

maven 强制插件allows to ban dependencies:

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <version>3.0.0-M3</version>
        <executions>
          <execution>
            <id>enforce-banned-dependencies</id>
            <goals>
              <goal>enforce</goal>
            </goals>
            <configuration>
              <rules>
                <bannedDependencies>
                <searchTransitive>true</searchTransitive>
                <message>Dependency not allowed!</message>
                  <excludes>
                    <!-- do one of the following: --> 
                    <exclude>all.dependencies.with.this.group.id.are.banned</exclude>
                    <exclude>group.id-to-ban:artifact-id-of-artifact-to-ban</exclude>
                    <exclude>group.id-to-ban:artifact-id-of-artifact-to-ban:versiontoban</exclude>
                    <!-- wildcards with '*' are also allowed -->
                  
                </bannedDependencies>
              </rules>
              <fail>true</fail>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

【讨论】:

  • 啊哈,我以为我以前用过一个插件来做类似的事情,但不记得名字了。我会探索这个选项
【解决方案2】:

您可以自定义 maven-enforcer 以包含您自己的规则,该规则实现您的特定用例(文本模式匹配)。例如看看here

【讨论】:

    猜你喜欢
    • 2019-10-10
    • 2019-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-05
    • 2015-09-27
    • 1970-01-01
    相关资源
    最近更新 更多