blank
blank
发布于 2020-07-01 / 817 阅读 / 0 评论 / 0 点赞

搭建 Maven 私服

搭建 maven 服务器

创建 nexus 数据存放目录并赋权

因为 sonatype/nexus3 镜像使用 200 运行 所以将文件夹赋予 200

mkdir -p /docker/nexus/data && chown -R 200 /docker/nexus/data

创建 docker-compose.yml 文件

vim /docker/nexus/docker-compose.yml

version: '3.7'
services:
  nexus:
    image: sonatype/nexus3
    # user: root
    # privileged: true
    container_name: nexus
    restart: always
    ports:
      - "9081:8081"
    environment:
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    deploy:
      resources:
        limits:
          cpus: '0.9'
          memory: 500M
        reservations:
          cpus: '0.5'
          memory: 500M
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - /docker/nexus/data:/nexus-data
    healthcheck:
      test: curl https://localhost:8081 >/dev/null; if [[ $$? == 52 ]]; then echo 0; else echo 1; fi
      interval: 30s
      timeout: 10s
      retries: 5    
    networks:
      -  nexus

networks:
   nexus:

启动 nexus 服务

docker-compose up -d

启动完毕后 会在 /docker/nexus/data/admin.password 生成 admin 临时登陆密码

# 获取临时登陆密码
cat /docker/nexus/data/admin.password

使用 admin 此密码登陆
IP:9081

登陆后会强制要求更改 admin 密码

上传 jar包

使用 idea 上传 自己开发的项目( pom 打包 jar 格式) 到私服

使用命令行上传 jar 包 到私服

mvn deploy:deploy-file -DgroupId=com.blankhang -DartifactId=test -Dversion=1.0.0-SNAPSHOT -Dpackaging=jar -Dfile=D:\jar\test-1.0.0-SNAPSHOT.jar -Durl=http://IP:9081/repository/maven-snapshots/ -DrepositoryId=maven-snapshots

配置私服到项目中

    <!--依赖仓库配置 (从私服下载已上传的私有jar包需要)-->
    <repositories>
        <!-- 自有私服 -->
        <repository>
            <id>maven-releases</id>
            <name>Nexus Release Repository</name>
            <url>http://IP:9081/repository/maven-releases/</url>
        </repository>
        <!-- 自有私服 -->
        <repository>
            <id>maven-snapshots</id>
            <name>Nexus Snapshot Repository</name>
            <url>http://IP:9081/repository/maven-snapshots/</url>
        </repository>
    <repositories>


    <!--私服仓库 (发布到私服需要)-->
    <distributionManagement>
        <repository>
            <id>maven-releases</id>
            <name>Nexus Release Repository</name>
            <url>http://IP:9081/repository/maven-releases/</url>
        </repository>
        <snapshotRepository>
            <id>maven-snapshots</id>
            <name>Nexus Snapshot Repository</name>
            <url>http://IP:9081/repository/maven-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>

添加发布插件

    
    <build>
        <plugins>
            <!--添加发布jar包插件-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.8</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>

配置maven settings文件的服务器用户名密码

一般在 用户目录下 (win)C:\Users{USERNAME}.m2\setting.xml

# 注意:id必须和私服中releases和snapshots仓库名一致
<servers>
    <server>  
        <id>maven-releases</id>  
        <username>admin</username>  
        <password>admin123</password>  
      </server>  
      <server>  
        <id>maven-snapshots</id>  
        <username>admin</username>  
        <password>admin123</password>  
      </server> 
  </servers>

重新打开项目,对需要的模块进行 deploy 即可自动上传成私服

参阅 https://hub.docker.com/r/sonatype/nexus3

DONE


评论