SpringBoot引入外部jar,通过maven打包项目

1、引入jar
在/src/main/resources/目录下新建lib文件夹,并把jar包文件放到这个目录下

2、在pom.xml文件定义依赖指向该jar
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
    <version>${spring-boot-admin.version}</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/src/main/resources/lib/commons-logging-1.2.jar</systemPath>
</dependency>
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
    <version>${spring-boot-admin.version}</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/src/main/resources/lib/software-share-1.0.3.jar</systemPath>
</dependency>

注意:重点是systemPath这个路径必须得是你jar的路径。其他的按照套路填就行,要求不是太严格。${project.basedir}只是一个系统自己的常量,不用管它

如果需要打包成jar,同时把jar也引入进去,直接在maven的pom里给springboot的打包插件引入一下参数就行
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>${spring-boot.version}</version>
    <configuration>
        <includeSystemScope>true</includeSystemScope>
        <skipTests>true</skipTests>
        <testFailureIgnore>true</testFailureIgnore>
    </configuration>
</plugin>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>${maven-compiler-plugin.version}</version>
    <configuration>
        <skipTests>true</skipTests>
        <testFailureIgnore>true</testFailureIgnore>
        <!--指定外部lib,/data/lib目录存放jar-->
        <compilerArguments>
            <extdirs>/data/lib</extdirs>
        </compilerArguments>
        <source>${java.version}</source>
        <target>${java.version}</target>
</plugin>

3、修改pom文件
在build标签下增加如下配置
<build>
    <resources>
        <resource>
            <directory>${project.basedir}/src/main/resources/lib/</directory>
            <targetPath>BOOT-INF/lib/</targetPath>
            <includes>
                <include>**/*.jar</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <targetPath>BOOT-INF/classes/</targetPath>
        </resource>
    </resources>
</build>

 

posted on 2020-12-09 19:51  Ruthless  阅读(1848)  评论(0编辑  收藏  举报