转载:https://zhuanlan.zhihu.com/p/587605618
一、添加外部jar包
准备一个外部jar包,比如api.jar:
在项目路径下创建一个文件夹libs,将api.jar放到libs下。
添加外部jar依赖:
<dependency>
<groupId>com.kfit</groupId>
<artifactId>api</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/libs/api.jar</systemPath>
</dependency>
添加完之后,代码中就可以使用到该jar里的类和方法。
说明:
(1)groupI、artifactId、version随便填写,并不一定要和api.jar的信息,但肯定是要一定的规范和关联性会比较好了。
(2)scope=system表示此依赖是来自外部jar,而不是maven仓库。当scope设置为system时,systemPath属性才会生效,systemPath为一个物理文件路径,来指定依赖的jar其物理磁盘的位置。
(3)${project.basedir}代表根目录。
二、打包外部jar包
上面哪一步仅仅是引用到了项目里,打包的时候是不会打包到jar/war中的。对于Spring Boot的项目,一般都会使用到打包插件,那么只需要添加一个配置即可,如下:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
说明:重要的是includeSystemScope为true。
如果有些配置文件,并非Spring Boot的默认路径,那么可能就需要借助resouce配置(resources节点和plugins节点同级):
<resources>
<resource>
<!-- directory 表示取该目录下的文件 -->
<directory>libs</directory>
<!--targetPath 指定打包到哪个目录下 默认是放到class目录下-->
<targetPath>/BOOT-INF/lib/</targetPath>
<!-- 取符合格式的所有文件 *代表全部 -->
<includes>
<include>**/*.jar</include>
</includes>
</resource>
</resources>
总结
(1)如何添加外部jar包:指定score=system,并且配置sysemPath。
(2)如何打包外部jar包:使用spring-boot-maven-plugin,并且配置属性includeSystemScope为true。