最近发现公司的Jenkins打包时需要通过https://maven.aliyun.com/repository/public 去下载一些依赖包,但是局域网里之前搭建了Nexus的,登录到nexus上一看,全是公司内部开发的基础包和第三方合作机构的包,感觉这种模式有问题,自己百度上网看了下,发现标准模式是:开发人员全部通过Nexus下载依赖包,如果Nexus本地找不到,就通过代理下载Maven中央仓库的文件到Nexus本地,然后开发人员再从Nexus上拉到自己的电脑上(鄙人见识短浅,非专业配置管理)。以下为我这边调整后的拓扑图:
这种情况下,Nexus上不存在相关的jar时,只要从Maven中央仓库同步了1次就永久存在本地了。以后不管来了多少开发人员,只要本地settings指向Nexus私服后,就可以从私服上下载文件,内网下载速度快而且稳定。之前那种开发人员电脑直接访问互联网maven仓会带来很多问题:1、多个人同时下载jar依赖会消耗大量互联网带宽资源,2、下载资源的会话可能会不稳定,3、依赖jar可能还有很多依赖,建立很多会话数,消耗公司互联网网络设备的性能。
以下为主要的设置步骤,在修改设置之前我先看了这篇文章受到一点启发,文章的链接:https://www.cnblogs.com/zhangzimo/p/13931917.html
1、进入nexus的管理界面,将Central的远程地址改为阿里云的地址:https://maven.aliyun.com/repository/public/ ,按照下图改下配置
2、改下apache-snapshots的配置,我感觉这个用不到,算了统一改了吧,https://maven.aliyun.com/repository/apache-snapshots/
3、将相关仓库加到组中
好了,接下来到jenkins上修改apache-maven-3.5.4/conf下的配置文件,主要的内容如下
<localRepository>/app/repository</localRepository>
<pluginGroups>
</pluginGroups>
<proxies>
</proxies>
<servers>
<server>
<id>releases</id>
<username>admin</username>
<password>k123456K</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>k123456K</password>
</server>
</servers>
<mirrors>
<mirror>
<id>nexus</id>
<name>nexus</name>
<url>http://10.50.11.15:8081/nexus/content/groups/public/</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>
<profiles>
<profile>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>release</id>
<name>release</name>
<releases>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
<url>http://10.50.11.15:8081/nexus/content/groups/public/</url>
</repository>
<repository>
<id>thirdparty</id>
<name>thirdparty</name>
<releases>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
<url>http://10.50.11.15:8081/nexus/content/groups/public/</url>
</repository>
<repository>
<id>snapshots</id>
<name>snapshots</name>
<releases>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
<url>http://10.50.11.15:8081/nexus/content/groups/public/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>snapshots</id>
<name>snapshots</name>
<releases>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
<url>http://10.50.11.15:8081/nexus/content/groups/public/</url>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
</settings>
好了,所有的步骤完成了。结果jenkins打包的时候,提示本地私服上没有相关的依赖包,但是不去阿里云Maven中央库上下载文件。百度了都没有类似的问题,头疼得很,后来从nexus上的日志下手,发现提示远端仓库不可达(后来可以下载中央仓库文件这个日志还是一直在,好像和这个问题没关系)。
jvm 1 | 2022-11-10 10:41:43,455+0800 WARN [proxy-3-thread-18732] admin org.sonatype.nexus.proxy.maven.maven2.M2Repository - Remote peer of proxy repository "Apache Snapshots" [id=apache-snapshots] threw a org.sonatype.nexus.proxy.ItemNotFoundException exception. - Cause(s): Remote peer of repository M2Repository(id=apache-snapshots) detected as unavailable.
jvm 1 | 2022-11-10 10:42:32,979+0800 INFO [ar-4-thread-3] admin org.sonatype.nexus.proxy.maven.routing.internal.RemoteScrapeStrategy - Not possible remote scrape of M2Repository(id=apache-snapshots), no scraper succeeded.
jvm 1 | 2022-11-10 10:46:39,488+0800 WARN [proxy-3-thread-18734] admin org.sonatype.nexus.proxy.maven.maven2.M2Repository - Remote peer of proxy repository "Central" [id=central] threw a org.sonatype.nexus.proxy.ItemNotFoundException exception. - Cause(s): Remote peer of repository M2Repository(id=central) detected as unavailable.
再去看了下nexus上的状态,发现Central和Apache Snapshots的代理功能被自动禁用了 (下图):
估计问题就和这个有关,后面改了下Central和Apache Snapshots的设置
分别启用Central和Apache Snapshots的代理功能
然后无意中点到仓库组Public repositories的‘Rebuild Metadata’,之前好像还点到了Central的‘Expire Cache’和Public repositories的‘Expire Cache’,发现Nexus可以同步下载阿里云Maven中央仓库的文件了。汗,终于搞定了。
以下为jenkins上apache-maven-3.5.4/conf/settings的所有内容,这个settings的文件要替换所有的jenkins服务器上maven配置文件和开发人员的电脑上maven程序的配置文件。
<?xml versinotallow="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocatinotallow="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<!-- TIPS: modify local repository -->
<localRepository>/app/comm/repository</localRepository>
<pluginGroups>
</pluginGroups>
<proxies>
</proxies>
<!-- servers | This is a list of authentication profiles, keyed by the server-id
used within the system. | Authentication profiles can be used whenever maven
must make a connection to a remote server. | -->
<servers>
<server>
<id>releases</id>
<username>admin</username>
<password>k2244444u1K</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>k2244444u1K</password>
</server>
</servers>
<!-- mirrors | This is a list of mirrors to be used in downloading artifacts
from remote repositories. | | It works like this: a POM may declare a repository
to use in resolving certain artifacts. | However, this repository may have
problems with heavy traffic at times, so people have mirrored | it to several
places. | | That repository definition will have a unique id, so we can create
a mirror reference for that | repository, to be used as an alternate download
site. The mirror site will be the preferred | server for that repository.
| -->
<mirrors>
<!--<mirror>
<id>repo2</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://repo2.maven.org/maven2/</url>
</mirror> -->
<!--<mirror>
<id>net-cn</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://maven.net.cn/content/groups/public/</url>
</mirror>
<mirror>
<id>ui</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://uk.maven.org/maven2/</url>
</mirror>-->
<!--<mirror>
<id>nexus-aliyun</id>
<mirrorOf>nexus-aliyun</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
<mirror>
<id>ibiblio</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://mirrors.ibiblio.org/pub/mirrors/maven2/</url>
</mirror>
<mirror>
<id>jboss-public-repository-group</id>
<mirrorOf>central</mirrorOf>
<name>JBoss Public Repository Group</name>
<url>http://repository.jboss.org/nexus/content/groups/public</url>
</mirror>-->
<mirror>
<id>nexus</id>
<name>nexus</name>
<url>http://10.50.11.15:8081/nexus/content/groups/public/</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>
<!-- profiles | This is a list of profiles which can be activated in a variety
of ways, and which can modify | the build process. Profiles provided in the
settings.xml are intended to provide local machine- | specific paths and
repository locations which allow the build to work in the local environment.
| | For example, if you have an integration testing plugin - like cactus
- that needs to know where | your Tomcat instance is installed, you can provide
a variable here such that the variable is | dereferenced during the build
process to configure the cactus plugin. | | As noted above, profiles can
be activated in a variety of ways. One way - the activeProfiles | section
of this document (settings.xml) - will be discussed later. Another way essentially
| relies on the detection of a system property, either matching a particular
value for the property, | or merely testing its existence. Profiles can also
be activated by JDK version prefix, where a | value of '1.4' might activate
a profile when the build is executed on a JDK version of '1.4.2_07'. | Finally,
the list of active profiles can be specified directly from the command line.
| | NOTE: For profiles defined in the settings.xml, you are restricted to
specifying only artifact | repositories, plugin repositories, and free-form
properties to be used as configuration | variables for plugins in the POM.
| | -->
<profiles>
<profile>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>release</id>
<name>release</name>
<releases>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
<url>http://10.50.11.15:8081/nexus/content/groups/public/</url>
</repository>
<repository>
<id>thirdparty</id>
<name>thirdparty</name>
<releases>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
<url>http://10.50.11.15:8081/nexus/content/groups/public/</url>
</repository>
<repository>
<id>snapshots</id>
<name>snapshots</name>
<releases>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
<url>http://10.50.11.15:8081/nexus/content/groups/public/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>snapshots</id>
<name>snapshots</name>
<releases>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
<url>http://10.50.11.15:8081/nexus/content/groups/public/</url>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
</settings>