Tools-nexus-maven

maven私库nexus3搭建使用

常用命令

1
2
#Maven 测试仓库命令,下载jar包,测试一般会报错,说没有权限
mvn dependency:get -DremoteRepositories=http://47.98.114.63:14006/repository/maven-third/ -DgroupId=com.taobao -DartifactId=taobao-sdk-java-auto -Dversion=20190804

sonatype/nexus3安装

  1. 创建挂载目录mkdir -p v-nexus/data并修改目录权限chown -R 200 v-nexus/data

  2. 创建部署脚本

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    # 默认用户名admin/admin123
    version: '3.2'

    services:
    nexus:
    restart: always
    image: sonatype/nexus3
    ports: #自定义端口
    - target: 8081
    published: 18081 #只有worker能访问该端口
    protocol: tcp
    mode: host #版本要求3.2
    volumes:
    - "/dockerdata/v-nexus/data:/nexus-data"
    deploy:
    replicas: 1
    restart_policy:
    condition: on-failure
    placement:
    constraints: [node.hostname == lfadmin]
  3. 测试访问http://192.168.1.213:18081/然后输入admin和admin123进行登陆即可

配置yum代理

远程原remote url: http://maven.aliyun.com/nexus/content/groups/public

新建一个type:yum(proxy)

在新建一个组yum(group),添加刚刚的代理地址,同理,可以添加elpe,docker等其他仓库代理

复制生成的地址http://192.168.1.230:18081/repository/yum-public/配置在`nexus.repo`

执行vim /etc/yum.repos.d/nexus.repo

1
2
3
4
5
6
[nexusrepo]
name=Nexus Repository
baseurl=http://192.168.1.230:18081/repository/yum-public/$releasever/os/$basearch/
enabled=1
gpgcheck=0
priority=1

yum clean all

rm -rf /etc/yum.repos.d/C*

注意

epel源需要单独配置,直接用public不识别

执行vim /etc/yum.repos.d/nexus-epel.repo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[nexus-epel-debuginfo]
name = Extra Packages for Enterprise Linux 7 - $basearch - Debug
baseurl = http://192.168.1.230:18081/repository/yum-epel/7/$basearch/debug
failovermethod = priority
enabled = 0
gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck = 0

[nexus-epel-source]
name = Extra Packages for Enterprise Linux 7 - $basearch - Source
baseurl = http://192.168.1.230:18081/repository/yum-epel/7/SRPMS
failovermethod = priority
enabled = 0
gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck = 0

[nexus-epel]
baseurl = http://192.168.1.230:18081/repository/yum-epel/7/$basearch
failovermethod = priority
gpgcheck = 0
name = EPEL YUM repo

