SpringBoot项目使用外部jar包,打包运行后报错class未找到
打包方式:
1. 先将jar发布到本地maven仓库
mvn install:install-file -DgroupId=com.labi -DartifactId=pub -Dversion=1.0 -Dpackaging=jar -Dfile=pub-1.0-SNAPSHOT.jar
-DgroupId:表示jar对应的groupId
<groupId>com.labi</groupId>
-DartifactId: 表示jar对应的artifactId
<artifactId>pub</artifactId>
-Dversion: 表示jar对应的 version
<version>1.0</version>
2. 在pom文件中中添加依赖
本项目中外部jar包同一放在resources的lib文件夹下
<dependency>
<groupId>com.labi</groupId>
<artifactId>pub</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/pub-1.0-SNAPSHOT.jar</systemPath>
</dependency>
3. 本项目pom文件,配置打包时把外部jar也打进来
// 主要为以下两行
<includeSystemScope>true</includeSystemScope>
<executable>true</executable>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.7.RELEASE</version>
<configuration>
<mainClass>com.center.CenterApplication</mainClass>
<includeSystemScope>true</includeSystemScope>
<executable>true</executable>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
打包之前先clean,再package
打包命令:mvn clean package -Dmaven.test.skip=true
打完包之后项目中会自动生成一个target文件夹,其中有个项目名.jar包就是我们打成的包