docker搭建maven私服

发布时间 2023-04-20 10:29:29作者: fhaain

安装 nx3 & 创建仓库

官方文档:https://help.sonatype.com/docs

 

 
docker run -d -p 8880:8081 --name nexus \
-e INSTALL4J_ADD_VM_PARAMS="-Xms2703m -Xmx2703m -XX:MaxDirectMemorySize=2703m" \
-e NEXUS_CONTEXT=nx \
sonatype/nexus3:3.37.3

# 更多参数请查看:https://hub.docker.com/r/sonatype/nexus3
  1. 访问 http://<your-ip>:8880/<NEXUS_CONTEXT>/
  2. 修改密码;禁用匿名访问
  3. 创建本地 blob 文件存储(类似命名空间?)
  4. 创建 mytools​ 仓库(maven2 格式 hostes 模式),指定存放位置为上一步创建的文件存储

settings.xml​ 配置账号密码

 

 
    <server>
      <id>mytools</id>
      <username>admin</username>
      <password>sSKBX8cxaFhZ@Zt</password>
    </server>

查看创建的仓库:

  1. 仓库地址
  2. 版本策略(默认为 RELEASE,版本号中不能包含 SNAPSHOT。可在创建仓库时修改为 mixed)

发布依赖(depoly)

The deploy is the last phase of the maven lifecycle. In this phase, the build is completed and the current project is being copied to the remote repository. This makes the built project available for other projects to add as dependency or developers.

配置 pom.xml​(添加 deploy 时需要的配置)

 

 
<!--指定仓库地址 -->
<distributionManagement>
  <repository>
    <!-- server.id -->
    <id>mytools</id>
    <!--上传的位置。此处容易出错! -->
    <url>http://localhost:8880/nx/repository/mytools/</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>

修改项目坐标(仓库默认策略是 RELEASE,所以 version 中不能有 snapshot)

 

 
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>im.eg</groupId>
  <artifactId>dms</artifactId>
  <version>1.0.0-test</version>

...

 

 
# 上传(经过构建、测试、打包等步骤后上传)
mvn deploy

在其他项目中使用 nexus 仓库中的依赖

  1. 指定 nexus 认证信息

  1. 配置 pom.xml。指定仓库地址、server.id、gav 坐标

 

 
<repositories>
    <repository>
        <id>mynx</id>
        <url>http://localhost:8880/nx/repository/mytools/</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>im.eg</groupId>
        <artifactId>dms</artifactId>
        <version>1.0.0-abc</version>
    </dependency>
</dependencies>

问题:上边 pom.xml 中指定的 nuxes 仓库地址可能要在别的地方二次指定,可否进行全局配置?

答:使用 profiles 标签进行全局配置(settings.xml 中)

 

 
<profiles>
    <profile>
        <id>mynxprofile</id>

        <!-- 依赖仓库 -->
        <repositories>
            <repository>
                <id>mynx</id>
                <url>http://localhost:8880/nx/repository/mytools/</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>
        </repositories>

        <!-- 插件仓库 -->
        <pluginRepositories>
            <pluginRepository>
                <id>aliyun-nexus</id>
                <url>http://maven.aliyun.com/nexus/content/groups/public</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </pluginRepository>
        </pluginRepositories>
    </profile>

...

配置完后可在 idea 的 maven 工具中指定使用的 profile

参考:https://blog.csdn.net/china_coding/article/details/128480154