win10下maven安装

  1. 下载apache-maven-3.5.4-bin.zip然后解压

  2. 添加环境变量,新建系统环境变量Maven_HOME值为解压路径,编辑path环境变量添加%Maven_HOME%\bin

  3. 命令窗口测试mvn -v,只支持cmd

  4. 修改apache-maven-3.5.4\conf\settings.xml文件

    1
    2
    <!--jar本地缓存地址-->
    <localRepository>D:\MavenRepository</localRepository>
  5. 完整的setting.xml设置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    <?xml version="1.0" encoding="UTF-8"?>

    <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

    <!-- jar本地缓存地址 -->
    <localRepository>D:\MavenRepository</localRepository>

    <pluginGroups>

    </pluginGroups>

    <proxies>

    </proxies>


    <servers>
    <!--配置权限,使用默认用户-->
    <server>
    <!--这里的id要和项目里的pom.xml的id一致-->
    <id>nexus-releases</id>
    <username>admin</username>
    <password>admin123</password>
    </server>
    <server>
    <id>nexus-snapshots</id>
    <username>admin</username>
    <password>admin123</password>
    </server>
    </servers>

    <mirrors>

    </mirrors>

    <profiles>
    <profile>
    <id>MyNexus</id>

    <activation>
    <jdk>1.4</jdk>
    </activation>

    <repositories>
    <!-- 私有库地址-->
    <repository>
    <id>nexus</id>
    <name>>Nexus3 Repository</name>
    <!-- 注意修改成对应的IP,在nexus里面复制public里面的地址 -->
    <url>http://192.168.1.213:18081/repository/maven-public/</url>

    <releases>
    <enabled>true</enabled>
    </releases>
    <!-- snapshots默认是关闭的,需要手动开启 -->
    <snapshots>
    <enabled>true</enabled>
    </snapshots>
    </repository>

    </repositories>
    <pluginRepositories>
    <!--插件库地址-->
    <pluginRepository>
    <id>nexus</id>
    <url>http://192.168.1.213:18081/repository/maven-public/</url>
    <releases>
    <enabled>true</enabled>
    </releases>
    <snapshots>
    <enabled>true</enabled>
    </snapshots>
    </pluginRepository>
    </pluginRepositories>
    </profile>

    </profiles>

    <!--激活profile-->
    <activeProfiles>
    <activeProfile>MyNexus</activeProfile>
    </activeProfiles>

    </settings>
  6. 在项目的pom.xml修改或添加如下配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    <?xml version="1.0" encoding="UTF-8"?>
    <project ...>
    ....
    <!-- 配置maven地址 -->
    <distributionManagement>
    <repository>
    <!--这里的id要和maven里的的settings.xml的id一致-->
    <id>nexus-releases</id>
    <name>Nexus Release Repository</name>
    <url>http://192.168.1.213:18081/repository/maven-releases/</url>
    </repository>
    <snapshotRepository>
    <id>nexus-snapshots</id>
    <name>Nexus Snapshot Repository</name>
    <url>http://192.168.1.213:18081/repository/maven-snapshots/</url>
    </snapshotRepository>
    </distributionManagement>
    ...
    </project>
  7. 编译在cmd执行mvn install发布上传jar执行mvn deploy,可以到nexus地址进行检查

  8. 使用私库下载和上传是一样的

nexus3 配置阿里云代理仓库

  1. 点击Create Repository->maven2(proxy)
  2. 添加名字aliyun-proxy设置阿里云url地址http://maven.aliyun.com/nexus/content/groups/public
  3. 设置阿里云优先级,在maven-public里面的group把刚刚创建的添加过去并移到maven-central上面
  4. 设置允许发布release,在maven-release的hosted里面选择allow redeploy

创建第三方仓库

  1. create repository->maven2(hosted)

    name:3rd_part

    hosted:Allow redeploy

  2. 添加srd_partmaven_public

  3. 如果没有groupId最好统一为com.3rdPart标注为第三方包

发布上传jar包到nexus

语法:

1
2
3
4
5
6
7
8
mvn deploy:deploy-file \ 
-DgroupId=<group-id> \
-DartifactId=<artifact-id> \
-Dversion=<version> \
-Dpackaging=<type-of-packaging> \
-Dfile=<path-to-file> \
-DrepositoryId=<这里的id要和maven里的的settings.xml的id一致> \
-Durl=<url-of-the-repository-to-deploy>

实战

1
2
3
4
5
6
7
8
9
mvn deploy:deploy-file \
-Dfile=spring-boot-starter-druid-0.0.1-SNAPSHOT.jar \
-DgroupId=cn.binux \
-DartifactId=spring-boot-starter-druid \
-Dversion=0.0.1-SNAPSHOT \
-Dpackaging=jar \
-DpomFile=spring-boot-starter-druid-0.0.1-SNAPSHOT.pom \
-DrepositoryId=nexus-snapshots \
-Durl=http://192.168.1.213:18081/repository/maven-snapshots/

上传jar包到私有maven仓库

1
2
3
4
5
6
7
8
9
10
11
12
mvn deploy:deploy-file -Dfile=spring-boot-starter-druid-0.0.1-SNAPSHOT.jar -DgroupId=cn.binux -DartifactId=spring-boot-starter-druid -Dversion=0.0.1-SNAPSHOT -Dpackaging=jar -DpomFile=spring-boot-starter-druid-0.0.1-SNAPSHOT.pom -DrepositoryId=nexus-snapshots -Durl=http://192.168.1.213:18081/repository/maven-snapshots/

