仓库管理器也叫私服代理仓库
  仓库管理器有两个服务目的:首先它的角色是一个高度可配置的介于你的组织与公开Maven仓库之间的代理,其次它为你的组织提供了一个可部署你组织内部生成的构件的地方。

1Nexus介绍

  Nexus是一个强大的Maven仓库管理器, 它极大地简化了自己内部仓库的维护和外部仓库的访问。 利用Nexus你可以只在一个地方就能够完全控制访问和部署在你所维护仓库中的每个Artifact。Nexus是一套“开箱即用”的系统不需要数据库,它使用文件系统加Lucene来组织数据。 Nexus使用ExtJS来开发界面,利用Restlet来提供完整的REST APIs,通过m2eclipse与Eclipse集成使用。 Nexus支持WebDAV与LDAP安全身份认证。

2安装Nexus

2.1下载

下载地址:https://www.sonatype.com/download-oss-sonatype

云盘下载:https://pan.baidu.com/s/1ZivSkF8KKAoI1NeP-Ne4-A 提取码:o7wb

2.2安装

解压到非中文路径下

nexus下载远程仓库的索引 nexus搭建仓库_Java

在cmd下切换到bin目录执行 nexus install

nexus下载远程仓库的索引 nexus搭建仓库_nexus下载远程仓库的索引_02

以管理员身份打开cmd,否则有可以出现拒绝访问的问题

启动服务

nexus start

nexus下载远程仓库的索引 nexus搭建仓库_nexus下载远程仓库的索引_03

2.3测试访问

地址:http://localhost:8081/nexus
默认 账号: admin 密码: admin123

nexus下载远程仓库的索引 nexus搭建仓库_私服_04

nexus下载远程仓库的索引 nexus搭建仓库_Java_05

3nexus介绍

nexus下载远程仓库的索引 nexus搭建仓库_maven_06

3.1 仓库类型

名称

说明

group

仓库组,包含宿主仓库和代理仓库

hosted

代表宿主仓库,存放本公司开发jar包,以及一些第三方不允许的组件,比如oracle驱动、比如商业软件jar包

proxy

代表代理远程的仓库,最典型的就是Maven官方中央仓库、JBoss仓库等等

virtual

虚拟库

3.2 默认仓库介绍

仓库名称

描述

Public Repositories

仓库组

3rd party

无法从公共仓库获得的第三方发布版本的构件仓库

Apache Snapshots

用了代理ApacheMaven仓库快照版本的构件仓库

Central

用来代理maven中央仓库中发布版本构件的仓库

Central M1 shadow

用于提供中央仓库中M1格式的发布版本的构件镜像仓库

Codehaus Snapshots

用来代理CodehausMaven 仓库的快照版本构件的仓库

Releases

用来部署管理内部的发布版本构件的宿主类型仓库

Snapshots

用来部署管理内部的快照版本构件的宿主类型仓库

4nexus配置

4.1开启远程索引

方式一:

  新搭建的neuxs环境只是一个空的仓库,需要手动和远程中心库进行同步,nexus默认是关闭远程索引下载,最重要的一件事情就是开启远程索引下载。登陆nexus系统,默认用户名密码为admin/admin123。点击左边Administration菜单下面的Repositories,找到右边仓库列表中的二个仓库Apache Snapshots,Maven Central,然后再没有仓库的configuration下把Download Remote Indexes修改为true。如下图

nexus下载远程仓库的索引 nexus搭建仓库_nexus_07


  然后在Apache Snapshots,Codehaus Snapshots和Maven Central这三个仓库上分别右键,选择Repari Index,这样Nexus就会去下载远程的索引文件。

nexus下载远程仓库的索引 nexus搭建仓库_nexus_08


  这样设置以后, Nexus会自动从远程中央仓库下载索引文件, 为了检验索引文件自动下载是否生效,切换到Browse Index

这种方式需要从远程站点下载,速度相对比较慢。

方式二、

  直接给大家提供下载好的仓库索引文件,直接覆盖就可以了。

链接:https://pan.baidu.com/s/1_Rofajqlt4LaI8B_l9sUCg

提取码:616l

步骤:

1.先停止nexus服务

nexus下载远程仓库的索引 nexus搭建仓库_nexus_09


2.将给你文件解压缩。

3.删除该目录下所有文件%Nexus_Home%\sonatype-work\nexus\indexer\central-ctx。然后将解压的文件复制进去,

nexus下载远程仓库的索引 nexus搭建仓库_nexus_10


4.重启服务即可。

nexus下载远程仓库的索引 nexus搭建仓库_nexus下载远程仓库的索引_11

这样后面就不用再去官网查找了!!!

4.2创建宿主仓库

新建公司的内部仓库:
步骤为Repositories –> Add –> Hosted Repository,在页面的下半部分输入框中填入Repository ID和Repository Name即可,比如分别填入
myId 和 myRepostiory,另外把Deployment Policy设置为Allow Redeploy,点击save就创建完成了。

nexus下载远程仓库的索引 nexus搭建仓库_maven_12


nexus下载远程仓库的索引 nexus搭建仓库_nexus_13

nexus下载远程仓库的索引 nexus搭建仓库_Java_14

