docker搭建maven私服(nexus3),整合springboot上传下载依赖

一、前言

我们在JavaWeb开发中必不可少的就是jar包管理-maven,在没有maven之前,都是自己手动下载jar包导入到项目中,非常的繁琐。

maven出现之后,又迎来新的问题,对于仓库里人家发布的都可以引用下载,但是公司自己内部写的jar包,不想让外人看到,自己公司来回粘贴复制,非常的繁琐,版本维护起来也是十分头疼!

这时Nexus Repository出现了,现在主流的还是nexus3,所以今天小编带大家一起搭建使用一下。当然公司也必须有一个自己的私服,来存放公司的技术沉淀,提高开发效率!

网上教程看了很多,但是基本都是教怎么上传到私服,从私服拉取依赖就没有写!

本文从上传到拉去面面俱到,对你有帮助,一键三连哈!!

二、介绍

官网地址

nexus3是一种特殊的远程仓库,一般部署在公司服务器或者局域网内的仓库服务,私服代理广域网上的远程仓库,供公司的Maven用户使用。

当Maven依赖需要依赖的时候,它从私服请求,如果私服上不存在依赖,则从外部的远程仓库下载,缓存在私服上之后,再为Maven的下载请求提供服务。

我们还可以把一些无法从外部仓库下载到的构件上传到私服上。

总的准则:无论私服有没有,我们本地都是使用私服提供的!

三、优点

  • 引入速度提高
  • 加速Maven构建
  • 方便管理
  • 稳定性高
  • 降低中央仓库的负荷
  • 隐私性高

四、docker构建运行

1. 创建挂载目录

mkdir /mydata/nexus/nexus-data -p 

给权限:

chmod 777 /mydata/nexus/nexus-data/ 

2. 启动nexus

docker run -d -p 8081:8081 --name nexus -v /mydata/nexus/nexus-data:/nexus-data sonatype/nexus3 

3. 获取密码

我们看到密码可以在容器内获取:

默认用户是,唯一生成的密码可以在卷内的文件中找到。有关卷的信息,请参阅持久数据。
admin admin.password

由于我们挂载了数据文件,主要在宿主机上查看即可!

切换到目录:

cd /mydata/nexus/nexus-data/ 

查看密码:

cat admin.password 

密码:206d5b6b-cc58-403f-af03-e5c8772a803a

docker搭建maven私服(nexus3),整合springboot上传下载依赖

3. 访问页面

ip+端口访问:http://192.168.239.132:8081/

docker搭建maven私服(nexus3),整合springboot上传下载依赖

4. 登录

用户:admin
密码:挂载目录下的admin.password文件内

docker搭建maven私服(nexus3),整合springboot上传下载依赖

五、nexus配置和解释

1. 继续初始化

docker搭建maven私服(nexus3),整合springboot上传下载依赖

2. 编写新密码

docker搭建maven私服(nexus3),整合springboot上传下载依赖

3. 配置匿名访问

由于匿名访问有安全性问题,在公司不是还是禁用为好!

docker搭建maven私服(nexus3),整合springboot上传下载依赖

4. 完成

docker搭建maven私服(nexus3),整合springboot上传下载依赖

5. 仓库名称类型

docker搭建maven私服(nexus3),整合springboot上传下载依赖

我们可以看到有三种类型:

仓库类型 说明
proxy 代理到远程仓库,默认国外,可以修改为国内阿里云代理
group 存放:通过 Nexus 获取的第三方 jar 包
hosted 存放:本团队其他开发人员部署到 Nexus 的 jar 包

还有一些仓库名称:

仓库名称 说明
maven-central Nexus 对 Maven 中央仓库的代理
maven-public Nexus 默认创建,供开发人员下载使用的组仓库
maven-releasse Nexus 默认创建,供开发人员部署自己 jar 包的宿主仓库要求 releasse 版本(生产版本)
maven-snapshots Nexus 默认创建,供开发人员部署自己 jar 包的宿主仓库要求 snapshots 版本(测试版本)

6. 调整代理地址

点击maven-central进入详情:

把代理地址换为阿里云的:
https://maven.aliyun.com/repository/public

往下滑到最后点击保存!

docker搭建maven私服(nexus3),整合springboot上传下载依赖

7. 新建用户和仓库

关于很多教程都是新建用户和仓库,小编这里就使用admin和自带的仓库了!
一般的小公司够了,公司有一定规模在新建吧!!

六、settings.xml配置

1. 配置maven上传信息

我们打开本地的settings.xml文件,把私服的地址和用户配置上去!

