1. 安装maven

前提是先安装Java,可以参考其他文章或者同系列安装Java章节

# 创建存放目录
mkdir -p /opt/maven & cd /opt/maven
# 下载
wget https://mirror.bit.edu.cn/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
# 解压
tar zxvf apache-maven-3.6.3-bin.tar.gz
# 配置环境变量
vim /etc/profile
# 将以下环境变量放入到最后一行的后面
export M2_HOME=/opt/maven/apache-maven-3.6.3
export PATH=$PATH:$M2_HOME/bin
# 刷新环境变量
source /etc/profile
# 测试安装结果
mvn -v

maven server 节点 maven服务器搭建_xml

2. 配置阿里云仓库

# 进入到配置文件目录
cd /opt/maven/apache-maven-3.6.3/conf
# 打开配置文件
vim settings.xml

<mirrors>标签下加入仓库配置的信息

<mirror>
      <id>alimaven</id>
      <name>aliyun maven</name>
      <url>https://maven.aliyun.com/repository/public</url>
      <mirrorOf>central</mirrorOf>
    </mirror>

3. 搭建Nenus私服

3.1 下载安装
# 创建存放目录
mkdir -p /opt/maven/nexus
# 下载链接前面已经放出来了,在服务器下载特别慢,那就先下载到本地,然后scp到这个目录,这里的前提是已经做好了这个操作
cd /opt/maven/nexus
# 解压
tar -zxvf nexus-3.28.1-01-unix.tar.gz
3.2 目录结构
  • nexus-3.28.1-01 应用目录
  • /etc/nexus-default.properties 监听地址和端口
  • /bin/nexus.vmoptions 运行内存和工作目录
  • sonatype-work 工作目录,存放镜像仓库

/bin/nexus.vmoptions的默认内容如下,针对个人机器进行调整

-Xms2703m
-Xmx2703m
-XX:MaxDirectMemorySize=2703m

调整后, 虽然Java应用建议Xms和Xmx的值调成一样,但这个我们不确定占用多少的非应用,就保守点, 生产环境不建议不一致,这个是本地环境,为了节省内存

-Xms128m
-Xmx512m
-XX:MaxDirectMemorySize=512m
3.3 创建nexus用户并启动服务
# 创建nexus目录
useradd nexus
# 将nexus目录权限关联给上面创建的用户
chown -R nexus:nexus /opt/maven/nexus/
# 切换用户
su nexus
# 切换到bin目录下
cd opt/maven/nexus/nexus-3.28.1-01/bin
# 可以先尝试性的启动,这个能看到实时日志,但不是后台启动,发现没问题后再实时启动即可
./nexus run
# 后台启动nexus
./nexus start
3.4 访问配置

默认端口号是8081,上面配置文件也可以进行修改, 访问地址为ip:8081

点击sign in 按照提示获取账号密码, 然后登陆进去之后一路跟着提示去修改密码

  • 点击设置图标–> 左侧Roles–> Create role --> Nexus role
  • 选择权限 nx-all
  • 和Create Role一样的套路, Create User, 并把刚才创建的Role给当前User
  • maven server 节点 maven服务器搭建_maven私服_02


  • maven server 节点 maven服务器搭建_apache_03

3.5 设置Repository代理

选择左侧Repository->Repositories菜单

maven server 节点 maven服务器搭建_maven_04

创建代理仓库

maven server 节点 maven服务器搭建_apache_05

输入阿里云仓库地址: https://maven.aliyun.com/repository/public

maven server 节点 maven服务器搭建_maven私服_06

将阿里云的仓库代理加入到默认中央仓库组中去

点击maven-public 这个group

maven server 节点 maven服务器搭建_xml_07

将刚创建的代理加入到这个group中

maven server 节点 maven服务器搭建_xml_08

3.6 创建私库release和snapshot

Create repository, 选择类型maven2(hosted)

maven server 节点 maven服务器搭建_maven_09

version policy 选择Release, Development policy选择Allow redeploy

maven server 节点 maven服务器搭建_maven server 节点_10

Create repository, 选择类型maven2(hosted)

maven server 节点 maven服务器搭建_maven私服_11


version policy 选择Snapshot, Development policy选择Allow redeploy

maven server 节点 maven服务器搭建_apache_12

创建一个group, 把上面创建的两个仓库地址包含起来,同时把之前我们设置好的aliyun代理和自带的maven_public都加入进来汇总成一个group,这样后面我们引用的时候只要针对这个group就好

maven server 节点 maven服务器搭建_apache_13

