把jar包放入自己的maven项目中

Administrator
发布于 2025-08-05 / 12 阅读
0
0

把jar包放入自己的maven项目中

✅ 方式一:安装到本地 Maven 仓库

推荐使用这个方式,干净且 Maven 自动管理依赖。

命令如下:

mvn install:install-file \
  -Dfile=/路径/your-lib.jar \
  -DgroupId=com.example \
  -DartifactId=your-lib \
  -Dversion=1.0.0 \
  -Dpackaging=jar

示例(Windows):

mvn install:install-file ^
  -Dfile=C:\libs\mytool.jar ^
  -DgroupId=com.mycompany ^
  -DartifactId=mytool ^
  -Dversion=1.0.0 ^
  -Dpackaging=jar

然后在 pom.xml 中引入:

<dependency>
    <groupId>com.mycompany</groupId>
    <artifactId>mytool</artifactId>
    <version>1.0.0</version>
</dependency>

✅ 方式二:直接放入项目中使用(不推荐)

  1. .jar 文件放到项目目录下,例如:

    your-project/
    └── libs/
        └── my-lib.jar
  2. 修改 pom.xml,手动添加:

<dependency>
    <groupId>com.example</groupId>
    <artifactId>my-lib</artifactId>
    <version>1.0.0</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/libs/my-lib.jar</systemPath>
</dependency>

⚠️ 注意:

  • scope=system 不推荐使用,Maven 3.2+ 以后就不提倡这种方式。

  • 这种方式不支持打包传递依赖。


评论