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

Maven:如何在指定模块中优化排除传递依赖的配置?

更优实现方案

当然有更高效的方式,无需在4个模块中重复配置排除规则,以下两种方案均可解决问题:

方案一:借助父项目的dependencyManagement统一配置

如果7个模块共享同一个父pom,直接在父pom的<dependencyManagement>中定义好带有排除规则的spark-core_2.12依赖:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.12</artifactId>
            <version>3.4.1</version>
            <scope>provided</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.commons</groupId>
                    <artifactId>commons-compress</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
</dependencyManagement>

之后,需要排除依赖的4个模块只需简单引入该依赖即可,无需重复编写版本、scope和排除规则:

<dependencies>
    <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-core_2.12</artifactId>
    </dependency>
</dependencies>

父pom的dependencyManagement会自动将预定义的版本、scope和排除规则应用到子模块的依赖中。若其他3个不需要排除的模块也用到spark-core,可在自身pom中单独定义不带排除规则的依赖,覆盖父pom的配置即可。

方案二:创建独立的BOM(物料清单)pom

若不想修改父pom,或者需要跨多个项目复用该配置,可以单独创建一个打包类型为pom的BOM项目,在其<dependencyManagement>中配置好带排除规则的spark-core依赖:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>你的组ID</groupId>
    <artifactId>spark-deps-bom</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>
    <name>Spark依赖物料清单</name>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.apache.spark</groupId>
                <artifactId>spark-core_2.12</artifactId>
                <version>3.4.1</version>
                <scope>provided</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.apache.commons</groupId>
                        <artifactId>commons-compress</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

将该BOM项目安装到本地仓库或部署到私有仓库后,在需要的4个模块的pom中通过<dependencyManagement>引入这个BOM:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>你的组ID</groupId>
            <artifactId>spark-deps-bom</artifactId>
            <version>1.0.0</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

之后同样在这4个模块中直接引入spark-core依赖即可,BOM中的排除规则会自动生效。

这两种方案都能避免重复配置,降低维护成本,后续若需调整排除规则或依赖版本,仅需修改一处即可。

内容的提问来源于stack exchange,提问作者Jelly

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 09:07:27