在SpringBoot项目中,一般用Maven解决依赖Jar包的问题。有的时候还是需要加入第三方Jar。这时候如果需要将第三方Jar打入到Jar包里面,应该怎么做呢?
1、假设目录结构是这样的:
|--ProjectDirectory
|--lib
|--jar1.jar
|--jar2.jar
2、我们需要将以上的两个(或多个)第三方Jar包,打入到最终的Jar包,应该先在pom.xml里面将这两个Jar包引入。其中groupId、artifactId、version都是可以自己随意填的,但是最好不要完全一样,要有可区分的部分。然后scope这里需要填写system,systemPath这里需要填写你项目中第三方Jar的位置。${basedir}代表了你这个项目的根目录,也就是ProjectDirectory这个文件夹的目录,后面就根据Jar包在项目中的位置,照着下面的例子填写即可(如果在Intellij IDEA里面,能出现自动提示,就证明引入正确了):
<dependencies>
<!-- 其他的依赖 -->
<dependency>
<groupId>your_organization</groupId>
<artifactId>your_project_name</artifactId>
<version>your_version</version>
<scope>system</scope>
<systemPath>${basedir}/lib/jar1.jar</systemPath>
</dependency>
<dependency>
<groupId>your_organization</groupId>
<artifactId>your_project_name</artifactId>
<version>your_version</version>
<scope>system</scope>
<systemPath>${basedir}/lib/jar2.jar</systemPath>
</dependency>
</dependencies>
3、引入了第三方Jar之后,还没完,还需要在pom.xml里面的build之中加入plugin。plugin的groupId和artifactId如下面所示。还有就是要加入configuration,里面加入includeSystemScope,值为true,这个很重要,就是要包含scope为system的第三方Jar包。如果不加,则scope为system的第三方Jar包,只会在编译环节起作用,运行时则找不到依赖,system的scope类似于provided,所以必须要加:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
4、完整的pom.xml的所需要加入的依赖项如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>XXXGroup</groupId>
<artifactId>XXXArtifactId</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 其他的依赖 -->
<dependency>
<groupId>your_organization</groupId>
<artifactId>your_project_name</artifactId>
<version>your_version</version>
<scope>system</scope>
<systemPath>${basedir}/lib/jar1.jar</systemPath>
</dependency>
<dependency>
<groupId>your_organization</groupId>
<artifactId>your_project_name</artifactId>
<version>your_version</version>
<scope>system</scope>
<systemPath>${basedir}/lib/jar2.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
</project>
5、这个问题的解决方案我也是找了大半天才找到的,参考的以下资料,感谢原作者:https://jingyan.baidu.com/article/215817f74877021edb142378.html