使用nexus构建maven私有仓库

689 阅读2分钟

下载安装nexus3

nexus3 docker

搜索nexus镜像

docker search nexus

获取nexus3镜像

docker pull sonatype/nexus3

启动

docker run -d -p 8081:8081 -v /opt/nexus-data:/nexus-data --name nexus3 sonatype/nexus3

容器启动失败,查看日志。挂载的数据卷没有权限,需要赋予权限。

chmod 777 /opt/nexus-data

等待2-3分钟,访问网页管理端

localhost:8081

默认账号admin,第一次登陆需要修改密码.

初始密码在数据卷中admin.password

配置

  • hosted,本地仓库,通常我们会部署自己的构件到这一类型的仓库。比如公司的第二方库。
  • proxy,代理仓库,它们被用来代理远程的公共仓库,如maven中央仓库。
  • group,仓库组,用来合并多个hosted/proxy仓库,当你的项目希望在多个repository使用资源时就不需要多次引用了,只需要引用一个group即可。

允许匿名访问nexus

Anonymous 选择对应Allow anonymous users to access the server

配置指南

打开maven的配置文件(windows机器一般在maven安装目录的conf/settings.xml),在标签中添加mirror子节点:

<mirror>
    <id>nexus</id>
    <mirrorOf>central</mirrorOf>
    <name>internal nexus repository</name>
    <url>http://192.168.166.124:8081/repository/maven-public/</url>
</mirror>

如果想使用其它代理仓库,可在节点中加入对应的仓库使用地址。以使用spring代理仓为例:

<repository>
    <id>spring</id>
    <url>https://maven.aliyun.com/repository/spring</url>
    <releases>
        <enabled>true</enabled>
    </releases>
    <snapshots>
        <enabled>true</enabled>
    </snapshots>
</repository>

发布

比方说,需要发布girl-spring-boot-starter到私服。我们需要在girl-spring-boot-starter的 pom 文件中增加如下配置:

<distributionManagement>
        <snapshotRepository>
            <id>my-shop-snapshots</id>
            <name>snapshot repository</name>
            <url>http://192.168.166.124:8081/repository/maven-snapshots/</url>
        </snapshotRepository>
        <repository>
            <id>my-shop-releases</id>
            <name>releases repository</name>
            <url>http://192.168.166.124:8081/repository/maven-releases/</url>
        </repository>
    </distributionManagement>  

id为自定义 对应setting文件的标签的,url 节点对应的是私服连接

<server>  
      <id>my-shop-releases</id>  
      <username>admin</username>  
      <password>admin123</password>  
   </server>  
   <server>  
      <id>my-shop-snapshots</id>  
      <username>admin</username>  
      <password>admin123</password>  
   </server>

上传代码到私仓

 mvn delpoy -Dmaven.test.skip=true