SpringBoot maven引入本地jar包及打包

1,066 阅读1分钟
  1. 在pom.xml同级目录下新建lib文件夹,并放入本地jar包。
  2. 配置Jar包的dependency,包括groupIdartifactIdversion三个属性,同时还要包含scopesystemPath属性,分别指定Jar包来源于本地文件,和本地文件的所在路径。示例:
 <dependency>
     <groupId>com.PDFRenderer</groupId>
     <artifactId>PDFRenderer</artifactId>
     <version>0.0.0</version>
     <scope>system</scope> <!--system,类似provided,需要显式提供依赖的jar以后,Maven就不会在Repository中查找它-->
     <systemPath>${basedir}/lib/PDFRenderer.jar</systemPath> <!--项目根目录下的lib文件夹下-->
 </dependency>

注: scope systemPath 重点,

groupId 包路径

artifactId 主类名

注:${basedir}是指项目根路径。

3.配置插件将本地jar包打入运行jar/war包中,由于scope=system,默认并不会将Jar包打进jar/war包中,所有需要通过插件进行打包。

 <plugin>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-maven-plugin</artifactId>
     <configuration>
         <includeSystemScope>true</includeSystemScope>
     </configuration>
 </plugin>
 <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-dependency-plugin</artifactId>
     <version>2.10</version>
     <executions>
         <execution>
             <id>copy-dependencies</id>
             <phase>compile</phase>
             <goals>
                 <goal>copy-dependencies</goal>
             </goals>
             <configuration>
                 <outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/lib</outputDirectory>
                 <includeScope>system</includeScope>
             </configuration>
         </execution>
     </executions>
 </plugin>

4、将依赖jar包打包至jar包中 方法一:

 <build>
     <finalName>包名</finalName>
     <plugins>
         <!--源码编译-->
         <plugin>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-compiler-plugin</artifactId>
             <version>3.7.0</version>
             <configuration>
                 <source>1.8</source>
                 <target>1.8</target>
             </configuration>
         </plugin>
         <plugin>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-assembly-plugin</artifactId>
             <version>2.4.1</version>
             <configuration>
                 <appendAssemblyId>false</appendAssemblyId>
                 <descriptorRefs>
                     <descriptorRef>jar-with-dependencies</descriptorRef>
                 </descriptorRefs>
                 <archive>
                     <manifest>
                         <mainClass>包程序主类</mainClass>
                     </manifest>
                 </archive>
             </configuration>
             <executions>
                 <execution>
                     <id>make-assembly</id>
                     <phase>package</phase>
                     <goals>
                         <goal>assembly</goal>
                     </goals>
                 </execution>
             </executions>
         </plugin>
     </plugins>
 </build>

方法二:生成含依赖xxx.jar包和original-xxx.jar不含依赖jar包。

  <build>
         <finalName>包名</finalName>
         <plugins>
             <!--源码编译-->
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-compiler-plugin</artifactId>
                 <version>3.7.0</version>
                 <configuration>
                     <source>1.8</source>
                     <target>1.8</target>
                 </configuration>
             </plugin>
             <!-- shade插件打包成jar包 -->
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-shade-plugin</artifactId>
                 <version>3.1.1</version>
                 <executions>
                     <execution>
                         <phase>package</phase>
                         <goals>
                             <goal>shade</goal>
                         </goals>
                         <configuration>
                             <transformers>
                                 <transformer
                                         implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                     <mainClass>包程序主类</mainClass>
                                 </transformer>
                             </transformers>
                         </configuration>
                     </execution>
                 </executions>
             </plugin>         
         </plugins>
 </build>

注:如果tomcat运行找不到第三方jar 配置 如下图

image.png