Nexus 制品库管理实践02 搭建Maven私服(代理仓库)

在搭建好nexus之后会帮你创建好maven的仓库,这个就相对于私服,代理进来就可以直接使用了

 

搭建Maven私服(代理仓库)


为什么要搭建maven私服呢?

默认开发同学在进行开发的时候会使用一些包管理工具,例如:maven、ant、gradle这些都是常见项目编译构建工具 。这些工具可以理解为是一个命令行工具 本身不会存储任何依赖包,而是通过公网官方的仓库中下载当前项目构建所需要的包。 (内网的速度要比公网快,这会直接影响管道的构建速度)

maven私服指的是什么?

使用私服,就是在企业内部建立单一的可信源, 例如:我们在公司通过nexus创建一个代理仓库, 将公网仓库中的maven包代理到内网仓库中。 这样整个公司的同学就可以直接访问内网的私服进行下载构建依赖包。(减少了引入不信任依赖的风险)

代理仓库不会一下子把公网仓库中的所有包下载到本地,而是按需缓存。 例如: 此时我需要使用aa这个包, 如果代理仓库中没有, 则请求外部服务器下载这个包并进行缓存。第二次访问的时候,就直接访问代理仓库了。

搭建制品库(本地仓库)


本地仓库:对于Maven为例, RELEASE类型仓库(存放制品稳定版)SNAPSHOT类型仓库(存放制品开发版)。用于存储公司内部业务开发所生成的制品。

Nexus 制品库管理实践02 搭建Maven私服(代理仓库)_第1张图片

切记:release类型的仓库只能存放release版本的包。

安装nexus后,默认存在以下图中的仓库, 这些仓库是官方默认配置好的maven私服。(可以直接使用)

Nexus 制品库管理实践02 搭建Maven私服(代理仓库)_第2张图片

将maven仓库地址替换阿里云的源 

[root@jenkins-master ~]# vim /usr/local/apache-maven-3.8.1/conf/settings.xml 

    
      alimaven
      central
      aliyun maven
   
      http://139.198.170.122:8081/repository/maven-public/
    

清空本地的maven包 

