Springboot maven加入本地jar,提示找不到类

文章讲述了在Maven项目中,如何正确配置systemScope引用本地jar包以确保打包后能在其他工程中找到依赖。推荐了两种方法:SpringBoot插件配置`includeSystemScope`和使用`maven-compiler-plugin`的`extdirs`属性。同时介绍了打包war时将包含本地jar的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考资料

Maven项目引用本地jar涉及scope配置_includesystemscope-CSDN博客

问题

新建maven工程,通过systempath引入本地的jar,将工程打包后,导入其他工程,会提示找不到之前本地jar的类。

        <dependency>
            <groupId>com.org</groupId>
            <artifactId>jacoco-cli</artifactId>
            <version>1.0.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/org.jacoco.cli-0.8.7-SNAPSHOT-nodeps.jar</systemPath>
        </dependency>

解决方案

正确的做法: 在打包jar的时候,需要将${project.basedir}/lib目录也打包

做法1:

<plugins> 
  <plugin> 
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-maven-plugin</artifactId>  
    <configuration> 
      <includeSystemScope>true</includeSystemScope> 
    </configuration> 
  </plugin> 
</plugins>

做法2:

<plugin>
     <artifactId>maven-compiler-plugin</artifactId>
         <configuration>
             <source>1.8</source>
             <target>1.8</target>
             <compilerArguments>
                   <extdirs>lib</extdirs>
             </compilerArguments>
         </configuration>
</plugin>

打包war

<plugin> 
    <groupId>org.apache.maven.plugins</groupId>  
    <artifactId>maven-war-plugin</artifactId>  
    <configuration> 
      <webResources> 
        <resource> 
          <!-- 存放本地jar的文件夹 src/main/resources/lib -->
          <directory>${project.basedir}/src/main/resources/lib</directory>
          <!-- 目标lib目录 WEB-INF/lib/ -->
          <targetPath>WEB-INF/lib/</targetPath>  
          <includes>
            <!-- 匹配所有jar包 -->
            <include>**/*.jar</include> 
          </includes> 
        </resource> 
      </webResources> 
    </configuration> 
  </plugin>

Spring Boot Maven项目构建的大jar在启动时提示不到的错误,通常是因为在打过程中主的信息没有正确配置。要解决这个问题,可以按照以下步骤进行排查和修复: 1. 检查POM文件配置:确保在项目的`pom.xml`文件中配置了`maven-jar-plugin`,并且设置了正确的`<mainClass>`属性,该属性应指向含`main`方法的。例如: ```xml <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.2.0</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.example.MainClass</mainClass> </manifest> </archive> </configuration> </plugin> <!-- 其他插件配置 --> </plugins> </build> ``` 请确保`<mainClass>`中的名和名与你的主实际路径完全一致。 2. 检查主的`main`方法:确保你的主中有一个`public static void main(String[] args)`方法,并且该的访问修饰符是`public`。 3. 使用命令行正确运行jar:在构建完jar后,使用如下命令来运行jar: ```bash java -jar your-spring-boot-app.jar ``` 如果使用了Maven插件来运行jar,确保使用正确的命令和参数,如下: ```bash mvn spring-boot:run ``` 确保在运行上述命令时,你的工作目录和jar所在目录是正确的。 4. 确认是否有其他相关依赖未打jar:有时候,可能某些运行时需要的资源或库没有被打进最终的jar中。可以通过解压jar文件检查所需的资源是否存在。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值