maven下载私服jar

maven下载私服jar

私服是架设在局域网的一种特殊的远程仓库,目的是代理远程仓库及部署第三方构件。有了私服之后,当 Maven 需要下载构件时,直接请求私服,私服上存在则下载到本地仓库;否则,私服请求外部的远程仓库,将构件下载到私服,再提供给本地仓库下载。

我们可以使用专门的 Maven 仓库管理软件来搭建私服,比如:Apache ArchivaArtifactorySonatype Nexus。这里我们使用 Sonatype Nexus。

1、下载安装nexus

  • 下载nexus压缩包并解压
  • 通过cmd进入到bin目录
  • 执行nexus命令
  • 执行nexus install命令进行安装
  • 安装成功之后浏览器访问http://localhost:8081验证是否成功
  • 新用户默认账户,用户名:admin,密码:admin123可以进行登录

2、远程仓库认证

大部分公共的远程仓库无须认证就可以直接访问,但我们在平时的开发中往往会架设自己的Maven远程仓库,出于安全方面的考虑,我们需要提供认证信息才能访问这样的远程仓库。配置认证信息和配置远程仓库不同,远程仓库可以配置在settings.xml文件中,也可直接在pom.xml中配置,后面会分别举例说明,但是认证信息必须配置在settings.xml文件中。在settings.xml中配置认证信息更为安全。如下:在settings.xml中配置节点,用的账号为上面我们创建的账户。

<server>
    <!--此id保持一致-->
    <id>sfynexus</id>
    <username>admin</username>
    <password>admin123</password>
</server>

id是随便取的,但是后续还有id配置需要保持一致;username是用户名,password是密码

3、配置远程仓库

如果只有一个项目,可以配置在项目的pom文件中

 <repositories>
     <repository>
         <!--此id保持一致-->
         <id>sfynexus</id>
         <!--私服地址-->
         <url>http://127.0.0.1:8081/repository/maven-public/</url>
         <snapshots>
             <enabled>true</enabled>
         </snapshots>
         <releases>
             <enabled>true</enabled>
         </releases>
     </repository>
</repositories>

如果有多个项目,可以将如上配置放到maven的settings.xml文件中

<profile>
    <!--此id保持一致-->
    <id>sfynexus</id>
    <repositories>
        <repository>
            <id>sfy-nexus</id>
            <!--私服地址-->
            <url>http://127.0.0.1:8081/repository/maven-public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
  </profile>

repository:在repositories元素下,可以使用repository子元素声明一个或者多个远程仓库。

id:仓库声明的唯一id,尤其需要注意的是,Maven自带的中央仓库使用的id为central,如果其他仓库声明也使用该id,就会覆盖中央仓库的配置。

name:仓库的名称,让我们直观方便的知道仓库是哪个,暂时没发现其他太大的含义。

url:指向了仓库的地址,一般来说,该地址都基于http协议,Maven用户都可以在浏览器中打开仓库地址浏览构件。

releases和snapshots:用来控制Maven对于发布版构件和快照版构件的下载权限。需要注意的是enabled子元素,该例中releases的enabled值为true,表示开启JBoss仓库的发布版本下载支持,而snapshots的enabled值为false,表示关闭JBoss仓库的快照版本的下载支持。根据该配置,Maven只会从JBoss仓库下载发布版的构件,而不会下载快照版的构件。

4、配置仓库镜像

<mirror>
    <!--此id保持一致-->
    <id>sfynexus</id>
    <mirrorOf>*</mirrorOf>
    <!--私服地址-->
    <url>http://127.0.0.1:8081/repository/maven-public/</url>
</mirror>

5、激活远程仓库

<activeProfiles>
    <!--保持一致-->
    <activeProfile>sfynexus</activeProfile>
</activeProfiles>

至此,我们已经可以从自己搭建的私服下载jar了。

参考资料:https://www.cnblogs.com/huangwentian/p/9182819.html

记得快乐
原文地址:https://www.cnblogs.com/Y-wee/p/14472972.html