Nexus搭建Maven私服(docker)


title: Nexus搭建Maven私服
date: 2020-03-22 10:29:51
categories: 微服务基础设施


Nexus搭建Maven私服

安装

docker pull sonatype/nexus3

运行

version: '3.1'
services:
  nexus:
    restart: always
    image: sonatype/nexus3
    container_name: nexus
    ports:
      - 8081:8081
    volumes:
      - /usr/local/docker/nexus/data:/nexus-data
  • 启动时如果出现权限问题可以使用:chmod 777 /usr/local/docker/nexus/data 赋予数据卷目录可读可写的权限
  • 默认密码别惊讶,就是那么大一串per

异常情况处理*

*UNKNOWN com.sonatype.nexus.plugins.outreach.internal.outreach.SonatypeOutreach - Could not download page bundle
org.apache.http.conn.HttpHostConnectException: Connect to sonatype-download.global.ssl.fastly.net:443 [sonatype-download.global.ssl.fastly.net/69.171.245.49] failed: 连接超时
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:151) [httpcore:0.0.0]
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353) [httpcore:0.0.0]
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380) [httpcore:0.0.0]
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236) [httpcore:0.0.0]
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184) [httpcore:0.0.0]
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88) [httpcore:0.0.0]

处理方法:登录账号,打开【System】–》【Capabilities】,将【Outreach:Management】禁用即可。

在项目中使用maven私服

配置认证信息

在 Maven settings.xml 中添加 Nexus 认证信息(servers 节点下):

<server>
  <id>nexus-releases</id>
  <username>admin</username>
  <password>admin123</password>
</server>

<server>
  <id>nexus-snapshots</id>
  <username>admin</username>
  <password>admin123</password>
</server>

Snapshots 与 Releases 的区别

  • nexus-releases: 用于发布 Release 版本
  • nexus-snapshots: 用于发布 Snapshot 版本(快照版)

Release 版本与 Snapshot 定义如下:

Release: 1.0.0/1.0.0-RELEASE 
Snapshot: 1.0.0-SNAPSHOT
  • 在项目 pom.xml 中设置的版本号添加 SNAPSHOT 标识的都会发布为 SNAPSHOT 版本,没有 SNAPSHOT 标识的都会发布为 RELEASE 版本。
  • SNAPSHOT 版本会自动加一个时间作为标识,如:1.0.0-SNAPSHOT 发布后为变成 1.0.0-SNAPSHOT-20180522.123456-1.jar

配置自动化部署

pom.xml 中添加如下代码:

 <!--上传到私服-->
    <distributionManagement>
        <repository>
            <id>nexus-releases</id>
            <name>Nexus Release Repository</name>
            <url>http://192.168.1.3:8091/repository/maven-releases/</url>
        </repository>
        <snapshotRepository>
            <id>nexus-snapshots</id>
            <name>Nexus Snapshot Repository</name>
            <url>http://192.168.1.3:8091/repository/maven-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>

注意事项:

  • ID 名称必须要与 settings.xml 中 Servers 配置的 ID 名称保持一致。
  • 项目版本号中有 SNAPSHOT 标识的,会发布到 Nexus Snapshots Repository, 否则发布到 Nexus Release Repository,并根据 ID 去匹配授权账号。
  • 部署到仓库
mvn deploy -Dmaven.test.skip=true

上传第三方 JAR 包

Nexus 3.0 不支持页面上传,可使用 maven 命令:

# 如第三方JAR包:kaptcha-2.3.jar
mvn deploy:deploy-file 
  -DgroupId=com.google.code.kaptcha 
  -DartifactId=kaptcha
  -Dversion=2.3 
  -Dpackaging=jar 
  -Dfile=C:\Users\swing\Desktop\kaptcha-2.3.jar
  -Durl=http://172.18.48.62:8081/repository/maven-releases/
  -DrepositoryId=nexus-releases

注意事项:

  • 建议在上传第三方 JAR 包时,创建单独的第三方 JAR 包管理仓库,便于管理有维护。(maven-3rd)
  • -DrepositoryId=nexus-releases 对应的是 settings.xml 中 Servers 配置的 ID 名称。(授权)

配置代理仓库

<!--从私服下载-->
    <repositories>
        <repository>
            <id>nexus-releases</id>
            <name>Nexus Repository</name>
            <url>http://172.18.48.62:8081/repository/maven-releases/</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <releases>
                <enabled>true</enabled>
            </releases>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>nexus-snapshots</id>
            <name>Nexus Plugin Repository</name>
            <url>http://172.18.48.62:8081/repository/maven-snapshots/</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <releases>
                <enabled>true</enabled>
            </releases>
        </pluginRepository>
    </pluginRepositories>

猜你喜欢

转载自blog.csdn.net/qq_42013035/article/details/105620861