持续部署架构 -- 本地私仓Nexus

文章目录

Nexus可作为项目依赖存放以及项目发布的仓库

安装Nexus

安装软件(https://www.sonatype.com/nexus/repository-oss/download):

[root@nexus ~]# tar -zxf nexus-3.27.0-03-unix.tar.gz -C /usr/local/
[root@nexus ~]# cd /usr/local
[root@nexus ~]# ln -s /usr/local/nexus-3.27.0-03/ /usr/local/nexus
[root@nexus ~]# ll /usr/local/nexus
lrwxrwxrwx. 1 root root 27 Sep 18 10:11 /usr/local/nexus -> /usr/local/nexus-3.27.0-03/
[root@nexus ~]# 

创建启动用户并配置目录:

[root@nexus ~]# useradd -r nexus    
[root@nexus ~]# cd /usr/local/nexus/bin/
[root@nexus bin]# ls
contrib  nexus  nexus.rc  nexus.vmoptions
[root@nexus bin]# cp nexus.rc{,.bak}
[root@nexus bin]# vi nexus.rc
[root@nexus bin]# cat nexus.rc
run_as_user="nexus"
[root@nexus bin]# cd ../..
[root@nexus local]# chown -R nexus.nexus nexus
[root@nexus local]# chown -R nexus.nexus nexus-3.27.0-03/
[root@nexus local]# chown -R nexus.nexus sonatype-work/
[root@nexus local]# ll
total 0
drwxr-xr-x. 2 root  root    6 May 11  2019 bin
drwxr-xr-x. 2 root  root    6 May 11  2019 etc
drwxr-xr-x. 2 root  root    6 May 11  2019 games
drwxr-xr-x. 2 root  root    6 May 11  2019 include
drwxr-xr-x. 2 root  root    6 May 11  2019 lib
drwxr-xr-x. 2 root  root    6 May 11  2019 lib64
drwxr-xr-x. 2 root  root    6 May 11  2019 libexec
lrwxrwxrwx. 1 nexus nexus  27 Sep 18 10:11 nexus -> /usr/local/nexus-3.27.0-03/
drwxr-xr-x. 9 nexus nexus 163 Sep 18 10:10 nexus-3.27.0-03
drwxr-xr-x. 2 root  root    6 May 11  2019 sbin
drwxr-xr-x. 5 root  root   49 Sep 11 17:09 share
drwxr-xr-x. 3 nexus nexus  20 Sep 18 10:10 sonatype-work
drwxr-xr-x. 2 root  root    6 May 11  2019 src
[root@nexus local]# 

配置服务:

[root@nexus ~]# vi /etc/systemd/system/nexus.service
[root@nexus ~]# cat /etc/systemd/system/nexus.service
[Unit]
Description=nexus service
After=network.target
[Service]
Type=forking
LimitNOFILE=65536
ExecStart=/usr/local/nexus/bin/nexus start
ExecStop=/usr/local/nexus/bin/nexus stop
User=nexus
Restart=on-abort
[Install]
WantedBy=multi-user.target
[root@nexus ~]# systemctl daemon-reload
[root@nexus ~]# 

安装OpenJDK:

[root@nexus ~]# dnf install java-1.8.0-openjdk
Last metadata expiration check: 0:02:58 ago on Fri 18 Sep 2020 10:20:21 AM CST.
Dependencies resolved.
====================================================================================================================================================================================================================
 Package                                                    Architecture                          Version                                                            Repository                                Size
====================================================================================================================================================================================================================
Installing:
 java-1.8.0-openjdk                                         x86_64                                1:1.8.0.265.b01-0.el8_2                                            AppStream                                324 k
Installing dependencies:
 alsa-lib                                                   x86_64                                1.2.1.2-3.el8                                                      AppStream                                441 k
 copy-jdk-configs                                           noarch                                3.7-1.el8                                                          AppStream                                 27 k
 giflib                                                     x86_64                                5.1.4-3.el8                                                        AppStream                                 51 k
 java-1.8.0-openjdk-headless                                x86_64                                1:1.8.0.265.b01-0.el8_2                                            AppStream                                 34 M
 javapackages-filesystem                                    noarch                                5.3.0-1.module_el8.0.0+11+5b8c10bd                                 AppStream                                 30 k
 libfontenc                                                 x86_64                                1.1.3-8.el8                                                        AppStream                                 37 k
 lksctp-tools                                               x86_64                                1.0.18-3.el8                                                       BaseOS                                   100 k
 lua                                                        x86_64                                5.3.4-11.el8                                                       AppStream                                193 k
 ttmkfdir                                                   x86_64                                3.0.9-54.el8                                                       AppStream                                 62 k
 tzdata-java                                                noarch                                2020a-1.el8                                                        AppStream                                189 k
 xorg-x11-font-utils                                        x86_64                                1:7.5-40.el8                                                       AppStream                                103 k
 xorg-x11-fonts-Type1                                       noarch                                7.5-19.el8                                                         AppStream                                522 k
Installing weak dependencies:
 gtk2                                                       x86_64                                2.24.32-4.el8                                                      AppStream                                3.4 M
Enabling module streams:
 javapackages-runtime                                                                             201801                                                                                                           

Transaction Summary
====================================================================================================================================================================================================================
Install  14 Packages

Total download size: 39 M
Installed size: 134 M
Is this ok [y/N]: 

安装Nginx:

[root@nexus ~]# vi /etc/yum.repos.d/nginx.repo
[root@nexus ~]# cat /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[root@nexus ~]# dnf install nginx
nginx stable repo                                                                                                                                                                    18 kB/s |  16 kB     00:00    
Last metadata expiration check: 0:00:01 ago on Fri 18 Sep 2020 10:20:21 AM CST.
Dependencies resolved.
====================================================================================================================================================================================================================
 Package                                       Architecture                                   Version                                                    Repository                                            Size
====================================================================================================================================================================================================================
Installing:
 nginx                                         x86_64                                         1:1.18.0-1.el8.ngx                                         nginx-stable                                         806 k

Transaction Summary
====================================================================================================================================================================================================================
Install  1 Package

Total download size: 806 k
Installed size: 3.6 M
Is this ok [y/N]: 

配置Nexus代理:

[root@nexus ~]# vi /etc/nginx/conf.d/nexus.conf
[root@nexus ~]# cat /etc/nginx/conf.d/nexus.conf
server {
  listen *:80;
  server_name nexus.example.com www.nexus.example.com;

  proxy_send_timeout 120;
  proxy_read_timeout 300;
  proxy_buffering off;
  keepalive_timeout 5 5;
  tcp_nodelay on;

  # allow large uploads of files
  client_max_body_size 1G;

  # optimize downloading files larger than 1G
  #proxy_max_temp_file_size 2G;

  location / {
    # Use IPv4 upstream address instead of DNS name to avoid attempts by nginx to use IPv6 DNS lookup
    proxy_pass http://127.0.0.1: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;
  }
}
[root@nexus ~]# 

配置防火墙并启动服务(稍等直到Nexus8081端口出现):

[root@nexus ~]# firewall-cmd --zone=public --permanent --add-service=http
success
[root@nexus ~]# firewall-cmd --zone=public --permanent --add-service=https
success
[root@nexus ~]# firewall-cmd --reload
success
[root@nexus ~]# systemctl start nginx nexus
[root@nexus ~]# systemctl enable nginx nexus
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
Created symlink /etc/systemd/system/multi-user.target.wants/nexus.service → /etc/systemd/system/nexus.service.
[root@nexus ~]# ss -tlunp | grep 8081
tcp     LISTEN   0        50               0.0.0.0:8081           0.0.0.0:*      users:(("java",pid=10732,fd=784))                                              
[root@nexus ~]#

找到密码文件并登录:

[root@nexus ~]# cat /usr/local/sonatype-work/nexus3/admin.password 
363741a2-9af3-4e2a-b65f-4faa90ca89ba
[root@nexus ~]# 

在这里插入图片描述

是否允许匿名登陆(这里选否):

在这里插入图片描述

查看默认存在的仓库:

在这里插入图片描述

  • maven-central(proxy):Nexus私仓找不到的依赖,就通过这个代理去网上下
  • maven-snapshots(hosted):Maven的非正式发布版就存在这里
  • maven-release(hosted):Maven的正式发布版就存在这里
  • maven-public(group):将其它仓库成组统一管理
    在这里插入图片描述

修改代理仓库的镜像源:

在这里插入图片描述

使用Nexus

回到WebSrv服务器,修改Maven的配置文件。

参考文档:https://help.sonatype.com/repomanager3/formats/maven-repositories#MavenRepositories-ConfiguringApacheMaven

<servers>段内添加:

  <servers>
    ....
    <server>
      <id>nexus</id>
      <username>admin</username>
      <password>123456</password>
    </server>
    ....
  </servers>

<mirrors>段内添加:

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

<profiles>段内添加:

  <profiles>
    ....
    <profile>
      <id>nexus</id>
      <!--Enable snapshots for the built in central repo to direct -->
      <!--all requests to nexus via the mirror -->
      <repositories>
        <repository>
          <id>central</id>
          <url>http://central</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
      </repositories>
     <pluginRepositories>
        <pluginRepository>
          <id>central</id>
          <url>http://central</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
    ....
  </profiles>

<settings>段内添加:

<settings>
  ....
  <activeProfiles>
    <!--make the profile active all the time -->
    <activeProfile>nexus</activeProfile>
  </activeProfiles>
  ....
</settings>

13.13.5.5 nexus.example.com写入/etc/hosts文件:

[root@websrv ~]# vi /etc/hosts
[root@websrv ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

13.13.2.2 gitlab  gitlab.example.com
13.13.3.3 jenkins
13.13.4.4 websrv
13.13.5.5 nexus   nexus.example.com

[root@websrv ~]# 

再次尝试运行mvn指令(已指向私仓):

[root@websrv ~]# cd flamingo/
[root@websrv flamingo]# mvn clean package
[INFO] Scanning for projects...
Downloading from nexus: http://nexus.example.com/repository/maven-public/org/springframework/boot/spring-boot-starter-parent/2.1.3.RELEASE/spring-boot-starter-parent-2.1.3.RELEASE.pom
Downloaded from nexus: http://nexus.example.com/repository/maven-public/org/springframework/boot/spring-boot-starter-parent/2.1.3.RELEASE/spring-boot-starter-parent-2.1.3.RELEASE.pom (12 kB at 13 kB/s)
Downloading from nexus: http://nexus.example.com/repository/maven-public/org/springframework/boot/spring-boot-dependencies/2.1.3.RELEASE/spring-boot-dependencies-2.1.3.RELEASE.pom
Downloaded from nexus: http://nexus.example.com/repository/maven-public/org/springframework/boot/spring-boot-dependencies/2.1.3.RELEASE/spring-boot-dependencies-2.1.3.RELEASE.pom (143 kB at 438 kB/s)
Downloading from nexus: http://nexus.example.com/repository/maven-public/com/fasterxml/jackson/jackson-bom/2.9.8/jackson-bom-2.9.8.pom
................

检查Nexus仓库:

在这里插入图片描述

再将打包好的项目文件发布到私仓中吧(配置POM文件):

<project>
  ....
  <version>0.0.1-SNAPSHOT</version>
  ....
  <distributionManagement>
    <repository>
      <id>nexus</id>
      <name>Releases</name>
      <url>http://nexus.example.com/repository/maven-releases</url>
    </repository>
    <snapshotRepository>
      <id>nexus</id>
      <name>Snapshot</name>
      <url>http://nexus.example.com/repository/maven-snapshots</url>
    </snapshotRepository>
  </distributionManagement>
  .....
</project>

Maven部署SNAPSHOT非正式发布版:

[root@websrv flamingo]# mvn clean deploy
[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------< org.ray.flamingo:Flamingo >----------------------
[INFO] Building Flamingo 0.0.1-SNAPSHOT

............  omitted fot brevity

Uploading to nexus: http://nexus.example.com/repository/maven-snapshots/org/ray/flamingo/Flamingo/0.0.1-SNAPSHOT/Flamingo-0.0.1-20200918.034248-1.war
Uploaded to nexus: http://nexus.example.com/repository/maven-snapshots/org/ray/flamingo/Flamingo/0.0.1-SNAPSHOT/Flamingo-0.0.1-20200918.034248-1.war (49 MB at 19 MB/s)
Uploading to nexus: http://nexus.example.com/repository/maven-snapshots/org/ray/flamingo/Flamingo/0.0.1-SNAPSHOT/Flamingo-0.0.1-20200918.034248-1.pom
Uploaded to nexus: http://nexus.example.com/repository/maven-snapshots/org/ray/flamingo/Flamingo/0.0.1-SNAPSHOT/Flamingo-0.0.1-20200918.034248-1.pom (3.2 kB at 56 kB/s)
Downloading from nexus: http://nexus.example.com/repository/maven-snapshots/org/ray/flamingo/Flamingo/maven-metadata.xml
Uploading to nexus: http://nexus.example.com/repository/maven-snapshots/org/ray/flamingo/Flamingo/0.0.1-SNAPSHOT/maven-metadata.xml
Uploaded to nexus: http://nexus.example.com/repository/maven-snapshots/org/ray/flamingo/Flamingo/0.0.1-SNAPSHOT/maven-metadata.xml (774 B at 10 kB/s)
Uploading to nexus: http://nexus.example.com/repository/maven-snapshots/org/ray/flamingo/Flamingo/maven-metadata.xml
Uploaded to nexus: http://nexus.example.com/repository/maven-snapshots/org/ray/flamingo/Flamingo/maven-metadata.xml (284 B at 6.3 kB/s)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 17.219 s
[INFO] Finished at: 2020-09-18T11:42:50+08:00
[INFO] ------------------------------------------------------------------------
[root@websrv flamingo]# 

检查Nexus的maven-snapshots仓库:

在这里插入图片描述
Maven部署RELEASE正式发布版:

[root@websrv flamingo]# grep SNAPSHOT pom.xml 
	<version>0.0.1-SNAPSHOT</version>
[root@websrv flamingo]# sed -i 's/0.0.1-SNAPSHOT/0.1.0-RELEASE/g' pom.xml 
[root@websrv flamingo]#
[root@websrv flamingo]# mvn clean deploy
[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------< org.ray.flamingo:Flamingo >----------------------
[INFO] Building Flamingo 0.1.0-RELEASE

.............. omitted for brevity

[INFO] --- maven-deploy-plugin:2.8.2:deploy (default-deploy) @ Flamingo ---
Uploading to nexus: http://nexus.example.com/repository/maven-releases/org/ray/flamingo/Flamingo/0.1.0-RELEASE/Flamingo-0.1.0-RELEASE.war
Uploaded to nexus: http://nexus.example.com/repository/maven-releases/org/ray/flamingo/Flamingo/0.1.0-RELEASE/Flamingo-0.1.0-RELEASE.war (49 MB at 17 MB/s)
Uploading to nexus: http://nexus.example.com/repository/maven-releases/org/ray/flamingo/Flamingo/0.1.0-RELEASE/Flamingo-0.1.0-RELEASE.pom
Uploaded to nexus: http://nexus.example.com/repository/maven-releases/org/ray/flamingo/Flamingo/0.1.0-RELEASE/Flamingo-0.1.0-RELEASE.pom (3.2 kB at 48 kB/s)
Downloading from nexus: http://nexus.example.com/repository/maven-releases/org/ray/flamingo/Flamingo/maven-metadata.xml
Uploading to nexus: http://nexus.example.com/repository/maven-releases/org/ray/flamingo/Flamingo/maven-metadata.xml
Uploaded to nexus: http://nexus.example.com/repository/maven-releases/org/ray/flamingo/Flamingo/maven-metadata.xml (320 B at 7.1 kB/s)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.472 s
[INFO] Finished at: 2020-09-18T11:50:04+08:00
[INFO] ------------------------------------------------------------------------
[root@websrv flamingo]# 

检查Nexus的maven-releases仓库:
在这里插入图片描述
最后不要忘记将更改的pom.xml推送至GitLab:

[root@websrv flamingo]# git pull
Already up to date.
[root@websrv flamingo]# git add pom.xml 
[root@websrv flamingo]# git commit -m "make mvn point to local nexus"
[master 35c858f] make mvn point to local nexus
 1 file changed, 14 insertions(+), 1 deletion(-)
[root@websrv flamingo]# git push -u origin master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 473 bytes | 473.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
To gitlab.example.com:root/flamingo.git
   f6a18ae..35c858f  master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
[root@websrv flamingo]# 
  • 0
    点赞
  • 0
    评论
  • 1
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值