You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.17 18:45:03