[root@jenkins-master ~]# rm -rf /root/.m2/
[root@jenkins-master devops-maven-service-master]# mvn clean package
[INFO] Scanning for projects...
Downloading from alimaven: http://139.198.170.122:8081/repository/maven-public/org/springframework/boot/spring-boot-starter-parent/2.4.4/spring-boot-starter-parent-2.4.4.pom
[ERROR] [ERROR] Some problems were encountered while processing the POMs:
[FATAL] Non-resolvable parent POM for com.example:demo:0.0.1-SNAPSHOT: Could not transfer artifact org.springframework.boot:spring-boot-starter-parent:pom:2.4.4 from/to alimaven (http://139.198.170.122:8081/repository/maven-public/): authentication failed for http://139.198.170.122:8081/repository/maven-public/org/springframework/boot/spring-boot-starter-parent/2.4.4/spring-boot-starter-parent-2.4.4.pom, status: 401 Unauthorized and 'parent.relativePath' points at no local POM @ line 5, column 10
 @ 
[ERROR] The build could not read 1 project -> [Help 1]
[ERROR]   
[ERROR]   The project com.example:demo:0.0.1-SNAPSHOT (/root/devops-maven-service-master/pom.xml) has 1 error
[ERROR]     Non-resolvable parent POM for com.example:demo:0.0.1-SNAPSHOT: Could not transfer artifact org.springframework.boot:spring-boot-starter-parent:pom:2.4.4 from/to alimaven (http://139.198.170.122:8081/repository/maven-public/): authentication failed for http://139.198.170.122:8081/repository/maven-public/org/springframework/boot/spring-boot-starter-parent/2.4.4/spring-boot-starter-parent-2.4.4.pom, status: 401 Unauthorized and 'parent.relativePath' points at no local POM @ line 5, column 10 -> [Help 2]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException
[ERROR] [Help 2] http://cwiki.apache.org/confluence/display/MAVEN/UnresolvableModelException
[root@jenkins-master devops-maven-service-master]# 
[root@jenkins-master devops-maven-service-master]# 

这里认证失败,需要开启匿名访问 

Nexus 制品库管理实践02 搭建Maven私服(代理仓库)_第3张图片

[root@jenkins-master devops-maven-service-master]# mvn clean package
[INFO] Scanning for projects...
Downloading from alimaven: http://139.198.170.122:8081/repository/maven-public/org/springframework/boot/spring-boot-starter-parent/2.4.4/spring-boot-starter-parent-2.4.4.pom
Downloaded from alimaven: http://139.198.170.122:8081/repository/maven-public/org/springframework/boot/spring-boot-starter-parent/2.4.4/spring-boot-starter-parent-2.4.4.pom (8.6 kB at 3.0 kB/s)
Downloading from alimaven: http://139.198.170.122:8081/repository/maven-public/org/springframework/boot/spring-boot-dependencies/2.4.4/spring-boot-dependencies-2.4.4.pom
Downloaded from alimaven: http://139.198.170.122:8081/repository/maven-public/org/springframework/boot/spring-boot-dependencies/2.4.4/spring-boot-dependencies-2.4.4.pom (108 kB at 113 kB/s)
Downloading from alimaven: http://139.198.170.122:8081/repository/maven-public/com/datastax/oss/java-driver-bom/4.9.0/java-driver-bom-4.9.0.pom
Downloaded from alimaven: http://139.198.170.122:8081/repository/maven-public/com/datastax/oss/java-driver-bom/4.9.0/java-driver-bom-4.9.0.pom (4.1 kB at 8.9 kB/s)
Downloading from alimaven: http://139.198.170.122:8081/repository/maven-public/io/dropwizard/metrics/metrics-bom/4.1.18/metrics-bom-4.1.18.pom
Downloaded from alimaven: http://139.198.170.122:8081/repository/maven-public/io/dropwizard/metrics/metrics-bom/4.1.18/metrics-bom-4.1.18.pom (5.3 kB at 12 kB/s)
Downloading from alimaven: http://139.198.170.122:8081/repository/maven-public/io/dropwizard/metrics/metrics-parent/4.1.18/metrics-parent-4.1.18.pom

代理仓库是怎么实现的?

对于内网来看是这个地址http://139.198.170.122:8081/repository/maven-central/

但是实际上代理的是国外的地址 Central Repository:

Nexus 制品库管理实践02 搭建Maven私服(代理仓库)_第4张图片

刚开始仓库里面肯定是没有东西的,随着经常使用会帮你挨个的去缓存 

Nexus 制品库管理实践02 搭建Maven私服(代理仓库)_第5张图片

如果你没有私服,可以赶紧去创建一个

Nexus 制品库管理实践02 搭建Maven私服(代理仓库)_第6张图片

这里有个版本,release版本和snapshot版本,release是正式版本,snapshot是开发版本。比如Jenkins就有很多版本,有个稳定的版本和每周迭代的版本,正真发给用户的是一个稳定的版本,稳定的版本这里是release版本。然后输入代理仓库的地址。

仓库地址:Central Repository:Nexus 制品库管理实践02 搭建Maven私服(代理仓库)_第7张图片

Nexus 制品库管理实践02 搭建Maven私服(代理仓库)_第8张图片

复制这个地址就可以配置到你得maven里面了 ,刚刚创建是没有包的,这个是按需的,需要什么包再下载什么包,然后进行缓存。

Nexus 制品库管理实践02 搭建Maven私服(代理仓库)_第9张图片

可以看到很简答,配置好仓库,设置代理的地址就行了。

[root@jenkins-agent ~]# rm -rf .m2/
[ERROR]   The project com.example:demo:0.0.1-SNAPSHOT (/root/devops-maven-service-master/pom.xml) has 1 error
[ERROR]     Non-resolvable parent POM for com.example:demo:0.0.1-SNAPSHOT: Could not transfer artifact org.springframework.boot:spring-boot-starter-parent:pom:2.4.4 from/to alimaven (http://139.198.166.235:8082/repository/mymavenrepo/): authentication failed for http://139.198.166.235:8082/repository/mymavenrepo/org/springframework/boot/spring-boot-starter-parent/2.4.4/spring-boot-starter-parent-2.4.4.pom, status: 401 Unauthorized and 'parent.relativePath' points at no local POM @ line 5, column 10 -> [Help 2]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException
[ERROR] [Help 2] http://cwiki.apache.org/confluence/display/MAVEN/UnresolvableModelException

Nexus 制品库管理实践02 搭建Maven私服(代理仓库)_第10张图片

这些是公共的仓库

[root@jenkins-agent devops-maven-service-master]# mvn clean package
[INFO] Scanning for projects...
Downloading from alimaven: http://139.198.166.235:8082/repository/mymavenrepo/org/springframework/boot/spring-boot-starter-parent/2.4.4/spring-boot-starter-parent-2.4.4.pom
Downloaded from alimaven: http://139.198.166.235:8082/repository/mymavenrepo/org/springframework/boot/spring-boot-starter-parent/2.4.4/spring-boot-starter-parent-2.4.4.pom (8.6 kB at 7.1 kB/s)
Downloading from alimaven: http://139.198.166.235:8082/repository/mymavenrepo/org/springframework/boot/spring-boot-dependencies/2.4.4/spring-boot-dependencies-2.4.4.pom
Downloaded from alimaven: http://139.198.166.235:8082/repository/mymavenrepo/org/springframework/boot/spring-boot-dependencies/2.4.4/spring-boot-dependencies-2.4.4.pom (108 kB at 128 kB/s)
Downloading from alimaven: http://139.198.166.235:8082/repository/mymavenrepo/com/datastax/oss/java-driver-bom/4.9.0/java-driver-bom-4.9.0.pom
Downloaded from alimaven: http://139.198.166.235:8082/repository/mymavenrepo/com/datastax/oss/java-driver-bom/4.9.0/java-driver-bom-4.9.0.pom (4.1 kB at 12 kB/s)
Downloading from alimaven: http://139.198.166.235:8082/repository/mymavenrepo/io/dropwizard/metrics/metrics-bom/4.1.18/metrics-bom-4.1.18.pom
Downloaded from alimaven: http://139.198.166.235:8082/repository/mymavenrepo/io/dropwizard/metrics/metrics-bom/4.1.18/metrics-bom-4.1.18.pom (5.3 kB at 14 kB/s)
Downloading from alimaven: http://139.198.166.235:8082/repository/mymavenrepo/io/dropwizard/metrics/metrics-parent/4.1.18/metrics-parent-4.1.18.pom
Downloaded from alimaven: http://139.198.166.235:8082/repository/mymavenrepo/io/dropwizard/metrics/metrics-parent/4.1.18/metrics-parent-4.1.18.pom (17 kB at 43 kB/s)
Downloading from alimaven: http://139.198.166.235:8082/repository/mymavenrepo/org/codehaus/groovy/groovy-bom/2.5.14/groovy-bom-2.5.14.pom

Nexus 制品库管理实践02 搭建Maven私服(代理仓库)_第11张图片

你可能感兴趣的:(Devops,CI,制品库,Nexus,nexus)