Maven教程4(私服-nexus)

仓库管理器也叫 私服 或 代理仓库 仓库管理器有两个服务目的:首先它的角色是一个高度可配置的介于你的组织与公开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安装
解压到非中文路径下

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

以管理员身份打开cmd,否则有可以出现拒绝访问的问题
启动服务
nexus start

2.3测试访问
地址: http://localhost:8081/nexus 默认 账号: admin 密码: admin123


3nexus介绍

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。如下图

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

这样设置以后, Nexus会自动从远程中央仓库下载索引文件, 为了检验索引文件自动下载是否生效,切换到Browse Index
这种方式需要从远程站点下载,速度相对比较慢。
方式二、
直接给大家提供下载好的仓库索引文件,直接覆盖就可以了。 链接: https://pan.baidu.com/s/1_Rofajqlt4LaI8B_l9sUCg 提取码:616l 1.先停止nexus服务

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

4.重启服务即可。

这样后面就不用再去官网查找了!!!
4.2创建宿主仓库
新建公司的内部仓库: 步骤为Repositories –> Add –> Hosted Repository,在页面的下半部分输入框中填入Repository ID和Repository Name即可,比如分别填入 myId 和 myRepostiory,另外把Deployment Policy设置为Allow Redeploy,点击save就创建完成了。



4.3创建Nexus仓库组

4.4如何在maven中使用自己的私服。
在setting.xml文件中:

的servers节点中添加
<server>
<id>nexus</id>
<username>admin</username>
<password>admin123</password>
</server>

其次在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>

更新eclipse的配置

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

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

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


成功
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>

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>

3.执行上传命令 deploy



4.查看上传结果

6.上传特定jar包到私服
Oracle驱动包





<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>

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