将自己创建的host的仓库地址放在前面,优先从宿主机查找jar包,然后再到远程代理仓库中查找

maven server 节点 maven服务器搭建_maven_14

最终创建完成后的效果

maven server 节点 maven服务器搭建_maven server 节点_15

3.7 setting.xml配置

点击上面建立的nexus_public,然后看到里面的仓库地址

maven server 节点 maven服务器搭建_maven_16

settings.xml关键节点信息, 加入

<servers>
    <server>
      <!-- 在上面创建的用户名和密码 -->
      <id>nexus_public</id>
      <username>develop_user</username>
      <password>123456789</password>
    </server>
 
    <!-- 这里的id是随便取的,但是要和项目pom.xml中的<distributionManagement>节点下的<repository>下的<id>保持一致 -->
    <server>
      <id>nexus_release</id>
      <!-- 在上面步骤创建的用户名和密码 -->
      <username>develop_user</username>
      <password>123456789</password>
    </server>
    <server>
      <id>nexus_snapshot</id>
      <!-- 在上面步骤创建的用户名和密码 -->
      <username>develop_user</username>
      <password>123456789</password>
    </server>
  </servers>
 
    <mirrors>
        <!-- id 这是随便取的,而且这里是把私服的release和snapshot两个私库都添加到这个组里去了,统一对组进行配置 -->
        <mirror>
            <id>nexus_public</id>
            <name>nexus_public</name>
            <!-- 把上面点击进去的nexus_public里面的URL复制到这里, 注意这是一个maven group, 包含了nexus_release和nexus_snapshot两个仓库 -->
            <url>http://xxxxxxx:8081/repository/nexus_public/</url>
            <mirrorOf>*</mirrorOf>
        </mirror>
         <mirror>
          <id>alimaven</id>
          <name>aliyun maven</name>
          <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
          <mirrorOf>central</mirrorOf>        
         </mirror> 
         <mirror>  
          <id>repo2</id>  
          <mirrorOf>central</mirrorOf>  
          <name>Human Readable Name for this Mirror.</name>  
          <url>http://repo2.maven.org/maven2/</url>  
        </mirror>
    </mirrors>
 
<profiles>
    <profile>
        <id>java8</id>
        <activation>
            <activeByDefault>true</activeByDefault>
            <jdk>1.8</jdk>
        </activation>
        <properties>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
            <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
        </properties>
    </profile>
 
    <profile>
      <id>nexus_public</id>
       <!-- 远程仓库列表 -->
      <repositories>
        <repository>
          <id>nexus_public</id>
          <name>Nexus Central</name>
          <!-- 把上面点击进去的nexus_public里面的URL复制到这里, 注意这是一个maven group, 包含了nexus_release和nexus_snapshot两个仓库 -->
          <url>http://xxxxx:8081/repository/nexus_public/</url>
          <layout>default</layout>
         <!-- 表示可以从这个仓库下载releases版本的构件-->
          <releases>
            <enabled>true</enabled>
          </releases>
         <!-- 表示可以从这个仓库下载snapshot版本的构件 -->
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
        </repository>
      </repositories>
      <!-- 插件仓库列表 -->
      <pluginRepositories>
        <pluginRepository>
          <id>nexus_public</id>
          <name>Nexus Central</name>
          <url>http://xxxxxxx:8081/repository/nexus_public/</url>
          <layout>default</layout>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
          <releases>
             <enabled>true</enabled>
          </releases>
        </pluginRepository>
      </pluginRepositories>
   </profile>
</profiles>
<activeProfiles>
    <activeProfile>java8</activeProfile>
    <activeProfile>nexus_public</activeProfile>
</activeProfiles>

pom.xml内容如下

<distributionManagement>
        <repository>
            <!-- 注意这里的id要和settings.xml中配置servers节点下的server里的id一致,因为要找到对应的账号信息 -->
            <id>nexus_release</id>
            <name>Release Repository of nexus_release</name>
            <!-- 这个节点下的url对应的release的仓库地址 -->
            <url>http://xxxxx:8081/repository/nexus_release/</url>
        </repository>
        <snapshotRepository>
            <id>nexus_snapshot</id>
            <name>Snapshot Repository of nexus_snapshot</name>
            <!-- 这个节点下的url对应的snapshot的仓库地址 -->
            <url>http://xxxxx:8081/repository/nexus_snapshot/</url>
        </snapshotRepository>
    </distributionManagement>
3.8 执行mvn deploy命令

maven server 节点 maven服务器搭建_maven_17

maven server 节点 maven服务器搭建_xml_18