关于SpringBoot不能将第三方或本地jar文件打进war包中的解决办法

今天做东西时要用到第三发jar文件,本地代码运行没有问题,将war包部署服务器一直显示classNotFoundError之类的问题,经过排查发现打war包后第三方jar文件没有打进war中,看了springBoot文档后解决了该问题,特此记录!如下

1、我的第三方jar文件(本地jar亦是如此)

 <!-- 明日摄像头控制 -->
        <dependency>
            <groupId>com.sun</groupId>  <!--自定义-->
            <artifactId>jna</artifactId>    <!--自定义-->
            <version>1.0</version> <!--自定义-->
            <scope>system</scope> <!--system,类似provided,需要显式提供依赖的jar以后,Maven就不会在Repository中查找它-->
            <systemPath>${project.basedir}/src/main/resources/lib/jna.jar</systemPath> <!--项目根目录下的lib文件夹下-->
        </dependency>

2、spring-boot-maven-plugin 中 做如下添加

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <!--将本地包打进war包中-->
    <configuration>
        <includeSystemScope>true</includeSystemScope>
    </configuration>
</plugin>

3、最重要的一步,我就是将这步没有添加花费了好长时间

<!--引入本地/第三方jar包-->
            <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>
                

最后测试成功! 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Maven项目中,我们可以通过以下几种方式将第三方本地JAR文件引入到war包中: 1. 通过 system scope 引入:在项目的pom.xml文件中,添加如下配置: ```xml <dependency> <groupId>com.example</groupId> <artifactId>example</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>${project.basedir}/libs/example.jar</systemPath> </dependency> ``` 这种方式直接指定了本地JAR文件的路径,并将其作为系统路径引入。 2. 通过 systemPath 引入:在系统的pom.xml文件中,添加如下配置: ```xml <dependencies> ... <dependency> <groupId>com.example</groupId> <artifactId>example</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>/path/to/example.jar</systemPath> </dependency> ... </dependencies> ``` 与第一种方式类似,也是通过指定本地JAR文件的路径进行引入。 3. 通过 install 到本地仓库引入:首先需要使用以下命令将本地JAR文件安装到本地Maven仓库中: ``` mvn install:install-file -Dfile=/path/to/example.jar -DgroupId=com.example -DartifactId=example -Dversion=1.0 -Dpackaging=jar ``` 在项目的pom.xml文件中,添加如下配置: ```xml <dependencies> ... <dependency> <groupId>com.example</groupId> <artifactId>example</artifactId> <version>1.0</version> </dependency> ... </dependencies> ``` 这种方式会将本地JAR文件安装到Maven本地仓库中,然后从本地仓库中引入。 无论使用哪种方式,都需要在pom.xml文件中添加相应的依赖配置,以确保Maven能够正确地将第三方本地JAR文件打包进入war包中
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值