maven打包问题/ClassNotFoundException异常

报错

在这里插入图片描述
com.xxx.encrypt.Encrypt是本地jar包,本地开发环境没有问题,测试环境运行jar包报上述错误
ClassNotFoundException异常
问题:com.xxx.encrypt.Encrypt类找不到。
原因分析:该类可能未被正确打包到应用程序中,或者类的包路径、类名存在拼写错误。

排查

“该类可能未被正确打包到应用程序中”,测试用Jenkins打包工具报错,本地打包在测试环境启动也报错,说明jar包一直没有打进去。
验证
1.将包用解压工具打开,在目录 BOOT-INF/lib/xx 去找是否有报错信息中缺失的包,我这里验证是没有的

解决

  1. 目录结构,使用若依框架
    ruoyi (根项目,POM类型)
    ├── ruoyi-admin (Web服务入口) pom配置打包信息
    ├── ruoyi-framework (框架核心)
    ├── ruoyi-system (系统模块)
    ├── ruoyi-common (通用工具) 此次问题jar包引入处
    ├── ruoyi-quartz (定时任务)
    └── ruoyi-generator (代码生成器)
  2. maven项目,引入的本地依赖,在pom文件(ruoyi-common下)进行了配置,配置如下
<!-- 加解密工具 -->
<dependency>
  <groupId>xx</groupId>
  <artifactId>xx</artifactId>
  <version>xx</version>
  <scope>system</scope>
  <systemPath>${project.basedir}/lib/xx.jar</systemPath>
</dependency>

需要注意的是<scope>system</scope>标签,表示从本地引入

  1. 检查打包pom配置中的<includeSystemScope>true</includeSystemScope>是否设置为true
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.5.15</version>
            <configuration>
                <fork>true</fork>
                <!--  检查是否为true  -->
                <includeSystemScope>true</includeSystemScope>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
                <warName>${project.artifactId}</warName>
            </configuration>
        </plugin>
    </plugins>
    <finalName>${project.artifactId}</finalName>
</build>

上述步骤检查,解决本地包没有打入包的问题

原因

  1. 在 Maven 的 Spring Boot 插件配置中,<includeSystemScope>true</includeSystemScope> 的作用是:
    当项目中存在使用 <scope>system</scope> 声明的系统依赖(通常是本地文件系统中的 JAR 包)时,设置此属性为 true 会让 Spring Boot 打包插件在构建可执行 JAR/WAR 时,将这些系统依赖包含到最终的打包文件中。
  2. 默认情况下,Maven 不会将 system 范围的依赖打包到构建产物中,因为这类依赖通常被认为是由系统环境提供的。而该配置强制将系统依赖包含进来,确保应用在运行时可以正常访问这些本地依赖,避免出现 ClassNotFoundException 等因缺少依赖导致的错误。
Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