1 .搭建私服原因
搭建mavn私服的原因在于 可能公司内部开发人员无法上外网 这时 需要搭建一个中介平台(私服) 该平台(需要联网)可以从外部的网络中获取需要的依赖库 缓存在私服中
开发环境中需要配置私服的地址 开发环境发起依赖包的请求到私服时 如果私服中存在该依赖库则返回 没有则联网同步
2. 搭建私服
2.1 安装包搭建maven私服
1 >私服的搭建需要借助软件Nexus 下载地址: https://www.sonatype.com/download-oss-sonatype
2 >进入nexus目录bin目录下 安装并启动服务 nexus.bat install
nexus-2.1.1\bin>nexus install
wrapper | nexus installed.
nexus-2.1.1\bin>nexus start
wrapper | Starting the nexus service...
wrapper | Waiting to start...
wrapper | nexus started.
找到 conf目录下nexus.properties 文件查看应用端口 application-port=8081 通过浏览器访问 http://localhost:8081/nexus [该地址为搜索依赖库地址]
右上角 Log in点击登录就可以管理私服仓库 用户名 admin密码 admin123 点击左侧Repositories 可以看到Central中央仓库的地址
在中央仓库上点击右键 updateindex 会自动到外网更新依赖库的索引 速度很慢 这里可以先下载索引文件后 更新nexus缓存中
步骤如下:
apache官网下载index文件 地址: http://repo.maven.apache.org/maven2/.index/ 下载如下两个文件
nexus-maven-repository-index.gz 2017-04-30 14:09 355151742
nexus-maven-repository-index.properties
下载解压工具包(search.maven.org搜索indexer-cli下载indexer-cli-5.1.1.jar)
http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22org.apache.maven.indexer%22
将下载的indexer的jar和下载的index文件和properties文件放在同一个目录下
执行该jar会自动解压 该索引文件到indexer目录下(解压时间需要几分钟 根据机器速度而定)
java -jar indexer-cli-5.1.1.jar -u nexus-maven-repository-index.gz -d indexer
然后将indexer目录下的文件全部拷贝到 %NEXUS_HOME%\sonatype-work\nexus\indexer\central-ctx 覆盖
以上这种方式更新索引一直没有成功 现在推荐另外一种
使用tomcat模拟 http://repo1.maven.org/maven2/.index/的路径
安装一个tomcat 在tomcat的webapps目录下 新建目录maven2 maven2下新建.index文件夹 将下载的index文件和properties文件拷贝到这个.index目录下
假设tomcat端口为8080 可以通过路径 http://localhost:8080/maven2/.index/nexus-maven-repository-index.properties 访问测试是否部署成功
点击central点击下面的配置选项 修改nexus上更新索引的位置 会自动更新索引
点击save后 点击左侧菜单的Administrators下的Scheduled Tasks可以看到正在更新索引 因为是从本地tomcat更新索引所以一般几分钟就更新完成了 在更新的过程中可以
查看 sonatype-work\nexus\tmp目录下的文件nexus-maven-repository-index.gz开头的目录 一直增大到几个G 更新完成这个目录会自动被拷贝到indexer目录下
索引更新完成就可以 browse index查看是否更新成功
在中心库中使用本地索引 方便我们快速搜索依赖库的配置
这里我们需要将开始设置的Remote Storage Location 从刚开始修改后的 http://localhost:8080/maven2修改为原始的镜像地址,并且将download remote indexs修改为false 否则无法找不到jar包 会报搜索出错
这里 以前的值是 http://repo.maven.apache.org/maven2/ 由于到国外服务器下载所以速度非常慢 这里可以替换为 aliyun的maven地址 毕竟在国内
如果直接修改后save 会重新更新索引前面工作等于白做 这里考虑 修改配置文件 不要点击save
找到sonatype-work\nexus\conf\nexus.xml 该文件
找到central的库配置
repositories>
<repository>
<id>central</id>
<name>Central</name>
<providerRole>org.sonatype.nexus.proxy.repository.Repository</providerRole>
<providerHint>maven2</providerHint>
<localStatus>IN_SERVICE</localStatus>
<notFoundCacheActive>true</notFoundCacheActive>
<notFoundCacheTTL>1440</notFoundCacheTTL>
<userManaged>true</userManaged>
<exposed>true</exposed>
<browseable>true</browseable>
<writePolicy>READ_ONLY</writePolicy>
<indexable>true</indexable>
<searchable>true</searchable>
<localStorage>
<provider>file</provider>
</localStorage>
<remoteStorage>
<provider>apacheHttpClient3x</provider>
<url>http://maven.aliyun.com/nexus/content/groups/public</url> <!--这里修改为aliyun的地址即可-->
</remoteStorage>
重启nexus 通过maven客户端配置到public的镜像地址后 可以新建eclipse maven项目测试是否能够正常添加jar包
maven客户端找到 conf/settting.xml
<mirrors>
<mirror>
<id>central</id>
<name>central</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
2.1 docker镜像方式安装(docker私服)
1. nexus镜像安装
docker run -d \
--name nexus \
-p 8085:8081 \
-p 8086:8086 \
-p 5000:5000 \
-v nexus_data:/nexus-data \
sonatype/nexus3:latest
进入镜像查看密码
docker exec -it 镜像id bash
cat /opt/sonatype/sonatype-work/nexus3/admin.password
使用默认的用户名admin和上面密码登录后,重新修改密码
2. 创建私有仓库
Repository -> Repositories 点击右边菜单 Create repository 选择 docker (hosted)
Name:仓库的名称HTTP:仓库单独的访问端口,如 8086Hosted -> Deployment pollcy:请选择 Allow redeploy 否则无法上传 Docker 镜像。
还可以创建一个 docker (proxy) 类型的仓库连接到 DockerHub 上。再创建一个 docker (group) 类型的仓库把刚才的 hosted 与 proxy 添加在一起。主机在访问的时候默认下载私有仓库中的镜像,如果没有将连接到 DockerHub 中下载并缓存到 Nexus 中。
3. 添加访问权限
菜单 Security -> Realms 把 Docker Bearer Token Realm 移到右边的框中保存。
添加用户规则:菜单 Security->Roles->Create role 在 Privlleges 选项搜索 docker 把相应的规则移动到右边的框中然后保存。
点击Users选择用户赋予这个角色。
4. 配置仓库
点击左侧菜单Repositorys点击对应的docker仓库行,双击进入
其他选项请参考下图依次设置
5. docker测试
使用 docker login 进行测试,用户名密码与 Nexus 账号一致。
```
docker login https://nexus.example.com
Username: admin
Password:
Login Succeeded
```
通过 docker push 命令将自己的镜像推送到 Nexus:
```
docker tag nginx nexus.example.com/nginx:20200326
docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 6678c7c2e56c 6 days ago 127MB
nexus.example.com/nginx 20200326 6678c7c2e56c 6 days ago 127MB
docker push nexus.example.com/nginx:20200326
6. docker https
Docker 上传镜像操作默认使用https协议,登录使用http协议,上传镜像一般会遇到
报错http: server gave HTTP response to HTTPS client
解决方案1:
在docker客户端环境下修改 /etc/docker/daemon.json,支持http:
在insecure-registries添加nexus私服服务器的ip和5000端口。
解决方案2:
使用nginx搭建https指向nexus私服服务器的ip和5000端口。