Maven私有仓库搭建以及使用

Maven私有仓库搭建以及使用

一、使用Docker-compose安装Nexus

version: '3'

services:
  nexus:
    image: sonatype/nexus3
    container_name: nexus
    restart: always
    ports:
    - "8081:8081"
    volumes:
      - ./nexus-data:/nexus-data3

使用docker-compose up -d启动

查看密码文件

cat nexus-data/admin.password

二、登录Nexus进行配置

1、登录nexus

访问 http://192.168.2.132:8081 进入登录界面 使用默认管理员身份登录,帐号:admin,密码:上一步得到的密码

2、进入管理界面

image-20200904113338404

  • Browse可以查看当前有多少仓库,搭建好的Nexus,默认会带有一些maven仓库,一般使用这些仓库就足够了。
  • 默认仓库说明
    • maven-central:maven中央库,默认从https://repo1.maven.org/maven2/拉取jar
    • maven-releases:私库发行版jar,初次安装请将Deployment policy设置为Allow redeploy
    • maven-snapshots:私库快照(调试版本)jar
    • maven-public:仓库分组,把上面三个仓库组合在一起对外提供服务,在本地maven基础配置settings.xml或项目pom.xml中使用
  • 仓库类型
    • Group:这是一个仓库聚合的概念,用户仓库地址选择Group的地址,即可访问Group中配置的,用于方便开发人员自己设定的仓库。maven-public就是一个Group类型的仓库,内部设置了多个仓库,访问顺序取决于配置顺序,3.x默认Releases,Snapshots,Central,当然你也可以自己设置。
    • Hosted:私有仓库,内部项目的发布仓库,专门用来存储我们自己生成的jar文件
    • Snapshots:本地项目的快照仓库
    • Releases: 本地项目发布的正式版本
    • Proxy:代理类型,从远程中央仓库中寻找数据的仓库(可以点击对应的仓库的Configuration页签下Remote Storage属性的值即被代理的远程仓库的路径),如可配置阿里云maven仓库
    • Central:中央仓库

3、增加新的代理源

  1. 按照步骤添加新的代理源

    image-20200904113744271

  2. 选择添加maven2的代理

    image-20200904113844684

  3. 添加代理 (名称和地址在下一步;Cache统一设置为200天 288000 )

    image-20200904114003740

    image-20200904114141288

  4. 逐个增加常用代理

    1. aliyun
    http://maven.aliyun.com/nexus/content/groups/public
    2. apache_snapshot
    https://repository.apache.org/content/repositories/snapshots/
    3. apache_release
    https://repository.apache.org/content/repositories/releases/
    4. atlassian
    https://maven.atlassian.com/content/repositories/atlassian-public/
    5. central.maven.org
    http://central.maven.org/maven2/
    6. datanucleus
    http://www.datanucleus.org/downloads/maven2
    7. maven-central (安装后自带,仅需设置Cache有效期即可)
    https://repo1.maven.org/maven2/
    8. nexus.axiomalaska.com
    http://nexus.axiomalaska.com/nexus/content/repositories/public
    9. oss.sonatype.org
    https://oss.sonatype.org/content/repositories/snapshots
    10.pentaho
    https://public.nexus.pentaho.org/content/groups/omni/
    
  5. 设置maven-public 将这些代理加入Group,最好将默认的maven库放到最底下,下载顺序是从上到下依次查找,所以把下载最快的放到最上面

    image-20200904114419163

  6. 设置私用仓库可重复发布

    Nexus安装后自带maven-releases,maven-snapshots两个仓库,用于将生成的jar包发布在这两个仓库中,在实际开发中需要将maven-releases设置为可以重复发布

    image-20200904114544197

4、Maven配置使用Nexus

