Maven项目中使用本地jar包的两种方法详解
作者:mnyc2011
方法一:安装到本地 Maven 仓库(如果知道jar包中的groupId、artifactId、version)
1.使用 Maven 命令安装
# 基本命令 mvn install:install-file \ -Dfile=/path/to/your.jar \ -DgroupId=com.example \ -DartifactId=custom-library \ -Dversion=1.0.0 \ -Dpackaging=jar # 实际示例(Windows) mvn install:install-file ^ -Dfile=D:\libs\custom-1.0.0.jar ^ -DgroupId=com.company ^ -DartifactId=custom-sdk ^ -Dversion=1.0.0 ^ -Dpackaging=jar # 实际示例(Linux/Mac) mvn install:install-file \ -Dfile=~/libs/alipay-sdk-java.jar \ -DgroupId=com.alipay \ -DartifactId=alipay-sdk \ -Dversion=4.35.0.ALL \ -Dpackaging=jar
2.在 pom.xml 中引用(和普通的引用一样)
<dependency>
<groupId>com.company</groupId>
<artifactId>custom-sdk</artifactId>
<version>1.0.0</version>
</dependency>3.使用 IDEA 图形界面安装
方法二:使用 system scope(简单但不推荐生产)
1.将 jar 包放入项目目录
project-root/
├── src/
├── pom.xml
└── libs/ # 创建libs目录存放jar包
├── custom-1.0.0.jar
└── other-lib.jar
2.pom.xml 配置
<dependencies>
<!-- system scope 方式 -->
<dependency>
<groupId>com.custom</groupId>
<artifactId>custom-lib</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/libs/custom-1.0.0.jar</systemPath>
</dependency>
<!-- 多个jar包的示例 -->
<dependency>
<groupId>com.alipay</groupId>
<artifactId>alipay-sdk</artifactId>
<version>4.35.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/libs/alipay-sdk-java-4.35.0.ALL.jar</systemPath>
</dependency>
<dependency>
<!-- 这种添加方式groupId和artifactId不需要和jar包中一致,自己能区分就行-->
<groupId>com.jty</groupId>
<artifactId>com.jty.rfid</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<!--project.basedir:这个地方虽然是 project.basedir,但是实际上是模块目录-->
<systemPath>${project.basedir}/libs/rfid/comprehensive(20240827).jar</systemPath>
</dependency>
</dependencies>3.打包时需要额外配置
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!-- 告诉编译器system scope的jar位置 -->
<compilerArguments>
<extdirs>${project.basedir}/libs</extdirs>
</compilerArguments>
</configuration>
</plugin>
<!-- 打包时需要包含system scope的jar -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>方法补充
下面小编和大家整理了一些Maven中使用本地jar包的其他方法,希望对大家有所帮助
1.使用 mvn install:install-file 安装到本地仓库(推荐)
这是最推荐的方式,它能够最大程度保持 Maven 项目的整洁性与可维护性。
操作步骤:
- 打开命令行工具。
- 执行以下命令,将本地 JAR 包安装到 Maven 的本地仓库中:
mvn install:install-file \ -Dfile=/path/to/your.jar \ -DgroupId=com.example \ -DartifactId=your-artifact \ -Dversion=1.0.0 \ -Dpackaging=jar
请将 /path/to/your.jar 替换为你的 JAR 包实际路径。
在 pom.xml 中添加依赖:
<dependency> <groupId>com.example</groupId> <artifactId>your-artifact</artifactId> <version>1.0.0</version> </dependency>
优点:
- 与其他依赖使用方式一致;
- 支持依赖传递;
- 更适合多人协作和构建工具集成;
2.使用 system 作用域直接引用本地路径(不推荐)
此方法不需要安装到本地仓库,直接在 pom.xml 中配置 JAR 包的路径。
示例配置:
<dependency>
<groupId>com.example</groupId>
<artifactId>your-artifact</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/your.jar</systemPath>
</dependency>注意:${project.basedir} 表示项目根目录,推荐将 JAR 包放在 lib 目录中统一管理。
缺点:
- 无法参与依赖传递;
- 路径硬编码,不利于跨平台、多人协作;
- Maven 官方不推荐使用。
注意:
如打包需要如下配置:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>| 方法 | 推荐程度 | 特点 |
|---|---|---|
| install-file 安装到本地仓库 | ⭐⭐⭐⭐⭐ | 规范、支持依赖传递、适合长期使用 |
| system 作用域引用 | ⭐⭐ | 简单快捷,但不规范、不推荐 |
3.直接配置pom.xml文件
通过修改pom.xml文件,使用<scope>system</scope>和<systemPath>标签直接引用本地JAR包。这种方案适合临时性引用,但需要手动维护路径。
配置步骤
在pom.xml的<dependencies>中添加依赖:
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>my-local-lib</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/my-local-lib-1.0.0.jar</systemPath>
</dependency>
</dependencies>验证依赖:
- 执行
mvn clean package - 检查
target/lib目录是否包含该JAR包
注意事项
- 使用
${project.basedir}/开头确保路径通用性 system范围依赖需手动维护路径,不适合团队协作
4.IDE图形化配置(以IntelliJ IDEA为例)
对于使用IDE开发的场景,可以通过图形界面快速添加本地依赖。
配置步骤
打开项目结构:File → Project Structure → Modules → Dependencies
添加本地JAR包:
- 点击
+号选择JARs or directories - 选中本地JAR包,设置依赖范围为
Compile
IDEA会自动生成pom.xml配置:
<dependency>
<groupId>com.example</groupId>
<artifactId>my-local-lib</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/my-local-lib-1.0.0.jar</systemPath>
</dependency>到此这篇关于Maven项目中使用本地jar包的两种方法详解的文章就介绍到这了,更多相关Maven引入本地JAR包内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
