java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Maven引入本地JAR包

Maven项目中使用本地jar包的两种方法详解

作者:mnyc2011

这篇文章主要为大家详细介绍了Maven项目中使用本地jar包的两种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下

方法一:安装到本地 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 项目的整洁性与可维护性。

操作步骤:

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 目录中统一管理。

缺点:

注意:

如打包需要如下配置:

<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>

验证依赖:

注意事项

4.IDE图形化配置(以IntelliJ IDEA为例)

对于使用IDE开发的场景,可以通过图形界面快速添加本地依赖。

配置步骤

打开项目结构:File → Project Structure → Modules → Dependencies

添加本地JAR包:

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包内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文