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

Maven Assembly插件执行mvn verify与mvn deploy生成Zip依赖不一致问题

Maven Assembly Plugin本地与CI打包结果不一致的问题解决

问题描述

本地执行mvn verify时,maven-assembly-plugin生成的Zip文件包含com.example:module的JAR及其依赖JAR;但CI环境执行mvn deploy时,生成的Zip仅包含该module的JAR。使用的assembly descriptor配置如下:

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.2.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.2.0 http://maven.apache.org/xsd/assembly-2.2.0.xsd">

    <!-- providers with extra dependencies -->
    <id>providers-extra</id>

    <formats>
        <format>zip</format>
    </formats>

    <includeBaseDirectory>false</includeBaseDirectory>

    <moduleSets>
        <moduleSet>
            <useAllReactorProjects>true</useAllReactorProjects>
            <includes>
                <include>com.example:module</include>
            </includes>
            <binaries>
                <unpack>false</unpack>
            </binaries>
        </moduleSet>
    </moduleSets>
</assembly>

核心原因

你的assembly配置仅通过<moduleSets>指定了要打包的模块,但没有明确配置包含该模块的依赖。本地环境之所以能包含依赖,大概率是因为你在多模块项目的完整reactor中构建,useAllReactorProjects=true间接把同reactor中的依赖模块也纳入了打包范围;而CI环境可能是单独构建该模块,或者依赖不在当前reactor内,导致插件只打包了模块自身的JAR。

解决方案

1. 修改Assembly Descriptor,明确添加依赖集

在assembly配置中加入<dependencySets>,指定打包com.example:module的所有传递依赖:

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.2.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.2.0 http://maven.apache.org/xsd/assembly-2.2.0.xsd">

    <!-- providers with extra dependencies -->
    <id>providers-extra</id>

    <formats>
        <format>zip</format>
    </formats>

    <includeBaseDirectory>false</includeBaseDirectory>

    <moduleSets>
        <moduleSet>
            <useAllReactorProjects>true</useAllReactorProjects>
            <includes>
                <include>com.example:module</include>
            </includes>
            <binaries>
                <unpack>false</unpack>
            </binaries>
        </moduleSet>
    </moduleSets>

    <!-- 新增依赖集配置,打包module的所有传递依赖 -->
    <dependencySets>
        <dependencySet>
            <useProjectArtifact>false</useProjectArtifact>
            <includes>
                <include>com.example:module:*:*</include>
            </includes>
            <useTransitiveDependencies>true</useTransitiveDependencies>
            <unpack>false</unpack>
        </dependencySet>
    </dependencySets>
</assembly>

2. 检查CI构建上下文

确保CI环境的mvn deploy命令是在完整的多模块项目根目录执行,而非单独构建com.example:module模块。如果必须单独构建,确保所有依赖已发布到CI可访问的maven仓库。

3. 验证插件绑定阶段

检查pom.xml中maven-assembly-plugin的执行配置,确保其绑定到verify阶段(deploy生命周期会自动执行verify阶段的目标),避免因阶段配置错误导致依赖解析异常:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.6.0</version> <!-- 使用合适的插件版本 -->
    <executions>
        <execution>
            <id>build-assembly</id>
            <phase>verify</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <descriptors>
                    <descriptor>src/main/assembly/providers-extra.xml</descriptor>
                </descriptors>
            </configuration>
        </execution>
    </executions>
</plugin>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 16:05:08