我们需要执行以下步骤:
- 首先,打开命令提示符(CMD)或 PowerShell,执行以下命令:
bash
mvn install:install-file ^
-Dfile="你的jar包路径" ^
-DgroupId="组织ID" ^
-DartifactId="项目ID" ^
-Dversion="版本号" ^
-Dpackaging=jar
例如,如果你的 jar 包在 D:\mylib.jar
,你可以这样执行:
bash
mvn install:install-file ^
-Dfile="D:\scylla.jar" ^
-DgroupId=com.iflytek ^
-DartifactId=scylla ^
-Dversion=1.0.0 ^
-Dpackaging=jar ^
-DgeneratePom=true ^
-DlocalRepositoryPath="D:\repository"
- 安装成功后,在你的项目 pom.xml 中添加依赖:
xml
<dependency>
<groupId>com.example</groupId>
<artifactId>mylib</artifactId>
<version>1.0.0</version>
</dependency>
具体步骤:
-
确保已安装 Maven
- 在命令行输入
mvn -version
确认 Maven 已安装 - 如果没有安装,需要先下载安装 Maven
- 在命令行输入
-
准备参数信息
groupId
: 通常是公司或组织的反向域名(如:com.example)artifactId
: jar 包的名称(如:mylib)version
: 版本号(如:1.0.0)file
: jar 包的完整路径
-
执行安装命令
bashmvn install:install-file ^ -Dfile="jar包路径" ^ -DgroupId="组织ID" ^ -DartifactId="项目ID" ^ -Dversion="版本号" ^ -Dpackaging=jar
-
验证安装
- 安装后,jar 包会被复制到 Maven 本地仓库
- 默认位置:
C:\Users\你的用户名\.m2\repository
- 或者你指定的 Maven 仓库位置
-
在项目中使用
- 在项目的 pom.xml 中添加依赖配置
- 刷新 Maven 项目(在 IDE 中右键项目 -> Maven -> Reload Project)
如果你需要指定自定义的 Maven 仓库位置,可以添加 -DlocalRepositoryPath
参数:
bash
mvn install:install-file ^
-Dfile="D:\scylla.jar" ^
-DgroupId=com.iflytek ^
-DartifactId=scylla ^
-Dversion=1.0.0 ^
-Dpackaging=jar ^
-DgeneratePom=true ^
-DlocalRepositoryPath="D:\repository"
- 修改 systemPath 的写法:
xml
<dependency>
<groupId>com.iflytek</groupId>
<artifactId>scylla</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<!-- 使用更可靠的写法 -->
<systemPath>${basedir}/src/main/resources/jar/scylla.jar</systemPath>
</dependency>
- 或者使用绝对路径测试:
xml
<dependency>
<groupId>com.iflytek</groupId>
<artifactId>scylla</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>D:\code3\CorpKnowledgeBase\spark-knowledge\knowledge-web\src\main\resources\jar\scylla.jar</systemPath>
</dependency>
- 检查文件位置:
bash
# 创建目录(如果不存在)
mkdir -p src/main/resources/jar
# 确保 jar 包在正确位置
copy scylla.jar src\main\resources\jar\
- 刷新 Maven:
- 右键点击项目
- 选择 Maven -> Reload Project
- 或者在命令行执行:
bash
mvn clean install -U
- 验证 Maven 属性 :
你可以添加一个插件来打印实际路径:
xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>project.basedir: ${project.basedir}</echo>
<echo>System Path: ${project.basedir}/src/main/resources/jar/scylla.jar</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
- 完整的 pom.xml 示例:
xml
<project>
<!-- 其他配置 -->
<properties>
<lib.dir>${project.basedir}/src/main/resources/jar</lib.dir>
</properties>
<dependencies>
<dependency>
<groupId>com.iflytek</groupId>
<artifactId>scylla</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${lib.dir}/scylla.jar</systemPath>
</dependency>
<!-- 其他依赖 -->
</dependencies>
</project>
maven内置属性:
cpp
${project.basedir} <!-- 项目根目录,即包含pom.xml文件的目录 -->
${project.build.directory} <!-- 构建目录,默认为 target -->
${project.version} <!-- 项目版本 -->
${project.groupId} <!-- 项目的groupId -->
${project.artifactId} <!-- 项目的artifactId -->