Linux使用docker搭建Maven私有仓库

(一)引言

在实际开发工作中,通常需要搭建maven私有仓库。

(二)Nexus介绍

Nexus 是Maven仓库管理器,如果你使用Maven,你可以从Maven中央仓库 下载所需要的构件(artifact),但这通常不是一个好的做法,你应该在本地架设一个Maven仓库服务器,在代理远程仓库的同时维护本地仓库,以节省带宽和时间,Nexus就可以满足这样的需要。此外,他还提供了强大的仓库管理功能,构件搜索功能,它基于REST,友好的UI是一个extjs的REST客户端,它占用较少的内存,基于简单文件系统而非数据库。这些优点使其日趋成为最流行的Maven仓库管理器。

(三)安装docker

        请参考Docker安装_Icoolkj的博客

(四)docker中安装nexus3

1、拉取镜像

## 通过docker search nexus  命令搜索一下docker公有库在的 nexus相关的镜像
[root@icoolkj ~]# docker search nexus
## 拉取nexus3镜像
[root@icoolkj /]# docker pull sonatype/nexus3
Using default tag: latest
latest: Pulling from sonatype/nexus3
f70d60810c69: Pull complete 
545277d80005: Pull complete 
10b49635409a: Pull complete 
Digest: sha256:3fd7e90bcf49fb55d87d852cab854e5669ed115b09bdb25f47c45ee0797231aa  147.6MB/295.3MB
Status: Downloaded newer image for sonatype/nexus3:latest
docker.io/sonatype/nexus3:latest
[root@icoolkj /]# 

提示:可以去docker hub网站搜索对应的软件,掌握docker命令如何写9e58a0e5e73e9d110117208220dee5f6.png

## 查看镜像
[root@icoolkj /]# docker images
REPOSITORY        TAG       IMAGE ID       CREATED       SIZE
sonatype/nexus3   latest    aab1398bb647   2 weeks ago   705MB
[root@icoolkj /]# 

2、建立数据储存文件夹

## 建立数据存放文件夹,用于docker中nexus的数据与本地物理机映射
[root@icoolkj /]# mkdir -p /usr/local/nexus3/nexus-data
[root@icoolkj /]# ll /usr/local/nexus3/
总用量 4
drwxr-xr-x 2 root root 4096 6月   3 18:06 nexus-data
## 更改权限
[root@icoolkj /]# chmod 777 /usr/local/nexus3/nexus-data/
[root@icoolkj /]# ll /usr/local/nexus3/
总用量 4
drwxr-xr-x 2 777 root 4096 6月   3 18:06 nexus-data
[root@icoolkj /]# 

3、安装并运行容器

## 编写启动脚本docker-nexus3-start.sh
docker rm -f docker-nexus3 || true
docker run --name docker-nexus3 \
-p 8081:8081 \
-v /usr/local/nexus3/nexus-data:/nexus-data \
--restart=always \
-d sonatype/nexus3

## 参数说明
--name nexus #启动该容器的名字,可以自己更改为自己想要的名字
-p 8081:8081 #端口映射,将本地8081端口映射为容器的8081端口,第一个8081可以改成自己想要放开的端口
-v /docker/nexus/nexus-data:/nexus-data # 将容器的/nexus-data地址 代理到 本地/docker/nexus/nexus-data文件夹下
--restart=always #在重启docker时,自动重启改容器。
-d sonatype/nexus3 #即为后台运行一直sonatype/nexus3

## 运行脚本
[root@icoolkj nexus3]# sh docker-nexus3-start.sh  
docker-nexus3
667beabb4a5e712a0afe803640dba5db571b8a2fb5e4a09be7365a6b8d3b7852
[root@icoolkj nexus3]# 

 4、获取容器的日志

[root@icoolkj nexus3]# docker logs -f --tail 20 docker-nexus3

(五)使用nexus3

 1、浏览器访问

 2、配置Nexus

 提示输入密码,并告知你的密码储存位置

Your admin user password is located in /nexus-data/admin.password on the server.

因为docker中nexus3的数据储存位置与本地物理机建立了映射关系,所有在物理机上的地址应该是/usr/local/nexus3/nexus-data/admin.password

登录成功后需要更改密码,更改密码需要记住(浏览器都有记住密码的功能,顺⼿点保存⾯,下次你直接登录就好了);更改密码完成之后,admin.password⽂件⾃动删除!!!

## 默认仓库说明
maven-central:maven中央库,默认从https://repo1.maven.org/maven2/拉取jar
maven-releases:私库发行版jar,初次安装请将Deployment policy设置为Allow redeploy
maven-snapshots:私库快照(调试版本)jar
maven-public:仓库分组,把上面三个仓库组合在一起对外提供服务,在本地maven基础配置settings.xml或项目pom.xml中使用
## Nexus仓库类型介绍
hosted:本地仓库,通常我们会部署自己的构件到这一类型的仓库。比如公司的第二方库。
proxy:代理仓库,它们被用来代理远程的公共仓库,如maven中央仓库。
group:仓库组,用来合并多个hosted/proxy仓库,当你的项目希望在多个repository使用资源时就不需要多次引用了,只需要引用一个group即可。

 如图所示,代理仓库负责代理远程中央仓库,托管仓库负责本地资源,组资源库 = 代理资源库 + 托管资源库