<servers>   <server>     <id>maven-public</id>     <username>admin</username>     <password>123456</password>   </server>   <server>     <id>maven-snapshots</id>     <username>admin</username>     <password>123456</password>   </server>   <server>     <id>maven-releases</id>     <username>admin</username>     <password>123456</password>   </server> </servers> 

2. 配置从私服下载

 <profiles>     <profile>         <id>nexus-own</id>         <!-- 远程仓库列表 -->         <repositories>             <repository>                 <id>maven-public</id>                 <name>Nexus Central</name>                 <!-- 虚拟的URL形式,指向镜像的URL-->                 <url>http://192.168.239.132:8081/repository/maven-public/</url>                 <layout>default</layout>                 <!-- 表示可以从这个仓库下载releases版本的构件-->                 <releases>                     <enabled>true</enabled>                 </releases>                 <snapshots>                     <enabled>false</enabled>                     <updatePolicy>always</updatePolicy>                 </snapshots>             </repository>             <repository>                 <id>maven-snapshots</id>                 <name>Nexus Central</name>                 <!-- 虚拟的URL形式,指向镜像的URL-->                 <url>http://192.168.239.132:8081/repository/maven-snapshots/</url>                 <layout>default</layout>                 <!-- 表示可以从这个仓库下载snapshot版本的构件 -->                 <releases>                     <enabled>false</enabled>                 </releases>                 <snapshots>                     <enabled>true</enabled>                     <updatePolicy>always</updatePolicy>                 </snapshots>             </repository>         </repositories>         <pluginRepositories>             <pluginRepository>                 <id>maven-public</id>                 <name>Nexus Central</name>                 <url>http://192.168.239.132:8081/repository/maven-public/</url>                 <releases>                     <enabled>true</enabled>                 </releases>                 <snapshots>                     <enabled>false</enabled>                 </snapshots>             </pluginRepository>         </pluginRepositories>     </profile> </profiles> <activeProfiles>     <!--需要激活 <profile>中的ID才生效-->     <activeProfile>nexus-own</activeProfile> </activeProfiles>  

七、springboot项目配置

1. 父项目pom文件

	<build>         <pluginManagement>             <plugins>                 <plugin>                     <groupId>org.springframework.boot</groupId>                     <artifactId>spring-boot-maven-plugin</artifactId>                     <configuration>                         <layers>                             <enabled>true</enabled>                         </layers>                     </configuration>                     <executions>                         <execution>                             <goals>                                 <goal>repackage</goal>                             </goals>                         </execution>                     </executions>                 </plugin>             </plugins>         </pluginManagement>         <plugins>             <plugin>                 <groupId>org.apache.maven.plugins</groupId>                 <artifactId>maven-compiler-plugin</artifactId>                 <version>3.10.1</version>                 <configuration>                     <source>1.8</source>                     <target>1.8</target>                     <encoding>UTF-8</encoding>                     <parameters>true</parameters>                 </configuration>             </plugin>         </plugins>     </build>     <!--打包上传maven私服-->     <distributionManagement>         <repository>             <!--id的名字可以任意取,但是在setting文件中的属性<server>的ID与这里一致-->             <id>maven-releases</id>             <!--指向仓库类型为host(宿主仓库)的储存类型为Release的仓库-->             <url>http://192.168.239.132:8081/repository/maven-releases/</url>         </repository>         <snapshotRepository>             <id>maven-snapshots</id>             <url>http://192.168.239.132:8081/repository/maven-snapshots/</url>         </snapshotRepository>     </distributionManagement> 

2. deploy上传私服

双击deploy,上传私服!
docker搭建maven私服(nexus3),整合springboot上传下载依赖

3. 私服查看

docker搭建maven私服(nexus3),整合springboot上传下载依赖
一共上传了两次,都在私服中存在!

docker搭建maven私服(nexus3),整合springboot上传下载依赖

4. 私服依赖在本地引用

我们新建一个springboot项目,然后引入依赖:

要勾选Projects下面的配置,不然无法引入依赖!

docker搭建maven私服(nexus3),整合springboot上传下载依赖

5. 测试使用

新项目可以使用依赖中的注解:
docker搭建maven私服(nexus3),整合springboot上传下载依赖

八、总结

经过半天的测试加编写,终于完成,对于私服有了更深的认识!

私服是每个公司必须要有的,当然也是我们必须要掌握的,小编整理出来,方便大家学习!

优点前面都说了,这里就不多说了!


前人种树后人乘凉嘛,对你有帮助,还请不要吝啬你的发财小手点点关注哈!

关注小编的微信公众号,一起交流学习!文章首发看哦!

docker搭建maven私服(nexus3),整合springboot上传下载依赖

发表评论

评论已关闭。

相关文章