概述

maven仓库就是用来存储jar包的,在maven中,这个jar称之为构件,每个构件都有自己的坐标。

maven仓库根据坐标进行定位构件步骤:

  1. settings.xml配置文件中,找localRepository
  2. 根据坐标拼接路径groupId+artifiedId+version找到构件的目录
  3. 根据artifiedId+version+packaging找到文件

maven仓库是分级的,有本地仓库和远程仓库,远程仓库又包含:私服、公共仓库、中央仓库

阿里云仓库配置

仓库服务

仓库名称

阿里云仓库地址

阿里云仓库地址(老版)

源地址

central

https://maven.aliyun.com/repository/central

https://maven.aliyun.com/nexus/content/repositories/central

https://repo1.maven.org/maven2/

jcenter

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

https://maven.aliyun.com/nexus/content/repositories/jcenter

http://jcenter.bintray.com/

public

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

https://maven.aliyun.com/nexus/content/groups/public

central仓和jcenter仓的聚合仓

google

https://maven.aliyun.com/repository/google

https://maven.aliyun.com/nexus/content/repositories/google

https://maven.google.com/

gradle-plugin

https://maven.aliyun.com/repository/gradle-plugin

https://maven.aliyun.com/nexus/content/repositories/gradle-plugin

https://plugins.gradle.org/m2/

spring

https://maven.aliyun.com/repository/spring

https://maven.aliyun.com/nexus/content/repositories/spring

http://repo.spring.io/libs-milestone/

spring-plugin

https://maven.aliyun.com/repository/spring-plugin

https://maven.aliyun.com/nexus/content/repositories/spring-plugin

http://repo.spring.io/plugins-release/

grails-core

https://maven.aliyun.com/repository/grails-core

https://maven.aliyun.com/nexus/content/repositories/grails-core

https://repo.grails.org/grails/core

apache snapshots

https://maven.aliyun.com/repository/apache-snapshots

https://maven.aliyun.com/nexus/content/repositories/apache-snapshots

https://repository.apache.org/snapshots/

<repository>
  <id>aliyun</id>
  <name>aliyun</name>
  <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
  <layout>default</layout>
  <releases>
    <enabled>true</enabled>
  </releases>
  <snapshots>
    <enabled>false</enabled>
  </snapshots>
</repository>

在这里配置阿里云远程仓库的目的,仅为提高构件的下载速度,所以这种情况下,更推荐使用镜像。

远程仓库在哪些场景下适用呢?

  1. 企业内部各组开发的构件会上传至私服中,项目发生依赖时需要配置私服
  2. 一个开源项目,仅开源其中一部分代码。那部分未开源的代码,就可以放到企业的私有仓库中。当外部用户下载构件开源项目的时候,可指定远程仓库来下载不开源的构件

镜像配置

settings.xml

<mirror>
  <id>alimaven</id>
  <mirrorOf>central</mirrorOf>
  <name>aliyun maven</name>
  <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>

CentOS搭建Maven私服Nexus

nexus下载:Download Repository OSS

解压 nexus-3.37.3-02-unix.tar.gz

tar xvf nexus-3.37.3-02-unix.tar.gz -C ../

解压后看起来是这样的

.
├── bin
├── deploy
├── etc
├── lib
├── NOTICE.txt
├── OSS-LICENSE.txt
├── PRO-LICENSE.txt
├── public
├── replicator
└── system

修改可选配置

修改启动用户

切换目录至${NEXUS_HOME}/bin,执行 vim nexus.rc


run_as_user="root"


修改启动端口

切换目录至${NEXUS_HOME}/etc,执行 vim nexus-default.properties


application-port=18081


环境变量

执行vim /etc/profile

# nexus env
export NEXUS_HOME=/soft/nexus-3.37.3-02
export PATH=$PATH:$NEXUS_HOME/bin

编辑后,执行一下source /etc/profile

启动及验证

nexus {start|stop|run|run-redirect|status|restart|force-reload}

浏览器访问:ip:18081

账户密码存放路径:/sonatype-work/nexus3/admin.password,首次登录重新修改密码即可

Maven与Nexus整合

