Windows系统下Nexus搭建Maven和NPM私服

私服是架设在局域网的一种特殊的远程仓库,目的是代理远程仓库及部署第三方构件。有了私服之后,当 Maven 需要下载构件时,直接请求私服,私服上存在则下载到本地仓库;否则,私服请求外部的远程仓库,将构件下载到私服,再提供给本地仓库下载。

我们可以使用专门的 Maven 仓库管理软件来搭建私服,比如:Apache Archiva,Artifactory,Sonatype Nexus。这里我们使用 Sonatype Nexus。

下载Nexus

Nexus 专业版是需要付费的,这里我们下载开源版 Nexus OSS。下载地址:http://www.sonatype.org/nexus/go。

下载完成后,解压,解压后会看到两个文件

进入/nexus-3.14.0-04/bin,启动.exe文件,如果发生闪退,那是因为打开方式不对。

在nexus-3.14.0-04/etc下有个nexus-default.properties文件,打开里面修改其中application-port=8081为其他端口,8081这个端口比较抢手吧,经常被占用,这里我就修改为8083了。

这个时候就可以来打开nexus.exe了。

正确的打开方式:在cmd中切换到nexus.exe所在目录,然后敲入nexus.exe /run即可

之后等待一下看到

Started Sonatype Nexus OSS 3.2.1-01

表示启动成功了。

在浏览器输入localhost:8083即可看到此页面:

默认的账号admin,密码admin123.

component的介绍

component name的一些说明:

  • maven-central:maven中央库,默认从https://repo1.maven.org/maven2/拉取jar
  • maven-releases:私库发行版jar
  • maven-snapshots:私库快照(调试版本)jar
  • maven-public:仓库分组,把上面三个仓库组合在一起对外提供服务,在本地maven基础配置settings.xml中使用。

component type的一些说明:

  • hosted:类型的仓库,内部项目的发布仓库
  • releases:内部的模块中release模块的发布仓库
  • snapshots:发布内部的SNAPSHOT模块的仓库
  • 3rd party:第三方依赖的仓库,这个数据通常是由内部人员自行下载之后发布上去
  • proxy:类型的仓库,从远程中央仓库中寻找数据的仓库

NPM私服创建

如图,如果是要创建maven就选择maven, 如果需要创建npm就选择npm, 这里选择npm proxy为例

输入好name和代理仓库的地址,如果代理仓库需要验证则在HTTP那里输入验证信息,否则不需要,这里以淘宝镜像为例:https://registry.npm.taobao.org

点击create repository

之后回到列表页面,点击copy,将仓库地址复制下来待使用

NPM私服使用

在本机打开cmd, 输入: npm config set registry url(你刚刚复制的地址) 回车之后再输入npm config ls 查看你的仓库地址有没有变

输入: npm install dva 测试你的私服是否创建成功

Maven私服使用

创建方法和NPM一样

让maven项目使用nexus作为远程仓库有两种方式,第一种是在项目的pom.xml中进行更改,让单个项目使用nexus仓库;另一种是通过修改maven的配置文件settings.xml进行更改,让所有项目都使用nexus仓库。

进入maven安装目录的conf文件夹打开,修改settings.xml文件。

(1) 服务器配置

1
2
3
4
5
6
7
8
9
10
<server>  
    <id>nexus-releases</id>
    <username>admin</username>
    <password>admin123</password>
</server>
<server>
    <id>nexus-snapshots</id>
    <username>admin</username>
    <password>admin123</password>
</server>

id:这是Server的ID(不是登录进来的user),与Maven想要连接上的repository/mirror中的id元素相匹配。

username,password:这两个元素成对出现,表示连接这个server需要验证username和password。在nexus中,默认管理员用户名为admin,密码为admin123。

这里使用两个服务器配置,分别对应release和snapshot。

(2) 镜像

id,name:唯一的镜像标识和用户友好的镜像名称。id被用来区分mirror元素,并且当连接时候被用来获得相应的证书。
mirrorOf:镜像所包含的仓库的Id。例如,指向Maven central仓库的镜像(http://repo1.maven.org/maven2/),设置这个元素为central。更多的高级映射例如repo1,repo2 或者,!inhouse都是可以的。没必要一定和mirror的id相匹配。在这里mirrorOf项当然应该使用,以表明是所有仓库都会被镜像到指定的地址。

url:镜像基本的URL,构建系统将使用这个URL来连接仓库。这里应该添nexus仓库的地址,地址可以在nexus仓库页面中找到。

1
2
3
4
5
6
7
8
9
10
11
12
<mirrors>   
<mirror>
<id>nexus-releases</id>
<mirrorOf>*</mirrorOf> <!-- * 表示让所有仓库使用该镜像-->
<url>http://localhost:8081/nexus/content/groups/public</url>
</mirror>
<mirror>
<id>nexus-snapshots</id>
<mirrorOf>*</mirrorOf>
<url>http://localhost:8081/nexus/content/groups/public-snapshots</url> <!-- url为私服上copy过来的url -->
</mirror>
</mirrors>

(3)配置

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
<profiles>  
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>nexus-releases</id>
<url>http://nexus-releases</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>nexus-snapshots</id>
<url>http://nexus-snapshots</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus-releases</id>
<url>http://nexus-releases</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
<pluginRepository>
<id>nexus-snapshots</id>
<url>http://nexus-snapshots</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>

profile项代表maven的基本配置。按照maven的一贯尿性,很多xml的配置项都会有一个配置项的复数形式作为父节点,以保证该配置项可以配置多个。在profiles项中,当然也可以配置多个profile,不过在这里配一个就够了。下面介绍profile项的各个子节点。

id:用来确定该profile的唯一标识。

repositories/repository:用以规定依赖包仓库的相关信息。在下属节点中,id就不用多说了;URL是指仓库地址,这里使用伪造的地址,否则即使设置了mirror,maven也有可能会直接从中央仓库下载包;releases和snapshots放在一块说吧,这两个节点下属的enable节点用以规定对应的依赖包是否对当前策略有效,假如将snapshot的enable项设为disable,则不会下载snapshot包,这两个节点还有updatePolicy,checksumPolicy和layout属性,这里就不多介绍了,有兴趣的查查文档吧。

pluginRepositories/pluginRepository:用以规定插件仓库的相关信息。其下属节点与repository的相同,不多说了。

(4) 当前启用配置

1
2
3
<activeProfiles>  
<activeProfile>nexus</activeProfile>
</activeProfiles>

用以规定当前启用的配置,将对应profile的ID加入到这一项即可使profile生效。