基于【 Docker】五 || maven私服环境搭建

1.Maven  Nexus私服的原理

为了节省带宽和时间,在局域网内架设一个私有的仓库服务器,用其代理所有外部的远程仓库。当本地Maven项目需要下载构件时,先去私服请求,如果私服没有,则再去远程仓库请求,从远程仓库下载构件后,把构件缓存在私服上。这样,及时暂时没有Internet链接,由于私服已经缓存了大量构件,整个项目还是可以正常使用的。同时,也降低了中央仓库的负荷。

2.基于Docker搭建Maven私服

nexus3的镜像

docker pull sonatype/nexus3

将容器内部/var/nexus-data挂载到主机/data/nexus-data目录

docker run -d -p 8081:8081 --name nexus -v /data/nexus-data:/var/nexus-data --restart=always sonatype/nexus3

注意:maven私服启动较慢,大约1分钟时间。

关闭防火墙,访问http://ip:8081 ,默认登陆账号 admin admin123

 3.创建maven私服仓库

 点击repositories,点击Create repository

选择maven2(hosted),然后输入仓库名称(test-release)。在version policy中选择这个仓库的类型,这里选择release,在Deployment policy中选择Allow redeploy(这个很重要)

 

 4.创建私服账号

创建用户

设置用户名,密码,权限等信息

 5.本地上次jar包到maven私服

在 maven的settings.xml配置文件中添加私服账号,用户名和密码为maven中分配的私服用户

<servers>
    <server>
        <id>kevin</id>
        <username>kevin</username>
        <password>kevin</password>
      </server>    
 </servers>

创建一个maven工程,并且打包到maven私服,添加以下配置。

<!--注意限定版本一定为RELEASE,因为上传的对应仓库的存储类型为RELEASE -->
    <!--指定仓库地址 -->
    <distributionManagement>
        <repository>
            <!--此名称要和.m2/settings.xml中设置的ID一致 -->
            <id>kevin</id>
            <url>http://192.168.22.7:8081/repository/kevin-release/</url>
        </repository>
    </distributionManagement>

    <build>
        <plugins>
            <!--发布代码Jar插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.7</version>
            </plugin>
            <!--发布源码插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

执行命令:mvn deploy,打包上传到maven私服

6.测试依赖jar包

<dependencies>
        <dependency>
            <groupId>com.kevin</groupId>
            <artifactId>kevin-test</artifactId>
            <version>0.0.1-RELEASE</version>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>kevin</id>
            <url>http://192.168.22.7:8081/repository/kevin-release/</url>
        </repository>
    </repositories>
原文地址:https://www.cnblogs.com/kevin-ying/p/11198899.html