docker nexus3搭建 maven本地库管理依赖包

方便管理jar包搭建maven本地管理库,使用docker-compose 运行

docker 镜像

version: "3"
services:
  nexus3:
     image: sonatype/nexus3
     container_name: nexus
     restart: always
     ports:
       - 8181:8081
     volumes:
         - ./nexus/data:/nexus-data

权限问题

docker nexus3部署 nexus3 docker本地仓库_docker nexus3部署

  • 运行
docker-compose up -d

打开我网页登录http://192.168.0.254:8181

首次登录密码在 /usr/local/sonatype-work/nexus3/admin.password

之后可重置密码

(1)默认仓库说明:

  • maven-centralmaven 中央库,默认从 https://repo1.maven.org/maven2/ 拉取 jar
  • maven-releases:私库发行版 jar,初次安装请将 Deployment policy 设置为 Allow redeploy
  • maven-snapshots:私库快照(调试版本)jar
  • maven-public:仓库分组,把上面三个仓库组合在一起对外提供服务,在本地 maven 基础配置 settings.xml 或项目 pom.xml 中使用

(2)仓库类型说明:

  • group:这是一个仓库聚合的概念,用户仓库地址选择 Group 的地址,即可访问 Group 中配置的,用于方便开发人员自己设定的仓库。maven-public 就是一个 Group 类型的仓库,内部设置了多个仓库,访问顺序取决于配置顺序,3.x 默认为 ReleasesSnapshotsCentral,当然你也可以自己设置。
  • hosted:私有仓库,内部项目的发布仓库,专门用来存储我们自己生成的 jar 文件
  • snapshots:本地项目的快照仓库
  • releases: 本地项目发布的正式版本
  • proxy:代理类型,从远程中央仓库中寻找数据的仓库(可以点击对应的仓库的 Configuration 页签下 Remote Storage 属性的值即被代理的远程仓库的路径),如可配置阿里云 maven 仓库
  • central:中央仓库
  • 增加一个代理仓库

docker nexus3部署 nexus3 docker本地仓库_xml_02

选择maven2(proxy)

docker nexus3部署 nexus3 docker本地仓库_maven_03

配置阿里云代理 http://maven.aliyun.com/nexus/content/groups/public/

docker nexus3部署 nexus3 docker本地仓库_jar_04

阿里云代理仓库创建完毕后,我们编辑 maven-public,将其添加到放入 group 中,并调整优先级,然后保存:

docker nexus3部署 nexus3 docker本地仓库_docker nexus3部署_05

修改本地maven setting.xml配置

<mirrors>
    <mirror>
        <!--该镜像的唯一标识符。id用来区分不同的mirror元素。 -->
        <id>maven-public</id>
        <!--镜像名称 -->
        <name>maven-public</name>
        <!--*指的是访问任何仓库都使用我们的私服-->
        <mirrorOf>*</mirrorOf>
        <!--该镜像的URL。构建系统会优先考虑使用该URL,而非使用默认的服务器URL。 -->
        <url>http://192.168.0.254:8181/repository/maven-public/</url>     
    </mirror>
</mirrors>

在pom中引用

<repositories>
        <repository>
            <id>maven-public</id>
            <name>maven-public</name>
            <url>http://192.168.0.254:8181/repository/maven-public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>

发布jar 到私服

修改本地maven setting.xml

<servers>
    <server>
        <id>releases</id>
        <username>admin</username>
        <password>123</password>
    </server>
    <server>
        <id>snapshots</id>
        <username>admin</username>
        <password>123</password>
    </server>
</servers>

在pom.xml中加入

注意repository 里的 id 需要和上一步里的 server id 名称保持一致。

<!--    Maven 配置使用私服(发布依赖)-->
    <distributionManagement>
        <repository>
            <id>releases</id>
            <name>Releases</name>
            <url>http://192.168.0.254:8181/repository/maven-releases/</url>
        </repository>
        <snapshotRepository>
            <id>snapshots</id>
            <name>Snapshot</name>
            <url>http://192.168.0.254:8181/repository/maven-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>
    <!--    Maven 配置使用私服(发布依赖)-->

执行 mvn deploy 命令发布

登录控制台查看

代理插件

pom.xml中加入

<!--    Maven 配置使用私服(下载插件)-->
    <pluginRepositories>
        <pluginRepository>
            <id>maven-nexus</id>
            <name>maven-nexus</name>
            <url>http://192.168.0.254:8181/nexus/repository/maven-public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>
    <!--    Maven 配置使用私服(下载插件)-->

在springboot 中配置 build

<build>
        <resources>
            <resource>
                <directory>jar</directory>
                <targetPath>/BOOT-INF/jar/</targetPath>
                <includes>
                    <include>**/*.jar</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.4.5</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

            <!-- 生成javadoc文档包的插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.10.4</version>
                <configuration>
                    <aggregate>true</aggregate>
                </configuration>
                <executions>
                    <execution>
                        <id>attach-javadocs</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- 生成sources源码包的插件 -->
            <plugin>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.2.1</version>
                <configuration>
                    <attach>true</attach>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>