Docker搭建maven 私有库

maven私有仓库使用nexus3搭建,这个也支持docker的私有库。
先用docker 拉取 nexus3镜像

1
docker pull sonatype/nexus3

运行

1
docker run -d -p 8081:8081 --name nexus sonatype/nexus3

nexus3 的数据挂载目录为 /nexus-data ,如果想挂载在本机的话就加上 -v /some/dir/nexus-data:/nexus-data
访问 http://localhost:8081 可以看到nexus 主页面了.
查看admin密码
初始化后在nexus-data文件夹会有admin.password文件,存放初始密码, 进入 nexus 容器内查看

1
2
3
docker exec -it nexus /bin/bash 
cd nexus-data/
vi admin.password

第一次登录admin账号会要求修改密码。

创建maven仓库

image

选择 mavne2(hosted)
image

选择 maven-public仓库,将我们创建的私有库加入到 maven-public的Members
image
image

创建开发者账号

在户管理里面添加一个用于上传jar文件的开发者账户,当然你也可以是一所管理员用户,但是不建议这么做。点击Users选项,然后点击CreateUser按钮
image

这样我们的仓库就创建好了,上传账号也配置好了。

配置上传jar项目

在 maven 的setting.xml文件中配置账号密码
在settings.xml中找到servers节点,然后在里面添加一个server子节点,写入id/username/password信息。其中id随便命名,但是要记住,稍后我们会用到,username和password填写上一步中我们在Nexus后台中新添加的用户名和密码
image
image

打开我们要发布Jar到Nexus的项目的POM文件,添加distributionManagement节点,在其中添加repository子节点,然后填写id和url,其中id填写我们刚刚在settings.xml中配置的那个server-id,即btx-repo,然后url填写我们刚才在Nexus中添加的仓库的地址,我们可以通过在Nexus的仓库列表中,点击btx-repo仓库右侧的copy按钮来获取仓库地址,如下图
image

pom.xml配置如下
image

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<distributionManagement>
<repository>
<id>btx-repo</id>
<url>http://localhost:8081/repository/btx-repo/</url>
</repository>
</distributionManagement>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<!--这个是防止上传两个jar包的,不加的话引用下来每个类都变成两个文件-->
<configuration>
<arguments>-P!source-artifacts</arguments>
<useReleaseProfile>false</useReleaseProfile>
<goals>-Dmaven.test.skip=true deploy</goals>
</configuration>
</plugin>

</plugins>
</build>

用IDEA 选择maven -》deploy 就会打包上传到我们的私有仓库了
image

这时候我们去Nexus,依次点击上面导航栏的魔方图标 -> 左侧的Brose折叠菜单 -> conponents -> 我们创建的仓库 btx-repo
image
image

引用私有仓库中的依赖

首先我们打开要引用之前上传的依赖的工程,找到POM.xml打开,在project节点中添加repositories - repository节点,分别填写id/name/url信息,其中ID和name节点内容随便写,没有任何影响,url节点要填写我们创建的仓库url,如下配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<repositories>
......
<repository>
<id>btx-repo</id>
<name>btx-repo</name>
<url>http://localhost:8081/repository/btx-repo/</url>
</repository>
......
</repositories>

<dependencies>
......
<dependency>
<groupId>io.btx.common</groupId>
<artifactId>btx-dynamic-datasource</artifactId>
<version>1.0.0</version>
</dependency>
......
</dependencies>