maven导入本地jar包
方法 1:使用 systemPath 引入外部依赖
如果您希望直接引用项目中的 JAR 文件,可以使用 systemPath。
- 修改 pom.xml
在 pom.xml 中添加以下依赖配置:
<dependency>
<groupId>com.jdwx</groupId>
<artifactId>sms</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/java/lib/sms-1.0.jar</systemPath>
</dependency>
方式二
手动安装到本地 Maven 仓库
将外部的 JAR 文件安装到本地 Maven 仓库中,然后像普通依赖一样引用。
安装命令:
mvn install:install-file -Dfile=path/to/external-lib-1.0.jar -DgroupId=com.example -DartifactId=external-lib -Dversion=1.0 -Dpackaging=jar
以我个人电脑为例
mvn install:install-file -Dfile=D:\devtools\maven1\maven_pro\sms-jar -DgroupId=com.jdwx -DartifactId=sms -Dversion=1.0 -Dpackaging=jar
运行截图为
发现还是爆红,于是打开idea
配置了maven的本地仓库,于是配置
mvn install:install-file -Dfile=D:\devtools\maven1\maven_pro\sms-1.0.jar -DgroupId=com.jdwx -DartifactId=sms -Dversion=1.0 -Dpackaging=jar -DlocalRepositoryPath=D:\devtools\maven1\maven_pro
继续执行
查看本地仓库有jar包,配置成功!
3. 使用私有远程仓库
将外部的 JAR 文件上传到私有 Maven 仓库(如 Nexus 或 Artifactory),然后在 pom.xml 中配置仓库地址并引用。
配置示例:
在 pom.xml 中添加私有仓库配置:
<repositories>
<repository>
<id>my-repo</id>
<url>http://my-repo-url</url>
</repository>
</repositories>
添加依赖配置:
<dependency>
<groupId>com.example</groupId>
<artifactId>external-lib</artifactId>
<version>1.0</version>
</dependency>
4.使用 maven-dependency-plugin 复制 JAR 文件
通过 Maven 插件将外部的 JAR 文件复制到项目的 target 目录,并在构建时引用。
配置示例:
在 pom.xml 中添加插件配置:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>copy-external-lib</id>
<phase>initialize</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.example</groupId>
<artifactId>external-lib</artifactId>
<version>1.0</version>
<type>jar</type>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
在 pom.xml 中添加依赖配置:
**
<dependency>
<groupId>com.example</groupId>
<artifactId>external-lib</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.build.directory}/lib/external-lib-1.0.jar</systemPath>
</dependency>
**