黑名单Maven依赖项

时间:2012-10-09 06:28:39

标签: java maven maven-3

有没有办法,例如一个Maven插件,它可以列出不需要的/黑色列出的依赖项(直接和传递),如果它检测到列出的依赖项之一,它将失败?

在我的项目中,我们严格要求摆脱Apache Commons Logging并将其替换为SLF4J JCL Bridge。我知道我们必须排除不受欢迎的deps自己,但如果有人添加了一个带来黑名单依赖的依赖项,我想让构建失败。

2 个答案:

答案 0 :(得分:18)

您可以使用maven-enforcer-plugin禁止某些依赖项。

以下是他们排除Apache Commons Logging的更新示例。

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <version>1.1.1</version>
        <executions>
          <execution>
            <id>enforce-banned-dependencies</id>
            <goals>
              <goal>enforce</goal>
            </goals>
            <configuration>
              <rules>
                <bannedDependencies>
                  <excludes>
                    <exclude>commons-logging:commons-logging</exclude>
                  </excludes>
                </bannedDependencies>
              </rules>
              <fail>true</fail>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

运行mvn install时的输出为:

[WARNING] Rule 1: org.apache.maven.plugins.enforcer.BannedDependencies failed with message:
Found Banned Dependency: commons-logging:commons-logging:jar:1.1.1
Use 'mvn dependency:tree' to locate the source of the banned dependencies.

一切都以BUILD FAILURE结尾。

答案 1 :(得分:2)

是的,enforcer plugin支持bannedDependencies规则。