基于nexus私服配置项目pom.xml和maven settings.xml文件

备注:搭建nexus私服请参考上一篇文章基于Docker搭建Maven私服Nexus,Nexus详解


一:将jar发送到nexus私服务器

1.pom.xml文件添加配置

 pom.xml文件中的这个版本标签对应结尾的(RELEASE、SNAPSHOT)将决定分配到私服的那个仓库<version>1.0.0-RELEASE</version>

    <!--配置上传到私服-->
    <distributionManagement>
        <repository>
            <!--id的名字可以任意取,但是在setting文件中的属性<server>的ID与这里一致-->
            <id>releases</id>
            <!--指向仓库类型为host(宿主仓库)的储存类型为Release的仓库-->
            <url>http://116.62.106.24:8081/repository/boris-release/</url>
        </repository>
        <snapshotRepository>
            <id>snapshots</id>
            <!--指向仓库类型为host(宿主仓库)的储存类型为Snapshot的仓库-->
            <url>http://116.62.106.24:8081/repository/boris-snapshot/</url>
        </snapshotRepository>
    </distributionManagement>

2.maven settings.xml文件添加配置  

 <server>
      <id>releases</id>
      <username>boris-test-nexus-repository</username>
      <password>boris</password>
    </server>
    <server>
      <id>snapshots</id>
      <username>boris-test-nexus-repository</username>
      <password>boris</password>
    </server>

然后执行命令:mvn deploy,就成功部署到私服环境了,如下是我测试的一个结果

PS:还有一种上传jar包的方式,就是直接去nexus管理界面手动上传

二:从nexus私服下载第三方jar包(有两种方式)推荐方式二

1.方式一(pom.xml的方式)

<repositories>
<repository>
<id>maven-nexus-group</id>
<url>http://116.62.106.24:8081/repository/boris-group/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>maven-nexus-group</id>
<url>http://116.62.106.24:8081/repository/boris-group/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>

由于nexus私服需要有用户名和密码登录才能访问,需要在maven settings.xml文件中配置,加入如下内容

    <server>
      <id>maven-nexus-group</id>
      <username>boris-test-nexus-repository</username>
      <password>boris</password>
    </server>

注意上面的server->id的值和pom.xml中repository->id的值一致,通过这个id关联找到凭证的。

2.方式二(镜像方式)推荐这种方式

maven settings.xml文件中配置

<server>
      <id>maven-nexus-group</id>
      <username>boris-test-nexus-repository</username>
      <password>boris</password>
 </server>


 <profile>
    <id>nexus</id>
    <repositories>
        <repository>
            <id>maven-nexus-group</id>
            <url>http://localhost:8081/nexus/content/groups/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                    <enabled>false</enabled>
                </snapshots>    
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>maven-nexus-group</id>
            <url>http://localhost:8081/nexus/content/groups/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
         </pluginRepository>
    </pluginRepositories>
</profile>
<!-- 激活 -->
<activeProfiles>
    <activeProfile>nexus</activeProfile>
</activeProfiles>

注意上面的server->id的值和mirror->id的值需要一致,这样才能找到对应的凭证。

上面配置会自动从私服中下载构件,但是,如果私服中没有对应构件,还是会访问中央仓库。
所以为了保证只仅仅访问Nexus,不希望访问中央仓库,所以我们还需要配置镜像,通过镜像配置,创建一个匹配任何仓库的镜像,
镜像的地址变为私服,交给私服自己访问中央仓库缓存,本地只需要从私服上下载构件即可。

<mirror>
    <id>maven-nexus-group</id>
    <!-- 此处配置所有的构建均从私有仓库中下载,*代表所有,也可以写成central -->
    <mirrorOf>*</mirrorOf>
    <name>nexus boris images</name>
    <url>http://116.62.106.24:8081/repository/boris-group/</url>
</mirror>

区别:方式一只针对单个项目有效,方式二针对所有项目有效

关闭匿名访问,私服我们应该禁止没有通过用户名和密码验证的用户从我们的私服下载jar包

阿里云maven镜像仓库

<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
原文地址:https://www.cnblogs.com/boris-et/p/13564130.html