mvn deploy:deploy-file -Dfile=spring-boot-starter-dubbox-0.0.1-SNAPSHOT.jar -DgroupId=cn.binux -DartifactId=spring-boot-starter-dubbox -Dversion=0.0.1-SNAPSHOT -Dpackaging=jar -DpomFile=spring-boot-starter-dubbox-0.0.1-SNAPSHOT.pom -DrepositoryId=nexus-snapshots -Durl=http://192.168.1.213:18081/repository/maven-snapshots/

mvn deploy:deploy-file -Dfile=spring-boot-starter-redis-0.0.1-SNAPSHOT.jar -DgroupId=cn.binux -DartifactId=spring-boot-starter-redis -Dversion=0.0.1-SNAPSHOT -Dpackaging=jar -DpomFile=spring-boot-starter-redis-0.0.1-SNAPSHOT.pom -DrepositoryId=nexus-snapshots -Durl=http://192.168.1.213:18081/repository/maven-snapshots/

#这个不是snapshots要发布到releases,注意设置nexus为允许发布,看jar报后缀,没有`SNAPSHOT`就是release
mvn deploy:deploy-file -Dfile=dubbo-2.8.4.jar -DgroupId=com.alibaba -DartifactId=dubbo -Dversion=2.8.4 -Dpackaging=jar -DrepositoryId=nexus-releases -Durl=http://192.168.1.213:18081/repository/maven-releases/

mvn deploy:deploy-file -Dfile=fastdfs-1.24.jar -DgroupId=org.csource -DartifactId=fastdfs -Dversion=1.24 -Dpackaging=jar -DrepositoryId=nexus-releases -Durl=http://192.168.1.213:18081/repository/maven-releases/

mvn deploy:deploy-file -Dfile=examples-1.0.jar -DgroupId=com.haikang -DartifactId=examples -Dversion=1.0 -Dpackaging=jar -DrepositoryId=nexus-releases -Durl=http://192.168.1.230:18081/repository/maven-releases/

本地安装jar包到本地maven仓库

1
2
3
4
5
mvn install:install-file -Dfile=spring-boot-starter-druid-0.0.1-SNAPSHOT.jar -DgroupId=cn.binux -DartifactId=spring-boot-starter-druid -Dversion=0.0.1-SNAPSHOT -Dpackaging=jar
mvn install:install-file -Dfile=spring-boot-starter-dubbox-0.0.1-SNAPSHOT.jar -DgroupId=cn.binux -DartifactId=spring-boot-starter-dubbox -Dversion=0.0.1-SNAPSHOT -Dpackaging=jar
mvn install:install-file -Dfile=spring-boot-starter-redis-0.0.1-SNAPSHOT.jar -DgroupId=cn.binux -DartifactId=spring-boot-starter-redis -Dversion=0.0.1-SNAPSHOT -Dpackaging=jar
mvn install:install-file -Dfile=dubbo-2.8.4.jar -DgroupId=com.alibaba -DartifactId=dubbo -Dversion=2.8.4 -Dpackaging=jar
mvn install:install-file -Dfile=fastdfs-1.24.jar -DgroupId=org.csource -DartifactId=fastdfs -Dversion=1.24 -Dpackaging=jar

配置用户和角色

  1. 创建roles:

    id: nx-deploy

    prlvlleges: nx-repository-view-*-*-*

  2. 创建用户:

    ID: develop

    roles: nx-deploy

maven 项目内局部配置私库地址

1
2
3
4
5
6
7
8
## pom.xml里面设置    
<repositories>
<repository>
<id>maven-third</id>
<name>maven-third</name>
<url>http://47.98.114.63:14006/repository/maven-third/</url>
</repository>
</repositories>

问题

  1. 下载了找不到包,解决,删除项目重新导入,重新maven依赖
  2. 刚上传或添加了新的jar到私库,无法下载,解决,删除本地仓库的该包目录
  3. 注意powershell执行命令时需要在等号后面加双引号,不然改用cmd