如何把本地jar包添加到Maven项目?
2018-07-13 本文已影响0人
Duskalbatross
需求
如果要把本地的jar包添加到Maven项目,之前一直可以使用下面的依赖,但是高版本的maven却报了warning。Maven是想尽一切办法让你使用本地仓库,所以这种方法已经不推荐了,可以使用mvn install:install-file
命令安装jar包到本地仓库。
<dependency>
<groupId>net.paoding.analysis</groupId>
<artifactId>paoding-analysis</artifactId>
<version>2.0.4</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/paoding-analysis.jar</systemPath>
</dependency>
warning信息:
[WARNING] 'dependencies.dependency.systemPath' for net.paoding.analysis:paoding-analysis:jar should not point at files within the project directory, ${project.basedir}/lib/paoding-analysis.jar will be unresolvable by dependent projects @ line 71, column 25
[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.
安装Jar包到本地Maven仓库
Install the JAR into your local Maven repository as follows:
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
示例
例如:如果要把paoding-analysis.jar安装到mvn,需要执行以下命令:
mvn install:install-file -Dfile=./lib/paoding-analysis.jar -DgroupId=net.paoding -DartifactId=paoding-analysis -Dversion=2.0.4 -Dpackaging=jar
查看日志,mvn会把jar包安装到这个目录/Users/lyh/.m2/repository/net/paoding/paoding-analysis/2.0.4/
其实,如果熟悉maven的目录结构,可以直接把jar包复制到响应的目录下,不用执行上述命令,效果一样。
[INFO] --- maven-install-plugin:2.4:install-file (default-cli) @ bigdata-mr ---
[INFO] Installing /Users/lyh/dev/src/hadoop/MapReduce/lib/paoding-analysis.jar to /Users/lyh/.m2/repository/net/paoding/paoding-analysis/2.0.4/paoding-analysis-2.0.4.jar
[INFO] Installing /var/folders/91/_fj2gxc54t1g796srzhmc1r80000gn/T/mvninstall505131428054290220.pom to /Users/lyh/.m2/repository/net/paoding/paoding-analysis/2.0.4/paoding-analysis-2.0.4.pom
之后在本地系统,如果需要使用paoding-analysis.jar,pom.xml加入下面依赖,然后就可以像使用其它包一样使用这个包了。
<dependency>
<groupId>net.paoding</groupId>
<artifactId>paoding-analysis</artifactId>
<version>2.0.4</version>
</dependency>
参考
Guide to installing 3rd party JARs
I have a jar that I want to put into my local repository. How can I copy it in?