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

Spring Boot多模块项目CI构建缺失common-lib依赖问题排查

Spring Boot多模块Maven项目CI构建依赖缺失问题

项目结构与配置

父模块pom.xml

<?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>com.abc.xyz.parent</groupId>
    <artifactId>parent</artifactId>
    <version>0.1.0</version>
    <packaging>pom</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId> 
        <version>2.7.5</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <revision>0.1.0</revision>
        <java.version>1.8</java.version>
        <spring-boot.version>2.7.5</spring-boot.version>
        <powermock.version>1.6.4</powermock.version>
        <common-lib.version>${revision}</common-lib.version>
        <maven-jacoco.version>0.8.6</maven-jacoco.version>
        <maven-surefire.version>3.0.0</maven-surefire.version>
    </properties>

    <!-- To build everything properly use "mvn clean install -Pbuild" -->
    <modules>        
        <module>common-lib</module>
        <module>my-service</module>
    </modules>
    <dependencyManagement>
      <dependencies>
         <dependency>
            <groupId>com.abc.xyz.parent</groupId>
            <artifactId>common-lib</artifactId>
            <version>${common-lib.version}</version>
         </dependency>
      </dependencies>
    </dependencyManagement>
</project>

common-lib模块pom.xml

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.abc.xyz.parent</groupId>
<artifactId>common-lib</artifactId>
<version>0.1.0</version>
<parent>
    <groupId>com.abc.xyz.parent</groupId>
    <artifactId>parent</artifactId>
    <relativePath>../pom.xml</relativePath>
    <version>0.1.0</version>
</parent>

<!-- 
 Do not configure dependencies here (version, scope, exclusions, etc). Define them in a
 central place, the dependency management in the root pom.
-->
<dependencies>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
    </dependency>       
</dependencies>
<build>
    <plugins>
        <plugin>    
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${maven-surefire.version}</version>
            <configuration>
                <systemPropertyVariables>
                    <jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
                </systemPropertyVariables>
            </configuration>
        </plugin>
    </plugins>
</build>
</project>

my-service模块pom.xml

<?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>  
    <artifactId>my-service</artifactId>
    <version>0.1.0</version>

     <parent>
        <groupId>com.abc.xyz.parent</groupId>
        <artifactId>parent</artifactId>
        <relativePath>../pom.xml</relativePath>
        <version>0.1.0</version>
    </parent>

    <!-- 
     Do not configure dependencies here (version, scope, exclusions, etc). Define them in a
     central place, the dependency management in the root pom.
    -->
    <dependencies>
        <!-- All spring-boot dependencies requires omitted-->
       <dependency>
            <groupId>com.abc.xyz.parent</groupId>
            <artifactId>common-lib</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>${maven-jacoco.version}</version>
                <executions>
                    <execution>
                        <id>report</id>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                    <execution>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven-surefire.version}</version>
                <configuration>
                    <systemPropertyVariables>
                        <jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
                    </systemPropertyVariables>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <!-- <executable>true</executable> -->
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

问题现象

本地执行mvn package test时,my-service.jar的BOOT-INF/lib目录下正常包含common-lib.jar;但在Jenkins/CI环境构建时,编译成功但生成的my-service.jar中缺失该依赖,运行应用时抛出java.io.FileNotFoundException异常。

问题分析与解决办法

核心原因

  1. CI构建命令不符合项目要求:父模块注释明确标注要使用mvn clean install -Pbuild完成构建,但CI可能只执行了mvn package test。package命令只会在模块本地生成产物,不会安装到Maven仓库,而Spring Boot打包插件需要依赖在仓库中存在才能正确引入。
  2. CI构建路径错误:如果CI环境是单独进入my-service目录执行构建,Maven无法识别同级的common-lib模块,自然无法将其Jar包打入最终产物。

解决步骤

  • 统一CI构建命令:在Jenkins中执行父模块根目录的mvn clean install -Pbuild,确保common-lib先被构建并安装到CI环境的Maven仓库,再构建my-service。
  • 检查CI工作目录:确保CI从父模块目录启动构建流程,不要单独构建子模块。
  • 优化依赖声明(可选):在my-service的common-lib依赖中显式声明scope=compile(默认就是compile,但显式声明可避免CI环境潜在的配置异常):
<dependency>
    <groupId>com.abc.xyz.parent</groupId>
    <artifactId>common-lib</artifactId>
    <scope>compile</scope>
</dependency>
  • 验证CI Maven配置:确认CI环境的Maven设置没有忽略本地仓库,也不存在镜像仓库导致依赖无法被正确拉取的情况。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 09:24:52