SpringBoot项目如何引入外部jar及将外部jar打包到项目发布jar包

17 篇文章 0 订阅
15 篇文章 0 订阅

1、创建一个SpringBoot项目

在这里插入图片描述
下载项目之后将项目导入IDEA

2、如何添加外部jar包

准备一个外部的jar包, 我这里使用的是guava-31.1-jre.jar作为演示
下载地址:https://repo1.maven.org/maven2/com/google/guava/guava/31.1-jre/guava-31.1-jre.jar

在项目根路径下创建一个文件夹libs,将guava-31.1-jre.jar放到libs下。
在这里插入图片描述
在pom.xml添加外部jar依赖:

		<dependency>
			<groupId>com.google.guava</groupId>
			<artifactId>guava</artifactId>
			<version>31.1-jre</version>
			<!--
			    scope=system表示此依赖是来自外部jar,而不是maven仓库
			    注意:
			      只有当scope设置为system时,systemPath属性才会生效
			      systemPath是一个物理文件路径,来指定依赖的外部jar在物理磁盘的位置
			      ${project.basedir}代表项目根目录
			-->
			<scope>system</scope>
			<systemPath>${project.basedir}/libs/guava-31.1-jre.jar</systemPath>
		</dependency>

通过以上步骤, 外部的这个guava-31.1-jre.jar文件就被成功引入到当前项目中了,测试一下
新建一个Test测试类文件
在这里插入图片描述
在这里插入图片描述
测试通过, 外部jar包成功集成到项目中

3、如何将外部jar包打包到当前项目jar包中

如果我们不进行配置,打包的时候是不会将外部的jar打包到jar/war中的

Spring Boot的项目,一般都会使用到打包插件,那么只需要添加一个配置即可

修改pom.xml文件, 修改内容如下:

	<build>
		<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>
					<!-- 在打包时将引用的外部jar引入到当前项目包中	-->
					<includeSystemScope>true</includeSystemScope>
				</configuration>
			</plugin>
		</plugins>
	</build>

includeSystemScope为true这一个参数配置最为重要

如果有些配置文件,并非Spring Boot的默认路径,那么可能就需要借助resouce配置(resources节点和plugins节点同级):

	<build>
		<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>
					<!-- 在打包时将引用的外部jar引入到当前项目包中	-->
					<includeSystemScope>true</includeSystemScope>
				</configuration>
			</plugin>
		</plugins>
		
		<resources>
			<resource>
				<!-- directory 表示取该目录下的文件 -->
				<directory>libs</directory>
				<!--targetPath 指定打包到哪个目录下  默认是放到class目录下-->
				<targetPath>/BOOT-INF/lib/</targetPath>
				<!-- 取符合格式的所有文件  *代表全部 -->
				<includes>
					<include>**/*.jar</include>
				</includes>
			</resource>
		</resources>
	</build>

以上配置完成, 执行打包进行测试, 看看打包完成之后的项目包中是否包含外部jar
在这里插入图片描述

4、总结

  • 引入jar包, 最关键的以下配置
<dependency>
	<groupId>com.google.guava</groupId>
	<artifactId>guava</artifactId>
	<version>31.1-jre</version>
	<!--
	    scope=system表示此依赖是来自外部jar,而不是maven仓库
	    注意:
	      只有当scope设置为system时,systemPath属性才会生效
	      systemPath是一个物理文件路径,来指定依赖的外部jar在物理磁盘的位置
	      ${project.basedir}代表项目根目录
	-->
	<scope>system</scope>
	<systemPath>${project.basedir}/libs/guava-31.1-jre.jar</systemPath>
</dependency>
  • 发布jar包, 最关键的以下配置,使用spring-boot-maven-plugin插件及以下配置
	<build>
		<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>
					<!-- 在打包时将引用的外部jar引入到当前项目包中	-->
					<includeSystemScope>true</includeSystemScope>
				</configuration>
			</plugin>
		</plugins>
		
		<resources>
			<resource>
				<!-- directory 表示取该目录下的文件 -->
				<directory>libs</directory>
				<!--targetPath 指定打包到哪个目录下  默认是放到class目录下-->
				<targetPath>/BOOT-INF/lib/</targetPath>
				<!-- 取符合格式的所有文件  *代表全部 -->
				<includes>
					<include>**/*.jar</include>
				</includes>
			</resource>
		</resources>
	</build>
  • 9
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将外部Jar包引入Spring Boot项目打包Jar包,可以按照以下步骤进行操作: 1. 在pom.xml文件添加依赖项。例如,要引入一个名为example.jar外部Jar包,可以通过以下方式添加依赖项: ``` <dependency> <groupId>com.example</groupId> <artifactId>example</artifactId> <version>1.0.0</version> <scope>system</scope> <systemPath>${project.basedir}/lib/example.jar</systemPath> </dependency> ``` 其,systemPath指定了外部Jar包的路径,scope设置为system,表示使用系统路径下的Jar包。 2. 将外部Jar包复制到项目的lib目录下,例如,将example.jar复制到项目目录下的lib文件夹。 3. 在pom.xml文件添加Maven插件,以将外部Jar包打包到生成的Jar包。例如,可以添加以下插件: ``` <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.2.0</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.example.Application</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> ``` 其,classpathPrefix指定了Jar包lib文件夹下的依赖项路径前缀,mainClass指定了Spring Boot应用程序的主类。 4. 使用Maven命令进行打包,生成的Jar包将包含外部Jar包。例如,使用以下命令进行打包: ``` mvn clean package ``` 这样,生成的Jar包就包含了外部Jar包,并可以在运行时使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值