向Maven的本地库中添加jar文件

有时我们要用的 maven 依赖项在官方repo库中找不到,然而我们从其他渠道获得了依赖项中的所有jar文件,本文记录了如何向本地库添加jar文件。

从复杂到简单,有三种方法:

  1. 使用 maven 的仓库管理器(例如Nexus)来架设一个本地仓库服务器
  2. 使用指令 mvn install:install-file 将jar文件安装到本地仓库
  3. 通过项目pom配置文件引入

第一种方法有利于团队开发,内容多一点,我打算单独用一篇文章记录。这里介绍其他两种方法:

 

使用指令 mvn install:install-file 将jar文件安装到本地仓库

语法规范:

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging> -DgeneratePom=true Where: <path-to-file> the path to the file to load <group-id> the group that the file should be registered under <artifact-id> the artifact name for the file <version> the version of the file <packaging> the packaging of the file e.g. jar

我们以文件<not-yet-commons-ssl-0.3.17.jar>为例:

mvn install:install-file -Dfile=e:\not-yet-commons-ssl-0.3.17.jar -DgroupId=org.apache.commons -DartifactId=not-yet-commons-ssl -Dversion=0.3.17 -Dpackaging=jar -DgeneratePom=true

 

通过项目pom配置文件引入

编辑项目pom文件,在依赖项中增加条目,并指定<scope>和<systemPath>。

还是以文件<not-yet-commons-ssl-0.3.17.jar>为例:

<dependency> <groupId>org.apache.commons</groupId> <artifactId>not-yet-commons-ssl</artifactId> <version>0.3.17</version> <scope>system</scope> <systemPath>e:\not-yet-commons-ssl-0.3.17.jar</systemPath> </dependency>

等等,编译后出现一个警告:

[WARNING] 'dependencies.dependency.systemPath' for org.apache.commons:not-yet-commons-ssl:jar should use a variable instead of a hard-coded path e:\not-yet-commons-ssl-0.3.17.jar @ line 35, column 16
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.

原来是 maven 苦口婆心地告诉我们不要使用诸如 C:\Windows 这样的绝对路径,而应使用 ${java.home} 这样的相对路径(变量),否则降低了编译可重现性,并威胁我们以后可能不再支持这种方式。

嗯,我们可以建立一个 maven 仓库的环境变量来消除这个警告,如果你觉得有这个必要。

posted @ 2015-11-30 10:27  Gugia桑  阅读(3222)  评论(0编辑  收藏  举报