如何配置Maven,使其在存在指定禁止依赖时自动构建失败
之前我的一个POM文件曾出现依赖scope配置错误的问题,应用运行时,错误引入的测试类依赖最终导致了OutOfMemoryError。
我希望通过添加插件遍历项目的所有依赖(包括传递性依赖),校验是否存在符合特定规则的依赖,比如artifactId包含test的依赖。
要实现过滤所有依赖、匹配到特定规则时直接让构建失败,参考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>
上述配置借助maven官方的maven-enforcer-plugin的禁止依赖规则,会自动扫描所有直接和传递性依赖,只要匹配到artifactId以test-开头或者以-test结尾的依赖就会直接终止构建,完全满足自定义的依赖校验需求。
内容的提问来源于stack exchange,提问作者TomVW
相关产品推荐
相关产品推荐

