简体   繁体   English

当存在某些依赖项时,maven 构建如何失败?

[英]How can a maven build fail when certain dependencies are present?

In one of my POM's a dependency had a wrong scope.在我的一个 POM 中,一个依赖项有一个错误的 scope。 While running the application, this (test) dependency was the cause of an OutOfMemoryError.在运行应用程序时,这个(测试)依赖是 OutOfMemoryError 的原因。

I would like to prevent this by adding a plugin that iterates over all (including transitive) dependencies and verify the occurrence of a certain pattern, like an artifactId containing "test".我想通过添加一个迭代所有(包括传递)依赖项并验证特定模式的出现的插件来防止这种情况,例如包含“test”的 artifactId。

How can I filter all dependencies and fail the build when a certain pattern appears?当出现某种模式时,如何过滤所有依赖项并使构建失败?

After dan1st answer, this solution fits my current needs:在 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>

The maven enforcer plugin allows to ban dependencies : maven 强制插件允许禁止依赖

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

You can customize maven-enforcer to include your own rule which implements your specific use case (text pattern match).您可以自定义 maven-enforcer 以包含您自己的规则,该规则实现您的特定用例(文本模式匹配)。 For example take a look here .例如看看这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM