springboot 引入第三方 jar包,可打包

本文详述如何在Maven项目中正确引入第三方jar包,包括通过`pom.xml`配置`systemScope`,手动添加到MANIFEST.MF,以及解决打包时本地库缺失的问题。重点讲解了系统路径设置和清单文件配置技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

引入第三方包

1、不同的包引用可以通过 waven 方式引用 pom.xml 中配置,例:


        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.39</version>
        </dependency>

2、在maven中没有,的jar包引用方式,也是配置pom.xml文件
首先将jar包放入项目资源文件中
再配置pom.xml 文件

<!-- word 转 pdf -->
        <dependency>
            <groupId>com.aspose</groupId>
            <artifactId>aspose-words</artifactId>
            <version>19.2</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/lib/aspose-words-19.2-jdk16.jar</systemPath>
        </dependency>

这时只是将包引入了,但是 maven 中还是没有,打包后的包文件也不会有这个jar
需要在配置一下 pom.xml


            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
            </plugin>

现在可以clean 然后在 install 就完成了

另附maven包库:https://mvnrepository.com/

通过实验,我的第三方包还是没有生成到打包文件中
参考其他方式,通过mvn命令

mvn install:install-file -Dfile=jar包的位置 -DgroupId=上面的groupId -DartifactId=上面的artifactId -Dversion=上面的version -Dpackaging=jar

去掉上面的pom.xml配置,通过正常导入包就可以可了

<!-- word 转 pdf -->
        <dependency>
            <groupId>com.aspose</groupId>
            <artifactId>aspose-words</artifactId>
            <version>19.2</version>
        </dependency>

你会发现这种方式只有自己的电脑可以使用,其他的就不行了,那么下面彻底解决打包问题
同样是修改 pom.xml 文件


        <dependency>
            <groupId>com.aspose</groupId>
            <artifactId>aspose-words</artifactId>
            <version>19.2</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/lib/aspose-words-19.2-jdk16.jar</systemPath>
        </dependency>

配置 maven 打 jar 时修改清单文件,将本地 jar 写到 MANIFEST.MF 文件中,这样运行打包好的 jar 时就不会包找不到类的异常了(注意,多个 jar 的话,需要用空格隔开)

<!-- 打jar包的main方法配置 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.beyond.AutonaviApplication</mainClass>
                        </manifest>
                        <!-- 给清单文件添加键值对(配置文件外置)指定打包后的文件目录 -->
                        <manifestEntries>
                            <Class-Path>config/ config/lib/aspose-words-19.2-jdk16.jar</Class-Path>
                        </manifestEntries>
                    </archive>
                    <!--打包的时候忽略classes 路径下的配置文件 -->
                    <excludes>
                        <exclude>*.properties</exclude>
                        <exclude>*.xml</exclude>
                        <exclude>*.txt</exclude>
                        <exclude>templates/**</exclude>
                    </excludes>
                </configuration>
            </plugin>

现在重新打包,会发现lib库中存在本地包了,完成!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值