Nexus的作用

Maven私服的作用: - 加快第三方jar引用速度 - 缓存第三方jar - 托管自己开发的jar以便其他项目引用

作为Java开发者,maven的jar包最常用的镜像仓库应该是阿里云的镜像。但是阿里云的镜像只能解决引用第三方jar的速度,并不能托管自己开发的jar。

而Nexus即一款可实现托管自己开发的jar功能的软件。

Nexus安装

为了简化安装难度,使用docker一键安装,centos安装及使用Docker compose自行搜索

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
version: "2"
services:
  nexus:
    image: sonatype/nexus3
    restart: always
    container_name: nexus
    ports:
      - "8081:8081"
    environment:
      MYSQL_ROOT_PASSWORD: MYSQL数据库密码
    volumes:
        # 映射本地磁盘到Docker容器中,用来保存缓存下来的第三方jar文件
      - /home/data/nexus:/nexus-data
    networks:
      - nexus-network

networks:
  nexus-network:
    driver: bridge

前端nginx对应的conf

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
server
{
    listen 80;
    server_name nexus.au92.com;
    index index.html index.htm;

    location / {
        proxy_pass http://内网IP:8081/;   
        proxy_set_header   Host    $host;   
        proxy_set_header   X-Real-IP   $remote_addr;   
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;   
    } 
}

启动后即可通过 nexus.au92.com访问 maven私服。

Nexus设置

  1. 设置阿里云的proxy进一步加快nexus首次加载第三方jar的速度
    • 登录上一步搭建好的nexus(默认密码admin、admin123),依次选择Server administration and configuration->Repository->Repositories->Create repository
      • 选择maven2 (proxy),如图增加一个阿里云的proxy 添加maven proxy
      • 选择maven2 (group),如图增加刚创建的阿里云Proxy到新的maven group中,并设置为优先通过阿里云的proxy加载jar 添加maven group
  2. 设置可以发布release包,如不设置默认不可以发布releases类型jar到私服中
    • Repositories选择maven-releases并设置Deployment policyAllow redeploy 设置可发布releases类型文件

maven settings.xml的settings内容

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  <localRepository>本地第三方jar保存目录</localRepository>

  <pluginGroups>
    <pluginGroup>org.sonarsource.scanner.maven</pluginGroup>
  </pluginGroups>

  <servers>
    <server>
      <id>releases</id>
      <username>nexus私服用户名</username>
      <password>nexus私服密码</password>
    </server>
    <server>
      <id>snapshots</id>
      <username>nexus私服用户名</username>
      <password>nexus私服密码</password>
    </server>
  </servers>

  <mirrors>
    <mirror>
      <id>nexus</id>
      <url>http://nexus.au92.com/repository/maven-public/</url>
      <mirrorOf>*</mirrorOf>
    </mirror>
    <mirror>
      <id>snapshots</id>
      <url>http://nexus.au92.com/repository/maven-snapshots/</url>
      <mirrorOf>public-snapshots</mirrorOf>
    </mirror>
  </mirrors>

注:截止发文时间,文中使用的nexus3版本为OSS 3.18.1-01。

IDEA及项目中配置

  1. 打开IDEA编辑器,选择Preferences->Build, Execution, Develoyment->Build Tools->Maven,设置User settings file为上述settins.xml的地址。

  2. maven的POM文件增加

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    
    <distributionManagement>
    <repository>
        <id>releases</id>
        <name>releases Repository</name>
        <url>http://nexus.au92.com/repository/maven-releases/</url>
    </repository>
    
    <snapshotRepository>
        <id>snapshots</id>
        <url>http://nexus.au92.com/repository/maven-snapshots/</url>
        <uniqueVersion>true</uniqueVersion>
    </snapshotRepository>
    </distributionManagement>

碰到的问题

  • 上传jar时候提示with status code 413 大概率是因为项目build出的jar文件过大,增大nginx的client_max_body_size即可 附本人使用的nginx.conf文件

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    
    user  nginx;
    worker_processes  auto;
    
    error_log  /var/log/nginx/error.log warn;
    pid        /var/run/nginx.pid;
    
    worker_rlimit_nofile 51200;
    
    events
        {
            use epoll;
            worker_connections 51200;
            multi_accept on;
        }
    
    http {
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;
    
        server_tokens off;
        access_log off;
    
        server_names_hash_bucket_size 128;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 200m;
    
    
        sendfile   on;
        tcp_nopush on;
    
        keepalive_timeout 60;
        tcp_nodelay on;
    
        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.1;
        gzip_comp_level 2;
        gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
        gzip_vary on;
        gzip_proxied   expired no-cache no-store private auth;
        gzip_disable   "MSIE [1-6]\.";
    
        include /etc/nginx/conf.d/*.conf;
    }