如何将jar 包下载到自定义maven仓库

下载命令

mvn install:install-file -Dfile=artifactid-version.jar -DgroupId=groupid -DartifactId=artifactid -Dversion=version -Dpackaging=jar -DlocalRepositoryPath=. -DcreateChecksum=true

参数解释

在上述命令中,需要替换以下参数:

  • artifactid-version.jar:JAR包的文件名,例如:my-library-1.0.0.jar
  • groupid:JAR包的groupId,可以根据项目的命名规范或自定义。
  • artifactid:JAR包的artifactId,可以根据项目的命名规范或自定义。
  • version:JAR包的版本号,例如:1.0.0
  • -DlocalRepositoryPath=.:指定Maven本地仓库的路径为当前目录。
  • -DcreateChecksum=true 是Maven安装命令中的一个可选参数,用于在安装JAR包到本地仓库时生成校验和(checksum)文件。校验和是通过对文件内容进行哈希计算得到的固定长度的值,用于验证文件的完整性。生成校验和文件可以帮助确保安装的JAR包在传输过程中没有被篡改或损坏。

例子

<?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>org.example</groupId>
    <artifactId>Add</artifactId>
    <version>1.0.0</version>

    <repositories>
        <repository>
            <id>repository</id>
            <url>D:/DEVELOPMENT/maven/repository</url>
        </repository>
    </repositories>

</project>

以上是一个maven 工程项目,在项目中我们可以自己写一些工具例子,再将其编译,打包,下载到自己的仓库中,在执行下载命令的过程中,会出现两种情况,就是关于仓亏地址的问题。

  • 下载到本地默认仓库地址 “C:\Users\Acer.m2\repository”

    mvn install:install-file -Dfile="Add-1.0.0.jar" -DgroupId="org.example" -DartifactId="Add" -Dversion="1.0.0" -Dpackaging=jar
    
  • 下载到自定义的maven仓库地址"D:/DEVELOPMENT/maven/repository"

    mvn install:install-file -Dfile="Add-1.0.0.jar" -DgroupId="org.example" -DartifactId="Add" -Dversion="1.0.0" -Dpackaging=jar  -DlocalRepositoryPath="D:/DEVELOPMENT/maven/repository"
    

关键的区别在于是否指定路径 -DlocalRepositoryPath=“D:/DEVELOPMENT/maven/repository”,指定路径就会下载到我们想要的路径下面。
在这里插入图片描述

接下来我们就可以在另外一个项目中引用我们自定义的jar 包中的工具类了。

<?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>org.chafan</groupId>
    <artifactId>fast_maven</artifactId>
    <version>fast_1.0</version>

    <dependencies>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>Add</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>
</project>

猜你喜欢

转载自blog.csdn.net/weixin_45833112/article/details/131709646
今日推荐