3、配置阿里云代理仓库

1)、新建仓库(Create repository)

Repository-->Repositories-->Create repository-->maven2(proxy)

填写仓库名称——maven-aliyun,并填入仓库url

https://maven.aliyun.com/repository/public

d00918d082d7bec808111717530dbe36.png

2)、配置仓库组

(默认已有一个maven-public)

20f0f9bf096a1d39da44f086ca21e863.png

 注:注意仓库顺序。maven查找依赖时会依次遍历仓库组中的仓库。

## 官方文档中建议:
It is recommended practice to place hosted repositories higher in the list than proxy repositories. For proxy repositories, the repository manager needs to check the remote repository which will incur more overhead than a hosted repository lookup.
希望将hosted repositories【托管资源库】的顺序放在proxy repositories【代理资源库】之前,因为一个group【组资源库】中可以涵括这些托管资源库和代理资源库。而一整个的group是作为一个public,一个接口给别人使用的。所以当查找架包的时候,如果代理资源库在前面,那就是先从远程去查找jar,而不是先从托管资源库(本地仓库)去查找是否有jar。这样访问外网的消耗比起来在本地查找,当然是将托管资源库放在代理资源库之前的优先位置了。

4、创建角色

创建角色(develop),并分配nx-all权限

Security-->Roles-->Create

f82159b9b2302cc1c52f0e82bd8ec5ba.png

注:创建角色的同时可以为当前创建的角色分配权限。

5、创建用户

创建用户(developer),并授予develop角色

Security-->Users-->Create

355b16e69b3d719362c668b432596802.png

注:创建用户并为创建的用户挂上相应的角色。

(六)maven配置文件

Maven下的setting.xml文件和项目中的pom.xml文件的关系是:settting.xml文件是全局设置,而pom.xml文件是局部设置。pom.xml文件对于项目来说,是优先使用的。而pom.xml文件中如果没有配置镜像地址的话,就按照settting.xml中定义的地址去查找。

修改本地maven配置文件(conf/setting.xml)

servers节点下添加以下内容(username和password为刚刚在nexus3中添加的用户和其密码)

  <!--nexus服务器,id为组仓库name-->
  <servers>
	<server>
	  <id>maven-public</id>
	  <username>developer</username>
	  <password>icoolkj</password>
	</server>
	<server>
	  <id>maven-releases</id>  <!--对应pom.xml的id=releases的仓库-->
	  <username>developer</username>
	  <password>icoolkj</password>
	</server>
	 <server>
	  <id>maven-snapshots</id> <!--对应pom.xml中id=snapshots的仓库-->
	  <username>developer</username>
	  <password>icoolkj</password>
	</server>	
  </servers>

mirrors节点下添加以下内容

 <!--仓库组的url地址,id和name可以写组仓库name,mirrorOf的值设置为central--> 
  <mirrors>	 
	 <mirror>
		<id>maven-public</id>
		<name>maven-public</name>
		<!--镜像采用配置好的组的地址-->
		<url>http://182.92.199.85:8081/repository/maven-public/</url>
		<mirrorOf>central</mirrorOf>
	</mirror> 
  </mirrors>

 (七)项目中发布

pom.xml配置

实际使用中distributionManagement可以配置在parent项目中,子项目无需重复配置。

上述配置全部完成后就可以在项目中使用mven clean deploy将项目的jar包上传到自己的私服上了。

 <repositories>
        <repository>
            <id>maven-public</id>
            <name>Nexus Repository</name>
            <url>http://192.168.1.188:8081/repository/maven-public/</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <releases>
                <enabled>true</enabled>
            </releases>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>maven-public</id>
            <name>Nexus Plugin Repository</name>
            <url>http://192.168.1.188:8081/repository/maven-public/</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <releases>
                <enabled>true</enabled>
            </releases>
        </pluginRepository>
    </pluginRepositories>
<!--项目分发信息,在执行mvn deploy后表示要发布的位置。有了这些信息就可以把网站部署到远程服务器或者把构件jar等部署到远程仓库。 -->
    <distributionManagement>
        <repository><!--部署项目产生的构件到远程仓库需要的信息 -->
            <id>maven-releases</id><!-- 此处id和settings.xml的id保持一致 -->
            <name>Nexus Release Repository</name>
            <url>http://192.168.1.188:8081/repository/maven-releases/</url>
        </repository>
        <snapshotRepository><!--构件的快照部署到哪里?如果没有配置该元素,默认部署到repository元素配置的仓库,参见distributionManagement/repository元素 -->
            <id>maven-snapshots</id><!-- 此处id和settings.xml的id保持一致 -->
            <name>Nexus Snapshot Repository</name>
            <url>http://192.168.1.188:8081/repository/maven-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>

609a1f00534ea9044b9e962e0751b70f.png

至此,nexus搭建完毕,支持本地部署依赖jar包。

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值