如何把本地jar包添加到Maven项目?

需求

如果要把本地的jar包添加到Maven项目,之前一直可以使用下面的依赖,但是高版本的maven却报了warning。Maven是想尽一切办法让你使用本地仓库,所以这种方法已经不推荐了,可以使用mvn install:install-file命令安装jar包到本地仓库。


    net.paoding.analysis
    paoding-analysis
    2.0.4
    system
    ${project.basedir}/lib/paoding-analysis.jar

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=
   -DgroupId=
   -DartifactId=
   -Dversion=
   -Dpackaging=
   -DgeneratePom=true

Where:   the path to the file to load
         the group that the file should be registered under
      the artifact name for the file
          the version of the file
        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加入下面依赖,然后就可以像使用其它包一样使用这个包了。


    net.paoding
    paoding-analysis
    2.0.4

参考

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?

你可能感兴趣的:(如何把本地jar包添加到Maven项目?)