4.3 私服中配置代理

  虽然搭建了私服,但是如果私服中没有我们需要的jar包,它还是会去中央仓库去下载,这是速度同样很慢,这时我们可以在nexus中添加阿里的代理服务器。步骤如下:

nexus下载远程仓库的索引 nexus搭建仓库_nexus下载远程仓库的索引_15

远程地址是:http://maven.aliyun.com/nexus/content/groups/public/

在把该代理仓库添加到group中

nexus下载远程仓库的索引 nexus搭建仓库_nexus下载远程仓库的索引_16


如此:使用的时候就会先通过阿里镜像代理去下载

4.4 创建Nexus仓库组

nexus下载远程仓库的索引 nexus搭建仓库_Java_17

4.4如何在maven中使用自己的私服。

在setting.xml文件中:

nexus下载远程仓库的索引 nexus搭建仓库_nexus下载远程仓库的索引_18


的servers节点中添加

<server>
     <id>nexus</id>
     <username>admin</username>
     <password>admin123</password>
 </server>

nexus下载远程仓库的索引 nexus搭建仓库_nexus下载远程仓库的索引_19


其次在mirrors节点中配置:

<mirror>
  <id>nexus</id>
  <mirrorOf>*</mirrorOf>
  <name>Human Readable Name for this Mirror.</name>
  <url>http://localhost:8081/nexus/content/groups/DpbGroup/</url>
</mirror>

nexus下载远程仓库的索引 nexus搭建仓库_私服_20


更新eclipse的配置

nexus下载远程仓库的索引 nexus搭建仓库_nexus_21

注意:一定把勾去掉,不然不会远程下载。

nexus下载远程仓库的索引 nexus搭建仓库_Java_22


maven项目中通过坐标下载测试。

nexus下载远程仓库的索引 nexus搭建仓库_maven_23

<dependency>
  <groupId>c3p0</groupId>
  <artifactId>c3p0</artifactId>
  <version>0.9.1.2</version>
</dependency>

nexus下载远程仓库的索引 nexus搭建仓库_nexus_24

nexus下载远程仓库的索引 nexus搭建仓库_nexus下载远程仓库的索引_25

成功

5上传jar包私服

1.在conf/setting.xml文件中配置认证

<server>
		<id>releases</id>
		<username>admin</username>
		<password>admin123</password>
	</server>
	<server>
		<id>snapshots</id>
		<username>admin</username>
		<password>admin123</password>
	</server>

nexus下载远程仓库的索引 nexus搭建仓库_nexus下载远程仓库的索引_26

2.在将要上传的pom.xml中配置jar上传的路径

<!-- 配置上传私服路径 -->
  <distributionManagement>
  	<repository>
  		<id>releases</id>
  		<url>http://localhost:8081/nexus/content/repositories/releases/</url>
  	</repository>
  	<snapshotRepository>
  		<id>snapshots</id>
  		<url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
  	</snapshotRepository>
  </distributionManagement>

nexus下载远程仓库的索引 nexus搭建仓库_Java_27

3.执行上传命令 deploy

nexus下载远程仓库的索引 nexus搭建仓库_maven_28

nexus下载远程仓库的索引 nexus搭建仓库_Java_29

nexus下载远程仓库的索引 nexus搭建仓库_私服_30

4.查看上传结果

nexus下载远程仓库的索引 nexus搭建仓库_Java_31

6.上传特定jar包到私服

Oracle驱动包

nexus下载远程仓库的索引 nexus搭建仓库_nexus下载远程仓库的索引_32

nexus下载远程仓库的索引 nexus搭建仓库_Java_33

nexus下载远程仓库的索引 nexus搭建仓库_nexus下载远程仓库的索引_34


nexus下载远程仓库的索引 nexus搭建仓库_nexus_35

nexus下载远程仓库的索引 nexus搭建仓库_Java_36

<dependency>
  <groupId>com.dpb.oracle</groupId>
  <artifactId>ojdbc6-dpb</artifactId>
  <version>1.0.0</version>
</dependency>

通过此坐标使用

7.从私服上下载jar包

7.1在conf/setting.xml中配置 模板

<profile>
		<id>dev</id>
		<repositories>
			<repository>
				<id>nexus</id>
				<url>http://localhost:8081/nexus/content/groups/public/</url>
				<!--是否下载releases构件 -->
				<releases>
					<enabled>true</enabled>
				</releases>
				<!--是否下载snapshots构件 -->
				<snapshots>
					<enabled>true</enabled>
				</snapshots>
			</repository>
		</repositories>
		
		<pluginRepositories>
			<pluginRepository>
				<id>public</id>
				<name>Public_Repositories</name>
				<url>http://localhost:8081/nexus/content/groups/public/</url>
			</pluginRepository>
		</pluginRepositories>
	</profile>

nexus下载远程仓库的索引 nexus搭建仓库_私服_37

7.2激活模板

<!-- 激活模板-->
  <activeProfiles>
	<activeProfile>dev</activeProfile>
  </activeProfiles>

nexus下载远程仓库的索引 nexus搭建仓库_私服_38

7.3测试使用

使用Oracle的jar包

nexus下载远程仓库的索引 nexus搭建仓库_nexus下载远程仓库的索引_39

上一篇:Maven教程3(依赖管理) 下一篇:maven教程5(聚合工程)