maven入门:nexus安装与使用(二)

来源:互联网 发布:sqlserver读写分离 编辑:程序博客网 时间:2024/05/21 17:11

目录

  • 目录
  • 下载
  • 安装
  • 访问
  • 建立仓库组
  • 配置镜像
  • 把jar包在私有工厂和同事共享
  • 创建某个项目的仓库

1.下载 :

nexus官方下载地址 我下载的2.x版本,3.x版本要求jdk1.8

2.安装 :

cd /usr/localtar -xzvf nexus-2.14.4-03-bundle.tar.gzln -s nexus-2.14.4-03-bundle nexus (创建软连接)vi /etc/profile添加export RUN_AS_USER=rootsource /etc/profilecd /usr/local/nexus/bin && ./nexus start

3.访问

118.212.149.xx:8081/nexus/ 默认账号:admin 密码:admin123

默认仓库列表
七个仓库从上至下分别为: 仓库组,第三方库,apache Snapshots(研发阶段)库,中央仓库,中央仓库-M1格式(虚拟仓库),本地Releases(稳定版本)库,本地Snapshots库.

4.建立仓库组:

创建仓库组
在pom.xml中配置如下:

<!-- 设置私有工厂 --><repositories>    <repository>        <id>nexus</id>        <name>Nexus R</name>        <url>http://118.212.149.xx:8081/nexus/content/groups/public/</url>    </repository></repositories>

pom配置私有工厂有弊端,换个项目还要重新配置,所以在maven的setting.xml中配置,配置如下:

</profiles> <profile>  <id>nexusRepo</id>   <repositories>    <repository>        <id>nexus</id>        <name>Nexus R</name>        <url>http://118.212.149.xx:8081/nexus/content/groups/public/</url>        <releases>            <enabled>true</enabled>        </releases>        <snapshots>            <enabled>true</enabled>        </snapshots>    </repository>  </repositories> </profile></profiles><!-- 激活 --><activeProfiles> <activeProfile>nexusRepo</activeProfile></activeProfiles>

5.配置镜像

配置完私有工厂后,如果在私有工厂找不到的jar依然会到中央工厂下载.如果私有工厂找不到的话,不让maven在中央工厂下载需要配置镜像
目的有两个:
1.中央仓库下载速度较慢,所以配置镜像.起到映射中央仓库地址的作用(如:公司访问中央仓库很快,但家里很慢,我们可以在家里的setting.xml配置镜像,把对中央仓库的请求转发到我们配置的镜像地址上去.).
2.强制开发人员只到对应镜像下载jar,这样mirroOf建议配置*

<mirrors><!--只要mirrorOf中的工厂被访问,都会自动找镜像,如果镜像无法访问就不会再去中央工厂访问,使用*代表所有远程工厂-->  <mirror>    <id>nexusMirro</id>    <mirrorOf>*</mirrorOf>    <name>Human Readable Name for this Mirror.</name>    <url>http://118.212.149.60:8081/nexus/content/groups/public/</url>  </mirror></mirrors>

这里mirrorOf标签说明:

  • * 匹配所有远程仓库
  • external:* 匹配所有远程仓库,使用localhost的除外,使用file://协议的除外。也就是说,匹配所有不在本机上的远程仓库
  • repo1,repo2 匹配仓库repo1和repo2,使用逗号分隔多个远程仓库
  • *,!repo1 匹配所有远程仓库,repo1除外,使用感叹号将仓库从匹配中排除。

maven官方说明: Using Mirrors for Repositories

2017-07-21补充:
如果我们配置了mirrors为*后刚才已经激活的nexusRepo工厂就不需要激活了.因为把所有工厂都已经镜像到了nexus中.但是有一点大家要注意.默认的中央工厂是没有开启snapshots包下载的,也就是

<snapshots>  <enabled>true</enabled></snapshots>

所以我们可以采用如下配置覆盖中央工厂配置.

<profiles>      <profile>      <id>centralProfile</id>       <repositories>        <repository>            <id>central</id>            <name>central R</name>            <url>http://*</url>            <releases>                <enabled>true</enabled>            </releases>            <snapshots>                <enabled>true</enabled>            </snapshots>        </repository>      </repositories>    </profile></profiles><activeProfiles>  <activeProfile>centralProfile</activeProfile> </activeProfiles>

url可以乱写,因为对于mirrors来讲,只是把url做了镜像,但是其他配置还是遵循repository中的配置.

6.把jar包在私有工厂和同事共享

在pom中添加如下配置:

<!-- 发布管理 --><distributionManagement>    <repository>        <id>user-release</id>        <name>user release resp</name>        <url>http://118.212.149.xx:8081/nexus/content/repositories/releases/</url>    </repository>  <snapshotRepository>        <id>user-snapshots</id>        <name>user snapshots resp</name>        <url>http://118.212.149.xx:8081/nexus/content/repositories/snapshots/</url>    </snapshotRepository></distributionManagement>

setting.xml中配置账号密码

<server>     <id>user-releases</id>     <username>deployment</username>     <password>123456</password>   </server>    <server>     <id>user-snapshots</id>     <username>deployment</username>     <password>123456</password></server>

deploy命令将jar发布到私有工厂

7.创建某个项目的仓库

这个大家根据实际项目来吧,创建完仓库还要配置角色权限什么的,都不难.

原创粉丝点击