一、介绍
nexus是一个maven私服。
私服
私服是一种特殊的远程仓库,它是架设在局域网内的仓库服务,私服代理广域网上的远程仓库,供局域网内的用户使用。当Maven需要下载构件的时候,它从私服请求,如果 私服上不存在该构件,则从外部远程仓库下载,缓存在私服上之后,再为Maven的下载请求提供服务。
私服的好处:
a、节省自己的外网带宽
b、加速Maven构建
c、部署自己内部的第三方构件
d、提高稳定性,增强控制
e、降低中央仓库的负荷。
二、下载
网上很多文档说官网下载慢,但是能下载,但是我一直没下载下来,即使翻墙,也没有成功下载,最后从国内csdn网友的分享下载的。这里也分享一下:
链接:https://pan.baidu.com/s/1oN6p-v8r8kfHbaHUbtjEaA
提取码:bn50
nexus为windows、osX、unix三个平台提供支持。我分享的版本是:nexus-3.20.1-01-win64.rar
三、安装
1、解压压缩包到指定文件夹,比如:D:\06.tools\nexus3,得到nexus-3.20.1-01文件夹和 sonatype-work 文件夹
nexus-3.16.2-01:是nexus服务器相关的文件
sonatype-work:是nexus工作的数据文件,上传下载的jar包就在这个文件夹下面。
四、启动
在命令提示符中进去D:\06.tools\nexus3\nexus-3.20.1-01\bin文件夹,执行命令:nexus /run,访问http://localhost:8081
五、使用
nexus3的使用
1.登录nexus
使用默认用户admin,密码admin123,登录。
2.管理私服本地仓库
2.1仓库类型
Nexus有4个【类型】的数据仓库,分别是hosted,proxy,group、virtual(基本不用)。
- hosted 宿主仓库:主要用于部署无法从公共仓库获取的构件以及自己或第三方的项目构件;
- proxy 代理仓库:代理公共的远程仓库;
- group 仓库组:Nexus 通过仓库组统一管理多个仓库,这样我们在项目中直接请求仓库组即可请求到仓库组管理的多个仓库。
Nexus默认的仓库类型有以下四种:(上面的名字可以随便取,关键是它对应的是什么仓库类型) 1)group(仓库组类型):又叫组仓库,用于方便开发人员自己设定的仓库; 2)hosted(宿主类型):内部项目的发布仓库(内部开发人员,发布上去存放的仓库); 3)proxy(代理类型): 从远程中央仓库中寻找数据的仓库(可以点击对应的仓库的Configuration页签下Remote Storage Location属性的值即被代理的远程仓库的路径); 4)virtual(虚拟类型): 虚拟仓库(这个基本用不到,重点关注上面三个仓库的使用); Policy(策略):表示该仓库为发布(Release)版本仓库还是快照(Snapshot)版本仓库; |
2.2默认仓库
Nexus预定义了2个本地仓库,分别是maven-releases, maven-snapshots。
- maven-releases:这里存放我们自己项目中发布的构建, 通常是Release版本的。
- maven-snapshots:这个仓库非常的有用, 它的目的是让我们可以发布那些非release版本, 非稳定版本。
默认仓库介绍 1)maven-central(proxy): maven中央库,默认从https://repo1.maven.org/maven2/拉取jar 2)maven-releases(hosted): 私库发行版jar 3)maven-snapshots(hosted):私库快照(调试版本)jar 4)maven-public(group): 仓库分组,把上面三个仓库组合在一起对外提供服务,在本地maven基础配置settings.xml中使用。 |
2.3仓库拉取jar包流程
1)Maven可直接从【hosted宿主仓库】下载构件,也可以从【proxy代理仓库】下载构件,而代理仓库间接的从所代理的【远程仓库】下载并缓存构件
2)为了方便,Maven可以从仓库组下载构件,而仓库组并没有实际的内容(下图中用虚线表示,它会转向包含的宿主仓库或者代理仓库获得实际构件的内容).
2.4创建自己的仓库
(~~非必要~~)直接使用Nexus预定义了2个本地仓库maven-releases, maven-snapshots就行,用来存放我们自己工程发布的jar包
根据类型选择其中一个:
如果是创建的proxy仓库或者hosted仓库,需要添加到公共仓库里。
2.5上传本地jar包
(~~非必要~~):直接通过工程deploy发布我们自己工程项目
3.使用nexus
3.1 maven中配置nexus
1、目的:在本地maven中配置nexus私服,可以:
- 在本地项目下载第三方jar包时优先去nexus私服中下载
- 项目组其他maven项目发布到nexus后,供其他人下载
2、方式:修改setting.xml文件,可以修改maven安装目录下conf中的该文件($MAVEN_HOME/conf/setting.xml),也可以修改用户目录下.m2目录的该文件,我选择修改后者,因为更换maven版本时不需要考虑配置文件的再次修改。
1)设置私服账号密码:servers标签增加如下内容
注意这里配置的server的id必须和pom文件中的distributionManagement对应仓库的id保持一致,maven在处理发布时会根据id查找用户名称和密码进行登录和文件的上传发布。
这里的ID值对应pom.xml的id=releases的仓库,也就是
<server>
<id>releases</id> <!--对应pom.xml的id=releases的仓库-->
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshots</id> <!--对应pom.xml中id=snapshots的仓库-->
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>nexus-ma</id>
<username>admin</username>
<password>admin123</password>
</server>
2)为仓库列表配置的下载镜像列表<mirrors>标签下增加下面内容
<!--为仓库列表配置的下载镜像列表。 -->
<mirror>
<!--该镜像的唯一标识符。id用来区分不同的mirror元素。 -->
<id>nexus-ma</id>
<name>internal nexus repository</name>
<!--镜像采用配置好的组的地址-->
<!--该镜像的URL。构建系统会优先考虑使用该URL,而非使用默认的服务器URL。 -->
<url>http://10.20.30.158:8081/repository/maven-public/</url>
<!--此处配置所有的构建均从私有仓库中下载 *代表所有,也可以写central -->
<mirrorOf>*</mirrorOf>
</mirror>
3)配置远程发布的私服<profiles>标签中增加下面内容
<profile>
<id>nexus-pr</id>
<!--远程仓库列表,它是Maven用来填充构建系统本地仓库所使用的一组远程项目。 -->
<repositories>
<!--发布版本仓库-->
<repository>
<id>nexus-ma</id>
<name>Nexus Central</name>
<!--地址是nexus中repository(Releases/Snapshots)中对应的地址--
<!-- 虚拟的URL形式,指向镜像的URL-->
<url>http://10.20.30.158:8081/repository/maven-public/</url>
<layout>default</layout>
<!-- 表示可以从这个仓库下载releases版本的构件-->
<releases>
<enabled>true</enabled>
</releases>
<!-- 表示可以从这个仓库下载snapshot版本的构件 -->
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<!-- 插件仓库列表 -->
<pluginRepositories>
<pluginRepository>
<id>nexus-ma</id>
<name>Nexus Central</name>
<url>http://10.20.30.158:8081/repository/maven-public/</url>
<layout>default</layout>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
</profile>
4)激活 <profile>中的ID使之生效
<activeProfiles>
<!--需要激活 <profile>中的ID才生效-->
<activeProfile>nexus-pr</activeProfile>
</activeProfiles>
3.2 项目中配置私服
1、目的
项目通过执行maven的deploy任务可以将release或者snapshot版本发布到nexus上
2、方式
pom.xml中添加:
<distributionManagement>
<repository>
<!--id的名字可以任意取,但是在setting文件中的属性<server>的ID与这里一致-->
<id>releases</id>
<!--指向仓库类型为host(宿主仓库)的储存类型为Release的仓库-->
<url>http://10.20.30.158:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<!--指向仓库类型为host(宿主仓库)的储存类型为Snapshot的仓库-->
<url>http://10.20.30.158:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
</project>
其中maven-releases与maven-snapshots区别,是体现在创建仓库时【version pollcy版本策略】属性的设置上:
工程中通过pom.xml中version的内容(版本号中-SNAPSHOT部分)来决定deploy时发布到哪个库中
3.3 Maven deploy详解
1、maven中的仓库分为两种
- snapshot快照仓库:snapshot快照仓库用于保存开发过程中的不稳定版本,
- release发布仓库:release正式仓库则是用来保存稳定的发行版本。
定义一个maven项目(project或者模块)为快照版本,只需要在pom文件中在该模块的version版本号后加上-SNAPSHOT即可(注意这里必须是大写),如下:
<version>1.0-SNAPSHOT</version>
如果带有“-SNAPSHOTS” 打包快照版本,否则即为线上版本,约定俗成的命名,如下:
<version>1.0-RELEASE</version>
maven会根据模块的版本号(pom文件中的version)中是否带有-SNAPSHOT来判断是快照版本还是正式版本。
- 如果是快照版本,那么在mvn deploy时会自动发布到快照版本库中,使用快照版本的模块,在不更改版本号的情况下,直接编译打包时,maven【会自动从镜像服务器上下载最新的快照版本】
- 如果是正式发布版本,那么在mvn deploy时会自动发布到正式版本库中,而使用正式版本的模块,在不更改版本号的情况下,编译打包时如果本地已经存在该版本的模块则不会主动去镜像服务器上下载
所以,最佳实践:我们在开发阶段,可以将公用库的版本设置为【快照版本】,而被依赖组件则引用快照版本进行开发,在公用库的快照版本更新后,我们也不需要修改pom文件提示版本号来下载新的版本,直接mvn执行相关编译、打包命令即可重新下载最新的快照库了,从而也方便了我们进行开发。
如果是快照版本,那么在mvn deploy时会自动发布到快照版本库中,而使用快照版本的模块,在不更改版本号的情况下,直接编译打包时,maven会自动从镜像服务器上下载最新的快照版本。 SNAPSHOT是不稳定版,可能是还在开发中的版本,在开发时用户A可能每天都会更新代码,可能会频繁的发布版本。而另一组用户B需要实时得到A的最新代码版本,以进行同步开发。如果使用RELEASE仓库需要不停的更换坐标,才能升级到最新版本。而SNAPSHOT仓库则不需要这样做,用户A和用户B都不用升级版本。用户A每次发布时会根据当时时间创建一个新的快照版本,之前的快照版本也会保留成为历史版本。用户B每次构建项目时会自动根据版本时间加载最新的JAR包,这种模式更加适合于多模块同步开发测试阶段 如果是正式发布版本,那么在mvn deploy时会自动发布到正式版本库中,而使用正式版本的模块,在不更改版本号的情况下,编译打包时如果本地已经存在该版本的模块则不会主动去镜像服务器上下载。 所以,我们在开发阶段,可以将公用库的版本设置为快照版本,而被依赖组件则引用快照版本进行开发,在公用库的快照版本更新后,我们也不需要修改pom文件提示版本号来下载新的版本,直接mvn执行相关编译、打包命令即可重新下载最新的快照库了,从而也方便了我们进行开发。 用户A将代码打包发布到RELEASE仓库,具体操作参考上篇文章。用户B使用时,需要在pom.xml添加JAR包的依赖坐标。如果用户A将版本从1.0升级为2.0,用户B使用时也需要同时在pom.xml中修改坐标版本。但是RELEASE是稳定版本,是经过测试以后才会发布的,通常不会频繁的升级版本 |
4.4 完整的pom.xml示例
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<!--
| This is the configuration file for Maven. It can be specified at two levels:
|
| 1. User Level. This settings.xml file provides configuration for a single user,
| and is normally provided in ${user.home}/.m2/settings.xml.
|
| NOTE: This location can be overridden with the CLI option:
|
| -s /path/to/user/settings.xml
|
| 2. Global Level. This settings.xml file provides configuration for all Maven
| users on a machine (assuming they're all using the same Maven
| installation). It's normally provided in
| ${maven.conf}/settings.xml.
|
| NOTE: This location can be overridden with the CLI option:
|
| -gs /path/to/global/settings.xml
|
| The sections in this sample file are intended to give you a running start at
| getting the most out of your Maven installation. Where appropriate, the default
| values (values used when the setting is not specified) are provided.
|
|-->
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
<localRepository>D:\06.tools\maven-repository</localRepository>
<!-- interactiveMode
| This will determine whether maven prompts you when it needs input. If set to false,
| maven will use a sensible default value, perhaps based on some other setting, for
| the parameter in question.
|
| Default: true
<interactiveMode>true</interactiveMode>
-->
<!-- offline
| Determines whether maven should attempt to connect to the network when executing a build.
| This will have an effect on artifact downloads, artifact deployment, and others.
|
| Default: false
<offline>false</offline>
-->
<!-- pluginGroups
| This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
| when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
| "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
|-->
<pluginGroups>
<!-- pluginGroup
| Specifies a further group identifier to use for plugin lookup.
<pluginGroup>com.your.plugins</pluginGroup>
-->
</pluginGroups>
<!-- proxies
| This is a list of proxies which can be used on this machine to connect to the network.
| Unless otherwise specified (by system property or command-line switch), the first proxy
| specification in this list marked as active will be used.
|-->
<proxies>
<!-- proxy
| Specification for one proxy, to be used in connecting to the network.
|
<proxy>
<id>optional</id>
<active>true</active>
<protocol>http</protocol>
<username>proxyuser</username>
<password>proxypass</password>
<host>proxy.host.net</host>
<port>80</port>
<nonProxyHosts>local.net|some.host.com</nonProxyHosts>
</proxy>
-->
</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
| Specifies the authentication information to use when connecting to a particular server, identified by
| a unique name within the system (referred to by the 'id' attribute below).
|
| NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
| used together.
|
<server>
<id>deploymentRepo</id>
<username>repouser</username>
<password>repopwd</password>
</server>
-->
<!-- Another sample, using keys to authenticate.
<server>
<id>siteServer</id>
<privateKey>/path/to/private/key</privateKey>
<passphrase>optional; leave empty if not used.</passphrase>
</server>
-->
<server>
<id>releases</id> <!--对应pom.xml的id=releases的仓库-->
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshots</id> <!--对应pom.xml中id=snapshots的仓库-->
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>nexus-ma</id>
<username>admin</username>
<password>admin123</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
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
<mirror>
<id>nexus-ma</id>
<name>internal nexus repository</name>
<!--镜像采用配置好的组的地址-->
<url>http://10.20.30.158:8081/repository/maven-public/</url>
<mirrorOf>*</mirrorOf>
</mirror>
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</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
| Specifies a set of introductions to the build process, to be activated using one or more of the
| mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
| or the command line, profiles have to have an ID that is unique.
|
| An encouraged best practice for profile identification is to use a consistent naming convention
| for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
| This will make it more intuitive to understand what the set of introduced profiles is attempting
| to accomplish, particularly when you only have a list of profile id's for debug.
|
| This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
<profile>
<id>jdk-1.4</id>
<activation>
<jdk>1.4</jdk>
</activation>
<repositories>
<repository>
<id>jdk14</id>
<name>Repository for JDK 1.4 builds</name>
<url>http://www.myhost.com/maven/jdk14</url>
<layout>default</layout>
<snapshotPolicy>always</snapshotPolicy>
</repository>
</repositories>
</profile>
-->
<!--
| Here is another profile, activated by the system property 'target-env' with a value of 'dev',
| which provides a specific path to the Tomcat instance. To use this, your plugin configuration
| might hypothetically look like:
|
| ...
| <plugin>
| <groupId>org.myco.myplugins</groupId>
| <artifactId>myplugin</artifactId>
|
| <configuration>
| <tomcatLocation>${tomcatPath}</tomcatLocation>
| </configuration>
| </plugin>
| ...
|
| NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
| anything, you could just leave off the <value/> inside the activation-property.
|
<profile>
<id>env-dev</id>
<activation>
<property>
<name>target-env</name>
<value>dev</value>
</property>
</activation>
<properties>
<tomcatPath>/path/to/tomcat/instance</tomcatPath>
</properties>
</profile>
-->
<profile>
<!--ID用来确定该profile的唯一标识-->
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
<profile>
<id>nexus-pr</id>
<!-- 远程仓库列表 -->
<repositories>
<repository>
<id>nexus-ma</id>
<name>Nexus Central</name>
<!-- 虚拟的URL形式,指向镜像的URL-->
<url>http://10.20.30.158:8081/repository/maven-public/</url>
<layout>default</layout>
<!-- 表示可以从这个仓库下载releases版本的构件-->
<releases>
<enabled>true</enabled>
</releases>
<!-- 表示可以从这个仓库下载snapshot版本的构件 -->
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<!-- 插件仓库列表 -->
<pluginRepositories>
<pluginRepository>
<id>nexus-ma</id>
<name>Nexus Central</name>
<url>http://10.20.30.158:8081/repository/maven-public/</url>
<layout>default</layout>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<!--需要激活 <profile>中的ID才生效-->
<activeProfile>nexus-pr</activeProfile>
<activeProfile>jdk-1.8</activeProfile>
</activeProfiles>
<!-- activeProfiles
| List of profiles that are active for all builds.
|
<activeProfiles>
<activeProfile>alwaysActiveProfile</activeProfile>
<activeProfile>anotherAlwaysActiveProfile</activeProfile>
</activeProfiles>
-->
</settings>
五、常见问题
1、登录时,默认密码提示超时处理方式
问题现象
Incorrect username or password, or no permission (Nexus3 登陆密码不为 admin123 、重置登陆密码)
问题分析及处理
后来注意到在登陆时有提示信息 ,密码保存在 /nexus-data下的 admin.password 中
找到admin.password ,查看并复制密码登录即可,然后登录后会提示修改密码:
注意密码只有这一部分:cb424ffc-fc4b-41c1-9cbf-d7ea26efd978,紧跟后面的 bash-4.2$ 不是密码内容
2、为Nexus配置阿里云代理仓库
问题现象
Nexus默认远程仓库为maven的中央仓库,https://repo1.maven.org/maven2/,国内访问慢导致下载很慢
问题分析及处理
方式1:直接修改nexus自带的maven-centra代理库url为:http://maven.aliyun.com/nexus/content/groups/public
方式二:添加阿里云代理仓库将其加入maven-public并设置顺序在maven-central代理库前面
1)添加代理仓库,create repository -> maven2 proxy
填写:设置仓库名称,仓库地址,仓库存储的位置(default就行)
name:nexus-aliyun
remote storage:http://maven.aliyun.com/nexus/content/groups/public
把nexus-aliyun添加到仓库组中,记得把nexus-aliyun排在maven-central代理库上边。
这样,就可以优先访问阿里云仓库了。
六、参考
1)安装配置运行
https://www.cnblogs.com/breeze-zZ/p/11049743.html
2)发布项目到私服
https://www.cnblogs.com/lenovo_tiger_love/p/10309782.html
3)maven的pom.xml配置详解(也有在linux安装nexus过程)
https://blog.csdn.net/luoww1/article/details/87718932?utm_medium=distribute.pc_relevant.none-task-blog-baidujs-7
4)登录时,默认密码提示超时处理方式
https://cloud.tencent.com/developer/article/1478468
5)为Nexus配置阿里云代理仓库
https://www.cnblogs.com/godwithus/p/8955824.html