使用 system 作用域(不推荐,但简单直接)
<dependency>
<groupId>com.test</groupId> <!-- 可自定义,建议与项目相关 -->
<artifactId>open-sdk</artifactId> <!-- 可自定义,建议体现JAR包功能 -->
<version>1.0.1</version> <!-- 版本号,与JAR包实际版本对应 -->
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/open-sdk-1.0.1-jar-with-dependencies.jar</systemPath>
</dependency>
说明:
<groupId>、<artifactId>、<version>
可根据实际情况自定义,但要保证唯一性和可读性。
<scope>system</scope>
表示从本地文件系统获取依赖,需配合<systemPath>
指定路径。
<systemPath>
中的 ${project.basedir} 是项目根目录变量,确保路径正确指向 lib 下的目标 jar 包。
不过这种方式有弊端,使用 system 作用域的依赖,Maven 默认不会将其打入最终的可执行 jar 包中,若要打包进去,还需在 pom.xml 的 标签内添加如下配置:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
方式二:安装到本地 Maven 仓库后引用(推荐)
先通过命令将 jar 包安装到本地 Maven 仓库:
mvn install:install-file -Dfile=src/main/resources/lib/open-sdk-1.0.4-jar-with-dependencies.jar -DgroupId=com.moredia -DartifactId=open-sdk -Dversion=1.0.4 -Dpackaging=jar
上述命令中:
-Dfile 后是 jar 包在项目中的实际路径。
-DgroupId、-DartifactId、-Dversion 自定义,与后续 pom 配置对应。
-Dpackaging 表示打包类型为 jar 。
然后需要在 pom.xml 中配置依赖:
<dependency>
<groupId>com.moredia</groupId>
<artifactId>open-sdk</artifactId>
<version>1.0.4</version>
</dependency>