有时我们要用的 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. QString, string, int, char* 之间相互转换

    这三种数据类型在实际运用中经常需要互相转换,那么这里小结下它们之间的转换方法: - Qstring & string Qt中封装的类十分强大,其成员函数数量之多比STD有过之而无不及,许多程序 ...

  2. [原创翻译]Protocol Buffer Basics: C#

    Protocol Buffer 基础知识:c#    原文地址:https://developers.google.com/protocol-buffers/docs/csharptutorial   ...

  3. 基于Xenomai的实时Linux分析与研究

    转自:http://blog.csdn.net/cyberlabs/article/details/6967192 引 言 随着嵌入式设备的快速发展,嵌入式设备的功能和灵活性要求越来越高,很多嵌入式设 ...

  4. C++标准库:std_map作为一个关联数组

    摘要:std::map作为一个容器存在一个典型应用就是作为关联数组来作用.在诸如Java等等语言中,关联数组广泛存在.std::map是一个容器,在它的概念框架中存在两个词:键和值,std::map把 ...

  5. python命令行下安装redis客户端

    1. 安装文件: https://pypi.python.org/pypi/setuptools 直接下载然后拷贝到python目录下同下面步骤 下载 ez_setup.py>>> ...

  6. Warning: in_array() expects parameter 2 to be array, string given in D:\wamp\www\suiji.php on line 26

    可参考上篇文章  链接 PHP数组简写模式 同样是PHP版本问题:in_array()函数 第二个参数应该为数组 但是 lnmp下,PHP5.3不报错,wamp下PHP5.5报以下错误: echo & ...

  7. UI: 多窗口

    1.自定义帮助类,用于简化 SecondaryView 的管理UI/MultipleViews/SecondaryViewHelper.cs /* * SecondaryViewHelper - 自定 ...

  8. UI: 标题栏

    TitleBarDemo.xaml <Page x:Class="Windows10.UI.TitleBarDemo" xmlns="http://schemas. ...

  9. Bzoj3144 [Hnoi2013]切糕

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1494  Solved: 818 Description Input 第一行是三个正整数P,Q,R,表 ...

  10. U盘因为装linux系统变小了

    U盘在Windows下被UltraISO等软件制作成Linux启动盘后会自动被格式化成FAT格式,导致容量变小. 用DiskGenius去修复 http://www.jb51.net/softs/75 ...