其他分享
首页 > 其他分享> > SpringBoot引入第三方jar包或本地jar包的处理方式

SpringBoot引入第三方jar包或本地jar包的处理方式

作者:互联网

在开发过程中有时会用到maven仓库里没有的jar包或者本地的jar包,这时没办法通过pom直接引入,那么该怎么解决呢

一般有两种方法

第一种是将本地jar包安装在本地maven库
第二种是将本地jar包放入项目目录中
这篇文章主要讲第二种方式,这又分两种情况,一种是打包jar包,第二种是打包war包

image

1. pom文件中引入jar

<dependency>
    <groupId>javacsv</groupId>
    <artifactId>javacsv</artifactId>
    <scope>system</scope>
    <version>1.0</version>
    <systemPath>${project.basedir}/src/main/resources/lib/javacsv.jar</systemPath>
</dependency>

2. 打war包

2.1 定义打包类型为war

<groupId>com.xx</groupId>
<artifactId>xx-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

2.2 定义war包插件

<build>
    <resources>
        <resource>
            <directory>lib</directory>
            <targetPath>/lib/</targetPath>
            <includes>
                <include>${project.basedir}/src/main/resources/lib/</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*</include>
            </includes>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                    </exclude>
                </excludes>
            </configuration>
        </plugin>
        <!-- 定义war包插件  -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <webResources>
                    <resource>
                        <!-- 原路径 -->
                        <directory>src/main/resources/lib</directory>
                        <targetPath>WEB-INF/lib/</targetPath>
                        <includes>
                            <include>**/*.jar</include>
                        </includes>
                    </resource>
                </webResources>
            </configuration>
        </plugin>
        <!-- 定义war包插件  -->
        <!--<plugin>-->
        <!--    <artifactId>maven-compiler-plugin</artifactId>-->
        <!--    <configuration>-->
        <!--        <source>1.8</source>-->
        <!--        <target>1.8</target>-->
        <!--        <encoding>UTF-8</encoding>-->
        <!--        <compilerArguments>-->
        <!--            <extdirs>${project.basedir}/src/main/resources/lib</extdirs>-->
        <!--        </compilerArguments>-->
        <!--    </configuration>-->
        <!--</plugin>-->

        <!-- 打可执行jar包时 定义jar包插件  -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <includeSystemScope>true</includeSystemScope>
            </configuration>
        </plugin>
    </plugins>
</build>

3. 打jar包

3.1 定义打包类型为jar

<groupId>com.xx</groupId>
<artifactId>xx-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

3.2 定义jar包插件(同上面文件一样,通用都包含进去了)

<build>
    <resources>
        <resource>
            <directory>lib</directory>
            <targetPath>/lib/</targetPath>
            <includes>
                <include>${project.basedir}/src/main/resources/lib/</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*</include>
            </includes>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                    </exclude>
                </excludes>
            </configuration>
        </plugin>
        <!-- 定义war包插件  -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <webResources>
                    <resource>
                        <!-- 原路径 -->
                        <directory>src/main/resources/lib</directory>
                        <targetPath>WEB-INF/lib/</targetPath>
                        <includes>
                            <include>**/*.jar</include>
                        </includes>
                    </resource>
                </webResources>
            </configuration>
        </plugin>
        <!-- 定义war包插件  -->
        <!--<plugin>-->
        <!--    <artifactId>maven-compiler-plugin</artifactId>-->
        <!--    <configuration>-->
        <!--        <source>1.8</source>-->
        <!--        <target>1.8</target>-->
        <!--        <encoding>UTF-8</encoding>-->
        <!--        <compilerArguments>-->
        <!--            <extdirs>${project.basedir}/src/main/resources/lib</extdirs>-->
        <!--        </compilerArguments>-->
        <!--    </configuration>-->
        <!--</plugin>-->

        <!-- 打可执行jar包时 定义jar包插件  -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <includeSystemScope>true</includeSystemScope>
            </configuration>
        </plugin>
    </plugins>
</build>

4. 如果不加会出现的错误或者告警

[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for com.new3s:zghWindPowerManage:jar:0.0.1-SNAPSHOT
[WARNING] 'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: org.apache.commons:commons-lang3:jar -> version 3.8.1 vs 3.6 @ line 85, column 21
[WARNING] 'dependencies.dependency.systemPath' for javacsv:javacsv:jar should not point at files within the project directory, ${project.basedir}/src/main/resources/lib/javacsv.jar will be unresolvable by dependent projects @ line 231, column 25
[WARNING] 'build.plugins.plugin.(groupId:artifactId)' must be unique but found duplicate declaration of plugin org.springframework.boot:spring-boot-maven-plugin @ line 275, column 21
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 

[WARNING] 'dependencies.dependency.systemPath' for javacsv:javacsv:jar should not point at files within the project directory, ${project.basedir}/src/main/resources/lib/javacsv.jar will be unresolvable by dependent projects @ line 231, column 25
打包后不存在(BOOT-INF\lib目录下)
image

标签:SpringBoot,lib,--,jar,maven,WARNING,main,第三方
来源: https://www.cnblogs.com/mask-xiexie/p/16086612.html