<repositories>
  <repository>
    <id>local-nexus</id>
    <name>local-nexus</name>
    <layout>default</layout>
    <url>http://nna:18081/repository/maven-public/</url>
  </repository>
</repositories>

idea pom 配置后,可能会不生效

检查一下Maven目录下的/apache-maven-3.8.1/conf/settings.xml

pom中配置的local-nexus,添加到settings.xml中,配置好后看起来像下面这样

<mirror>
  <id>maven-default-http-blocker</id>
  <mirrorOf>external:http:*,!local-nexus</mirrorOf>
  <name>Pseudo repository to mirror external repositories initially using HTTP.</name>
  <url>http://0.0.0.0/</url>
  <blocked>true</blocked>
</mirror>

本地库批量导入Nexus

准备一个本地库

kilim maven仓库 maven公司仓库_maven

点击仓库按钮

kilim maven仓库 maven公司仓库_kilim maven仓库_02

选择maven2(hosted)类型

kilim maven仓库 maven公司仓库_spring_03

将本地包上传至Nexus服务器

建立一个文件夹repo

mkdir repo

将包上传至repo目录,上传完看起来像这样

.
├── ai
├── ant
├── antlr
├── aopalliance
├── asm
├── aspectj
├── au
├── avalon-framework
├── backport-util-concurrent
├── biz
├── bouncycastle
├── c3p0
├── cglib
├── ch
├── classworlds
├── cn
├── co
├── com

写一个上传脚本,名字为mavenimport.sh

#!/bin/bash

# copy and run this script to the root of the repository directory containing files
# this script attempts to exclude uploading itself explicitly so the script name is important
# Get command line params
while getopts ":r:u:p:" opt; do
        case $opt in
                r) REPO_URL="$OPTARG"
                ;;
                u) USERNAME="$OPTARG"
                ;;
                p) PASSWORD="$OPTARG"
                ;;
        esac
done

find . -type f -not -path './mavenimport\.sh*' -not -path '*/\.*' -not -path '*/\^archetype\-catalog\.xml*' -not -path '*/\^maven\-metadata\-local*\.xml' -not -path '*/\^maven\-metadata\-deployment*\.xml' | sed "s|^\./||" | xargs -I '{}' curl -u "$USERNAME:$PASSWORD" -X PUT -v -T {} ${REPO_URL}/{} ;

赋一个执行权限:chmod a+x ./mavenimport.sh

参数注解:

-u

用户名

admin

-p

密码

123456

-r

仓库地址

http://nna:18081/repository/bihu-hosted/

完整命令:

sh mavenimport.sh -u admin -p 123456 -r http://nna:18081/repository/bihu-hosted/

耐心等待即可,在Nexus管理界面可以看到,看起来像这样

kilim maven仓库 maven公司仓库_maven_04

kilim maven仓库 maven公司仓库_java_05

最后将我们新建立的新仓库加入到maven-public组中即可

kilim maven仓库 maven公司仓库_spring_06

集成开发环境IDEA Deploy至Nexus

pom 文件添加相关deploy配置

<distributionManagement>
  <repository>
    <id>release</id>
    <name>Local Nexus Repository</name>
    <url>http://nna:18081/repository/bihu-hosted/</url>
  </repository>
  <snapshotRepository>
    <id>release</id>
    <name>Local Nexus Repository</name>
    <url>http://nna:18081/repository/bihu-hosted/</url>
  </snapshotRepository>
</distributionManagement>

settings.xml文件找<servers>节点,需要注意的是,pom 中的id要与server中的id一致,否则不生效,会出现401权限异常

<servers>
  <!-- server
   | Specifies the authentication information to use when connecting to a particular server, identified by
   | a unique name within the system (referred to by the 'id' attribute below).
   |
   | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
   |       used together.
   | -->
  <server>
    <id>release</id>
    <username>admin</username>
    <password>123456</password>
  </server>
  <!-- Another sample, using keys to authenticate.
  <server>
    <id>siteServer</id>
    <privateKey>/path/to/private/key</privateKey>
    <passphrase>optional; leave empty if not used.</passphrase>
  </server>
  -->
</servers>