Skip to content

nexus私有仓库maven搭建

docker-compose快速搭建

基于docker-compose快速搭建

docker-compose.yaml

yaml
name: "nexus"
services:
  nexus:
    image: registry.cn-beijing.aliyuncs.com/zbzly/nexus
    ports:
      - "8081-8084:8081-8084"
    volumes:
      - NEXUS_DATA:/nexus-data
volumes:
  NEXUS_DATA: { }

登录修改配置

获取默认admin账户密码

bash
docker compose exec nexus cat /nexus-data/admin.password

修改中央仓库为阿里云

maven-central

text
https://maven.aliyun.com/repository/public

创建自己的仓库

配置存储位置

配置 告警策略避免存储超了

选择maven

几种maven仓库的类型

  1. proxy 代理只读
  2. hosted 本地上传jar
  3. group 组 会按照 组中的依次拉取包

版本策略

  1. Relase 发布版本
  2. Snapshot 快照版本
  3. Mixed 混合模式

这里我们选择混合模式

部署策略

  1. Allow redeploy 允许重新部署
  2. Disable redeploy 不允许重新部署
  3. ...

这里选择允许重新部署

存储位置

这里选择默认或者我们新建的存储位置

配置maven group

将刚刚创建的仓库加到maven group中

项目中使用

settings.xml

settings.xml

xml
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">
    <localRepository>D:\\Mavenrepository-new</localRepository>
    <pluginGroups>
    </pluginGroups>
    <proxies>
    </proxies>
    <servers>
        <server>
            <id>server_id</id>
            <username>用户名</username>
            <password>密码</password>
        </server>
    </servers>
    <mirrors>
        <mirror>
            <id>server_id</id>
            <mirrorOf>*</mirrorOf>
            <name>nexus-local</name>
            <url>http://localhost:8081/repository/maven-public/</url>
        </mirror>
    </mirrors>
    <profiles>
        <profile>
        </profile>
    </profiles>

</settings>

pom文件

pom.xml

xml
    <!--下载仓库-->
<repositories>
    <repository>
        <id>nexus</id>
        <url>http://localhost:8081/repository/maven-public/</url>
        <!--开启发布版下载-->
        <releases>
            <enabled>true</enabled>
        </releases>
        <!--开启快照下载-->
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>
<distributionManagement>
<!--发布版仓库这里是mixed模式仓库所以可以一样-->
<repository>
    <id>nexus</id>
    <url>http://localhost:8081/repository/zly/</url>
</repository>
<!--快照仓库-->
<snapshotRepository>
    <id>nexus</id>
    <url>http://localhost:8081/repository/zly/</url>
</snapshotRepository>
</distributionManagement>

发布!

bash
maven deploy

nginx 代理

nexus.conf

text
server{
    server_name nexus.local.com;
    client_max_body_size 0;
    listen 80;
    location / {
        proxy_pass http://127.0.0.1:8081;
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Real-PORT $remote_port;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}