修改Maven的settings.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <!-- localRepository
     | 将默认的仓库位置改为想要设置的位置
     |
     | Default: ${user.home}/.m2/repository
     -->
    <localRepository>D:\maven\repository</localRepository>

    <!-- 插件组 -->
    <pluginGroups/>

    <!-- 代理 -->
    <proxies/>

    <!-- servers服务器(其中username和password是私服的用户名和密码) -->
    <servers>
        <server>
            <id>maven-releases</id>
            <username>admin</username>
            <password>admin111</password>
        </server>

        <server>
            <id>maven-snapshots</id>
            <username>admin</username>
            <password>admin111</password>
        </server>
    </servers>

    <!-- 镜像
     | 这是从远程存储库下载依赖时使用的镜像列表。
     |-->
    <mirrors>
        <!--<mirror>
            <id>alimaven</id>
            <mirrorOf>central</mirrorOf>
            <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        </mirror>-->
        <mirror>
            <id>ManaphyMirror</id>
            <mirrorOf>*</mirrorOf>
            <name>Manaphy Repository Mirror.</name>
            <url>http://192.168.2.132:8081/repository/maven-public/</url>
        </mirror>
    </mirrors>

    <!-- 服务器配置 -->
    <profiles>
        <!-- java编译插件,配jdk的编译版本-->
        <profile>
            <id>jdk-1.8</id>
            <activation>
                <activeByDefault>true</activeByDefault>
                <jdk>1.8</jdk>
            </activation>
            <properties>
                <maven.compiler.source>1.8</maven.compiler.source>
                <maven.compiler.target>1.8</maven.compiler.target>
                <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
            </properties>
        </profile>
        <!-- 自定义私服的配置 -->
        <profile>
            <id>Manaphy</id>
            <repositories>
                <repository>
                    <id>nexus</id>
                    <name>Public Repositories</name>
                    <url>http://192.168.2.132:8081/repository/maven-public/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                </repository>
                <repository>
                    <id>maven-central</id>
                    <name>Central Repositories</name>
                    <url>http://localhost:8082/repository/maven-central/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                </repository>
                <repository>
                    <id>maven-releases</id>
                    <name>Release Repositories</name>
                    <url>http://192.168.2.132:8081/repository/maven-releases/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                </repository>
                <repository>
                    <id>maven-snapshots</id>
                    <name>Snapshot Repositories</name>
                    <url>http://192.168.2.132:8081/repository/maven-snapshots/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>

            <pluginRepositories>
                <pluginRepository>
                    <id>plugins</id>
                    <name>Plugin Repositories</name>
                    <url>http://192.168.2.132:8081/repository/maven-public/</url>
                </pluginRepository>
            </pluginRepositories>
        </profile>
    </profiles>

    <!-- 激活Profiles
     | 为所有生成激活的配置文件的列表。
     |-->
    <activeProfiles>
        <activeProfile>jdk-1.8</activeProfile>
        <activeProfile>Manaphy</activeProfile>
    </activeProfiles>

</settings>

三、创建私有公库

1、打开要发布的maven项目

2、修改项目的pom.xml

<distributionManagement>
    <repository>
        <id>maven-releases</id><!--此处的id要和settings.xml中repository的id保持一致-->
        <name>Nexus Release Repository</name>
        <url>http://192.168.2.132:8081/repository/maven-releases/</url>
    </repository>
    <snapshotRepository>
        <id>maven-snapshots</id><!--此处的id要和settings.xml中repository的id保持一致-->
        <name>Nexus Snapshot Repository</name>
        <url>http://192.168.2.132:8081/repository/maven-snapshots/</url>
    </snapshotRepository>
</distributionManagement>

3、发布私有公库

mvn clean deploy

登录Nexus,查看对应的仓库已经有相关的依赖包了

image-20200904115256759

注意以下几点:

  • 若项目版本号末尾带有 -SNAPSHOT,则会发布到snapshots快照版本仓库
    pository/maven-snapshots/


### 3、发布私有公库

```shell
mvn clean deploy

登录Nexus,查看对应的仓库已经有相关的依赖包了

[外链图片转存中…(img-ZEluMzia-1601300736566)]

注意以下几点:

  • 若项目版本号末尾带有 -SNAPSHOT,则会发布到snapshots快照版本仓库
  • 若项目版本号末尾带有 -RELEASES 或什么都不带,则会发布到releases正式版本仓库
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值