2012-10-09 51 views
15

有没有办法一个Maven插件可以列出不需要的/黑名单的依赖关系(直接和传递),如果检测到列出的依赖关系之一,则构建失败?黑名单Maven依赖关系

在我的项目中,我们严格地希望摆脱Apache Commons Logging并将其替换为SLF4J JCL Bridge。我意识到我们必须排除不需要的deps自己,但如果有人添加了依赖项,导致列入黑名单依赖项,我希望构建失败。

回答

17

您可以使用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结束。

2

是的,enforcer plugin支持这与bannedDependencies规则。

+0

另一个我最喜欢的是codehaus extra enforcer规则库中的[banDuplicateClasses](http://mojo.codehaus.org/extra-enforcer-rules/banDuplicateClasses.html)规则。这将检测由'commons-logging'和'jcl-over-slf4j'上的依赖引入的重复类。 – Ramon