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

Maven多模块构建中如何跳过指定模块构建但保留编译依赖?

Maven跳过指定模块的可行方案

Maven本身没有提供类似maven.skip的全局属性直接跳过整个模块,但可以通过以下几种简洁的方式实现需求:

方法1:自定义属性+生命周期插件(推荐)

在需要跳过的复杂模块的pom.xml中添加配置,通过自定义属性控制整个模块的构建流程是否跳过:

<properties>
    <!-- 默认不跳过,可通过命令行覆盖 -->
    <skip.slow.module>false</skip.slow.module>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-lifecycle-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
                <!-- 覆盖所有默认生命周期阶段,执行skip目标 -->
                <execution>
                    <id>skip-module</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>skip</goal>
                    </goals>
                    <configuration>
                        <skip>${skip.slow.module}</skip>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!-- 额外确保install/deploy阶段也跳过(如果需要) -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
            <configuration>
                <skip>${skip.slow.module}</skip>
            </configuration>
        </plugin>
    </plugins>
</build>

之后构建时,只需添加命令行参数即可跳过该模块:

mvn clean install -Dskip.slow.module=true

这种方式的优势是无需每次手动指定模块列表,且能确保该模块的所有构建步骤都被跳过,其他模块依然可以从本地仓库正常依赖它。

方法2:命令行直接排除模块

使用Maven的-pl(--projects)参数,通过!符号排除目标模块:

mvn clean install -pl "!com.yourgroup:slow-module"

如果需要排除多个模块,用逗号分隔:

mvn clean install -pl "!com.yourgroup:slow-module,!com.yourgroup:another-slow-module"

如果模块路径简单,也可以直接用相对路径排除:

mvn clean install -pl "!slow-module"

这种方式无需修改任何pom配置,但需要每次构建时携带该参数,适合临时跳过的场景。

注意事项

  • 不要直接使用maven.skip全局属性:它仅对部分插件(如编译、测试插件)生效,无法覆盖所有构建阶段,可能导致模块仍执行部分耗时操作。
  • 关于Profiles排除模块:其实Profiles可以实现类似效果(在profile中配置<modules>排除目标模块),但前提是本地仓库已存在该模块的构件,其他模块的依赖不会受影响。如果你之前尝试后有问题,可以检查profile的配置是否正确。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 13:03:22