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上传下载依赖_第1张图片

3. 访问页面

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

docker搭建maven私服(nexus3),整合springboot上传下载依赖_第2张图片

4. 登录

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

docker搭建maven私服(nexus3),整合springboot上传下载依赖_第3张图片

五、nexus配置和解释

1. 继续初始化

docker搭建maven私服(nexus3),整合springboot上传下载依赖_第4张图片

2. 编写新密码

docker搭建maven私服(nexus3),整合springboot上传下载依赖_第5张图片

3. 配置匿名访问

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

docker搭建maven私服(nexus3),整合springboot上传下载依赖_第6张图片

4. 完成

docker搭建maven私服(nexus3),整合springboot上传下载依赖_第7张图片

5. 仓库名称类型

docker搭建maven私服(nexus3),整合springboot上传下载依赖_第8张图片

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

仓库类型 说明
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上传下载依赖_第9张图片

7. 新建用户和仓库

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

六、settings.xml配置

1. 配置maven上传信息

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

<servers>
  <server>
    <id>maven-publicid>
    <username>adminusername>
    <password>123456password>
  server>
  <server>
    <id>maven-snapshotsid>
    <username>adminusername>
    <password>123456password>
  server>
  <server>
    <id>maven-releasesid>
    <username>adminusername>
    <password>123456password>
  server>
servers>

2. 配置从私服下载

 <profiles>
    <profile>
        <id>nexus-ownid>
        
        <repositories>
            <repository>
                <id>maven-publicid>
                <name>Nexus Centralname>
                
                <url>http://192.168.239.132:8081/repository/maven-public/url>
                <layout>defaultlayout>
                
                <releases>
                    <enabled>trueenabled>
                releases>
                <snapshots>
                    <enabled>falseenabled>
                    <updatePolicy>alwaysupdatePolicy>
                snapshots>
            repository>
            <repository>
                <id>maven-snapshotsid>
                <name>Nexus Centralname>
                
                <url>http://192.168.239.132:8081/repository/maven-snapshots/url>
                <layout>defaultlayout>
                
                <releases>
                    <enabled>falseenabled>
                releases>
                <snapshots>
                    <enabled>trueenabled>
                    <updatePolicy>alwaysupdatePolicy>
                snapshots>
            repository>
        repositories>
        <pluginRepositories>
            <pluginRepository>
                <id>maven-publicid>
                <name>Nexus Centralname>
                <url>http://192.168.239.132:8081/repository/maven-public/url>
                <releases>
                    <enabled>trueenabled>
                releases>
                <snapshots>
                    <enabled>falseenabled>
                snapshots>
            pluginRepository>
        pluginRepositories>
    profile>
profiles>
<activeProfiles>
    
    <activeProfile>nexus-ownactiveProfile>
activeProfiles> 

七、springboot项目配置

1. 父项目pom文件

	<build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                    <configuration>
                        <layers>
                            <enabled>trueenabled>
                        layers>
                    configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackagegoal>
                            goals>
                        execution>
                    executions>
                plugin>
            plugins>
        pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>3.10.1version>
                <configuration>
                    <source>1.8source>
                    <target>1.8target>
                    <encoding>UTF-8encoding>
                    <parameters>trueparameters>
                configuration>
            plugin>
        plugins>
    build>
    
    <distributionManagement>
        <repository>
            
            <id>maven-releasesid>
            
            <url>http://192.168.239.132:8081/repository/maven-releases/url>
        repository>
        <snapshotRepository>
            <id>maven-snapshotsid>
            <url>http://192.168.239.132:8081/repository/maven-snapshots/url>
        snapshotRepository>
    distributionManagement>

2. deploy上传私服

双击deploy,上传私服!
docker搭建maven私服(nexus3),整合springboot上传下载依赖_第10张图片

3. 私服查看

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

docker搭建maven私服(nexus3),整合springboot上传下载依赖_第12张图片

4. 私服依赖在本地引用

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

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

docker搭建maven私服(nexus3),整合springboot上传下载依赖_第13张图片

5. 测试使用

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

八、总结

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

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

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


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

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

你可能感兴趣的:(开发高阶技能,docker,maven,spring,boot)