springboot如何把下载的外部jar包导入maven打包(package)

下载jar包并导入

一般是在maven网站上直接搜索需要的jar包下载,并在pom中导入依赖

有时候需要添加systemPath来指定jar包的位置,让springboot引用

fastjson,因为是maven中下载的包,所以要指定scope和systemPath

	<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>fastjson</artifactId>
		<version>1.2.75</version>
		<scope>system</scope>
		<systemPath>${pom.basedir}/src/main/resources/lib/fastjson-1.2.75.jar</systemPath>
	</dependency>

pom.basedir还有project.basedir 也有直接basedir,具体出了问题搜bug就好
Maven: Maven的六类属性

下载好的jar包放在设置为resources的包下,一般是另外在下面建立一个lib表示外部导入
下载好的jar

pom配置打包环境

注意依赖和插件是加在不同地方的

依赖<dependencies>中

<!--maven打包插件-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
			<version>2.6.3</version>
		</dependency>

<!--maven默认插件,用于编译源码-->
		<dependency>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<version>3.8.1</version>
		</dependency>

插件<build>中

忽视lombok插件

<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
			<configuration>

				<includeSystemScope>true</includeSystemScope>

				<excludes>
					<exclude>
						<groupId>org.projectlombok</groupId>
						<artifactId>lombok</artifactId>
					</exclude>
				</excludes>
			</configuration>
		</plugin>
	</plugins>
</build>

解释:

  • spring-boot-maven-plugin是打包插件,引入这个插件之后用maven打包出来的jar包才是可执行的完整jar包

  • 而includeSystemScope>true</includeSystemScope是设置打包的时候是否包括外部引入的jar包

      <build>
      	<plugins>
      		<plugin>
      			<groupId>org.springframework.boot</groupId>
      			<artifactId>spring-boot-maven-plugin</artifactId>
      			<configuration>
    
      				<includeSystemScope>true</includeSystemScope>
    
      				<excludes>
      					<exclude>
      						<groupId>org.projectlombok</groupId>
      						<artifactId>lombok</artifactId>
      					</exclude>
      				</excludes>
      			</configuration>
      		</plugin>
      	</plugins>
      </build>
    

外部引入jar包

这里以阿里的fastJson为例

同时,需要在pom中引入依赖

	<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>fastjson</artifactId>
		<version>1.2.75</version>
		<scope>system</scope>
		<systemPath>${pom.basedir}/src/main/resources/lib/fastjson-1.2.75.jar</systemPath>
	</dependency>

注意:

  • scope>system</scope必须设置system,这里和上面的includeSystemScope>true</includeSystemScope呼应
  • systemPath>${pom.basedir}/src/main/resources/lib/fastjson-1.2.75.jar</systemPath中的路径必须指明引入的jar包

clean package

打包之后,点开jar包
打包之后

搜索lib
在jar包中找到了fastJson包

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值