关于Maven引入本地jar包

200 阅读1分钟

前言

最近给公司服务写了一套方便客户开发的SDK,SDK开发完毕后想在测试服务程序通过maven引入本地jar包的方式进行功能测试,在引入jar包的过程中出现了SDK.jar无法被测试服务.jar打包进去,进儿对maven打包进行了一些研究。

下面进行了两类项目的两种常用打包方式的对比,对打成的jar包目录结果进行了梳理,最后附上maven引入本地jar包的解决方案。

非SpringBoot项目

工程目录

image-20230105170956142

maven默认打包方式

默认打包方式即在pom文件中不设置打包方式,解压后的jar包目录结构如下:

image-20230105170933899

image-20230105171147630

image-20230105171159798

SpringBoot插件方式

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

经过验证,仍然和默认的打包方式是一样的,解压后的jar包目录结果如下:

image-20230105173756584

SpringBoot项目

工程目录

maven默认打包方式

image-20230105174141076

SpringBoot插件方式

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

image-20230105174450541

META-INF内容

image-20230105174607230

BOOT-INF内容

image-20230105174705952

image-20230105174713437

SpringBoot启动jar包class文件

image-20230105174805265

maven引入本地jar包

 <!-- 本地jar包 -->
 <dependency>
   <groupId>com.enss.quequan</groupId>
   <artifactId>quequan</artifactId>
   <version>1.0</version>
   <scope>system</scope>
   <systemPath>${pom.basedir}/conf/xxx-sdk-1.0.jar</systemPath>
 </dependency>
 ​
 <!-- 打包方式 -->
 <build>
   <plugins>
     <plugin>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-maven-plugin</artifactId>
       <configuration>
         <includeSystemScope>true</includeSystemScope>
       </configuration>
     </plugin>
   </plugins>
  • 本地jar包

    • ${pom.basedir}/conf/xxx-sdk-1.0.jar
    • pom.basedir表示项目目录
    • <scope>system</scope>必须是system
  • 打包方式

    • SpringBoot项目使用spring-boot-maven-plugin打包方式