当前位置:   article > 正文

springboot项目中引入本地依赖jar包,如何打包到lib文件夹中_springboot引入lib包

springboot引入lib包

前言:
工作中,碰到过springboot框架构建的java web项目,需要集成第三方推送功能,于是使用到了小米推送服务,下载了相关jar包。项目中引入本地jar,问题不大,编写完代码后,通过测试类测试,也没问题。然后就准备打包部署到开发服上。由于项目是通过tomcat部署的,所以打包方式是打成war包。打包后上传到开发服,启动成功后去测试编写的推送接口,发现失败了。

通过分析发现,打包后的war中存放项目依赖jar的lib目录中并没有本地引入的推送相关的jar包。折腾了半个小时,才解决了问题。解决后才发现,其实是对maven这块的基础知识有点遗忘了。这里总结下springboot项目引入本地jar包,如何通过maven打包将jar打进lib文件夹中:

1.首先在resources目录下创建一个lib文件夹,将需要的jar包放入lib文件夹中

在这里插入图片描述

2.在pom.xml文件中,添加依赖坐标。注意:这里由于导入的本地jar,所以< dependency></ dependency>标签中需要添加

<dependency>
	...
	<!-- 表示当前jar是外部引入的,maven不会在repository查找它 -->
	<scope>system</scope>
	<!-- 指定引入的外部jar存放的路径,一般将jar包放在项目的某个目录下,通过相对路径指定 -->
	<systemPath>...</systemPath>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

在这里插入图片描述

3.在pom的< build></ build>标签中还需进行如下修改

<build>
	<finalName>xxxxxx</finalName>
	<plugins>
		<!--配置将第三方jar打进jar包中,跟<packaging>jar</packaging>配合,如果不写,springboot默认是打成jar包-->
		<!--<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<!-- 项目中单独引入第三方jar时,includeSystemScope值要为true 
					<includeSystemScope>true</includeSystemScope>
				</configuration>
			</plugin>-->
			
		<!-- 打war包 -->
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-war-plugin</artifactId>
			<version>3.2.2</version>
			<configuration>
				<webResources>
					<!-- 配置将第三方jar打进war包中,跟<packaging>war</packaging>配合 -->
					<webResource>
						<directory>${pom.basedir}/src/main/resources/lib/</directory>
						<targetPath>WEB-INF/lib/</targetPath>
						<includes>
							<include>**/*.jar</include>
						</includes>
					</webResource>
				</webResources>
			</configuration>
		</plugin>
	</plugins>
</build>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

根据项目需要,打成jar包部署或war包部署,需要引入不同的插件,添加不同的配置。如果需要打成war包,不要忘了在pom.xml文件中添加< packaging >war</ packaging>配置。

4.打包部署后测试验证

可以通过调用相关接口测试,也可直接观察部署包中lib文件夹中是否存在引入的本地jar来判定

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/308108
推荐阅读
相关标签
  

闽ICP备14008679号