手动安装jar包到本地maven仓库,并且包含依赖信息

手动安装jar包到本地maven仓库,并且包含依赖信息

maven官方文档

http://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html


Guide to installing 3rd party JARs

安装第三方JAR的指南


Although rarely, but sometimes, you will have 3rd party JARs that you need to put in your local repository for use in your builds, since they don’t exist in any public repository like Maven Central. The JARs must be placed in the local repository in the correct place in order for it to be correctly picked up by Apache Maven.

尽管很少,但有时,您将需要将第三方JAR放入您的本地存储库以在构建中使用,因为它们在Maven Central之类的任何公共存储库中都不存在。 必须将JAR放置在本地存储库中的正确位置,以便Apache Maven可以正确拾取JAR。


To make this easier, and less error prone, we have provided an install-file goal in the maven-install-plugin which should make this relatively painless.

为了简化操作并减少错误的发生,我们在maven-install-plugin中提供了一个install-file目标, 应该使这个相对轻松。


To install a JAR in the local repository use the following command:

要在本地存储库中安装JAR,请使用以下命令:

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

If there’s a pom-file as well, you can install it with the following command:

如果还有pom文件,则可以使用以下命令进行安装:

mvn install:install-file -Dfile=<path-to-file> -DpomFile=<path-to-pomfile>

With version 2.5 of the maven-install-plugin, it can get even simpler: if the JAR was built by Apache Maven, it’ll contain a pom.xml in a subfolder of the META-INF/ directory, which will be read by default. In that case, all you need to do is:

使用maven-install-plugin的2.5版本,它甚至可以变得更加简单:如果JAR是由Apache Maven构建的,则它将在META-INF/目录的子文件夹中包含pom.xml,该文件将被默认读取。在这种情况下,您需要做的只是:

mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=<path-to-file>

前期准备

  1. 准备一个jar包:demo-1.0.jar
  2. 定义该jar包安装的到本地的坐标:

groupId:xin.yangshuai

artifactId:maven-dependent-demo

version:1.0-SNAPSHOT

  1. 如果有可能,还会准备一个与jar包相对应的pom文件,例如:1.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>xin.yangshuai</groupId>
    <artifactId>maven-dependent-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.71</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.8.1</version>
        </dependency>
    </dependencies>
</project>
  • 如果该jar包是maven构建的,则这个pom文件就是开发时的pom.xml文件,jar包的META-INF/目录的子文件夹中包含这个pom.xml
  • 如果是其它方式构建的,就手动创建一个pom文件,写上坐标和依赖

手动安装

  1. 只安装jar包,不关心该jar包的依赖信息

    mvn install:install-file -DgroupId=xin.yangshuai -DartifactId=maven-dependent-demo -Dversion=1.0-SNAPSHOT -Dpackaging=jar -Dfile=demo-1.0.jar
    
    • 此时,jar包已经安装到本地仓库中,并且文件名为maven-dependent-demo-1.0-SNAPSHOT.jar,该jar包实际上是demo-1.0.jar改了个名
    • 同目录的maven-dependent-demo-1.0-SNAPSHOT.pom中只有坐标信息,没有依赖信息,该文件是新生成的
    • 没有依赖信息会带来一个问题:项目中的pom文件引用该jar包的坐标时,不会自动引入该jar包所依赖的jar包(因为并不知道该引入什么jar包),需要手动引入。
  2. 安装jar包,并且声明相关依赖信息

    mvn install:install-file -Dfile=demo-1.0.jar -DpomFile=1.xml
    
    • 读取1.xml文件中的坐标信息
    • 本地仓库安装目录的maven-dependent-demo-1.0-SNAPSHOT.pom中有坐标信息也有依赖信息,该文件实际上是1.xml改了个名和后缀
  3. 使用2.5版本的插件

    mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=demo-1.0.jar
    
    • 实际是方式1的简化,该命令也没有声明依赖信息

项目中使用

<dependency>
    <groupId>xin.yangshuai</groupId>
    <artifactId>maven-dependent-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值