现在,仍然有很多的项目jar不能够在maven库里面找到,有的可能本身并不是maven项目,所以如何将一些我们需要或者自定义的jar添加到我们本地的仓库里面呢?

1.mvn intall

如果是你自己创建的maven项目,通过mvn install会自动编译,并且将jar添加到本地库里面。

如果不是maven项目,这时我们需要借助如下的命令:

1
2
3

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>

示例:

运行命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
C:\Users\duliu>mvn install:install-file -Dfile=D:\hello.jar -DgroupId=com.devnp.hello -DartifactId=h
ello -Dversion=1.0 -Dpackaging=jar
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-install-plugin:2.4:install-file (default-cli) @ standalone-pom ---
[INFO] Installing D:\hello.jar to D:\Program Files\apache-maven-3.1.0\repo\com\devnp\hello\hell\1.0\
hell-1.0.jar
[INFO] Installing C:\Users\duliu\AppData\Local\Temp\mvninstall3000998284107951309.pom to D:\Program
Files\apache-maven-3.1.0\repo\com\devnp\hello\hell\1.0\hell-1.0.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.022s
[INFO] Finished at: Sun Oct 23 21:39:24 SGT 2016
[INFO] Final Memory: 6M/245M
[INFO] ------------------------------------------------------------------------

查看pom.xml:

这个时候在我们本地的库里面就会有jar和对应的pom.xml文件信息:

1
2
3
4
5
6
7
8
9
10
11

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.devnp.hello</groupId>
<artifactId>hello</artifactId>
<version>1.0</version>
<description>POM was created from install:install-file</description>
</project>