有时我们要用的 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 仓库的环境变量来消除这个警告,如果你觉得有这个必要。

向Maven的本地库中添加jar文件的更多相关文章

  1. 如何在maven中添加jar包

    Maven 中央仓库地址: 1. http://www.sonatype.org/nexus/ 2. http://mvnrepository.com/ (本人推荐仓库) 3. http://repo ...

  2. maven 如何给web项目添加jar包依赖

      maven 如何给web项目添加jar包依赖 CreateTime--2018年4月19日19:06:21 Author:Marydon 开发工具:eclipse 1.打开pom.xml文件--& ...

  3. 怎样在nexus 中 搜索到远程maven仓库中的jar 文件

    怎样在nexus 中 搜索到远程maven仓库中的jar 文件 url: http://www.oschina.net/question/95712_21999 点击Administration菜单下 ...

  4. 如何在IOS开发中在自己的framework中添加.bunble文件

    今天就跟大家介绍一下有关,如何在IOS开发中在自己的framework中添加.bunble文件,该文章我已经在IOS教程网(http://ios.662p.com)发布过来,个人觉得还是对大家有帮助的 ...

  5. ASP.NET在主题中添加CSS文件

    ASP.NET在主题中添加CSS文件 在ASP.NET中,可以使用CSS来控制页面上HTML元素和ASP.NET控件的皮肤.如果在主题文件夹中添加了CSS文件,则在页面应用主题时也会自动应用CSS. ...

  6. Xcode7 Xcode6 中添加pch文件

    在Xcode7 和 Xcode6 中添加.pch文件是一样的,具体操作图文如下: 第一步:在Xcode的项目里,一般在Supporting Files 文件夹下创建,选中Supporting File ...

  7. cmd中执行jar文件命令(待参数)

    cmd中执行jar文件命令(待参数) 1,jar文件路径:F:\products 2,cmd命令: --两个日期参数(空格隔开) java -jar F:\products\analysis.jar ...

  8. 记录自己在 cmd 中执行 jar 文件遇到的一些错误

    记录自己在 cmd 中执行 jar 文件遇到的一些错误 场景: 请求接口,解析接口返回的 JSON 字符串并插入到我们的数据库里面. 情况: 项目在 eclipse 中正常运行,打成 jar 包后在 ...

  9. 如何在maven项目的pom.xml文件中添加jar包

    在使用maven进行项目开发时,我们需要在pom.xml文件中添加自己所需要的jar包.这就要求我们获取jar包的groupId和artifactId. 我们可以在一些maven仓库上搜索我们所需要的 ...

随机推荐

  1. 备忘:maven 中指定版本

    <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> ...

  2. Django用已有的数据库

    python mysite/manage.py inspectdb  会按照你的数据库生成Model  拷贝进去用就可以了!

  3. Mongo运行错误:Failed to connect 127.0.0.1:27017,reason:errno:10061由于目标计算机积极拒绝,无法连接

    Mongo运行错误:如下 原因是mongodb的服务没有开启,开启服务后问题就能解决了,开启服务命令:mongod --dbpath "d:\mongodb\data 只要操作mongodb ...

  4. Mvc中域的添加和不同域之间的跳转

    一.在新添加的域中中的 AreaRegistration中作如下设置: 二.在原来的Global.asax中设置: 三.不同域之间的跳转 @Url.Action("Index", ...

  5. python基础回顾1

    定义 tuple(元组), list (表) #!/usr/bin/env python # encoding: utf-8 a = 10 #定义一直变量,无需声明 s1 = (2,1.3,'love ...

  6. Servlet实现asp.net中的Global.asax启动事件(Servlet和Listener使用)

    1.Java Web中没有像asp.net的全局启动事件,但是可以通过web.xml中的load-on-startup节点来控制Servlet的开机启动和启动次数.web.xml详细配置参考:http ...

  7. Asynchronous fs.stat.isDirectory()

    function showFile() { for(var i = 0; i< files.length; i++){ var itemFile = files[i]; fs.stat(__di ...

  8. HTML5 图片缩放功能

    腾讯新闻上用的插件(xw.qq.com) 缩放插件scale.js (function(window, undefined) { var document = window.document, sup ...

  9. hibernate-cascade级联关系

    <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBL ...

  10. Wifi代替数据线调试

    我们通常都是使用USB线连接设备和电脑进行的程序调试. 有的程序本身就要使用USB口,例如一个文件浏览器,它可以列出通过USB接口接入的U盘. 在调试这样的应用的时候,USB接口被占用了,但设备也只有 ...