maven 私服搭建

  1. 1. 安装
  2. 2. 从中央仓库下载索引到私服
  3. 3. 发布 jar 包

安装

  1. 下载 nexus 压缩包并解压

    1
    2
    3
    4
    5
    wget http://www.sonatype.org/downloads/nexus-latest-bundle.tar.gz
    tar zxvf nexus-2.14.4-03-bundle.tar.gz
    mv nexus-2.14.4-03 /usr/program/ //将程序目录移动到 /usr/program 下
    mv sonatype-work /opt/ //将仓库目录移动到 /opt 下
    mv /usr/program/nexus-2.14.4-03 /usr/program/nexus2.14.4 //文件夹重命名
  2. 修改配置

    • /ect/profile 增加如下内容,修改后记得刷新配置:source /ect/profile

      1
      2
      3
      4
      5
      # Nexus
      NEXUS_HOME=/usr/program/nexus2.14.4
      export NEXUS_HOME
      RUN_AS_USER=root
      export RUN_AS_USER
      • 修改 nexus2.14.4/conf/nexus.properties

        1
        nexus-work=/opt/sonatype-work/nexus
      • 默认端口为8081,可以在 nexus2.14.4/conf/nexus.properties 中修改

        1
        application-port=10000
      • 防火墙设置

        1
        2
        3
        sudo iptables -I INPUT -p tcp -m tcp --dport 8081 -j ACCEPT
        sudo /etc/rc.d/init.d/iptables save
        sudo service iptables restart
  3. 启动访问

    • 启动

      1
      2
      3
      cd /usr/program/nexus2.14.4/bin
      ./nexus start // 启动命令
      ./nexus stop // 停止命令
    • 访问

从中央仓库下载索引到私服

  1. 下载,第一次下载因为索引很大,过程会很漫长。。。

    从中央仓库下载索引到私服

  2. 等到 schedule task 中的任务消失后说明已经下载完毕了,并可以查看索引,如下图

    浏览索引

发布 jar 包

  1. 修改 maven settings.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
    87
    88
    89
    90
    91
    92
    <?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">
    <!--设置 Nexus 认证信息-->
    <servers>
    <server>
    <id>nexus-releases</id>
    <username>admin</username>
    <password>admin123</password>
    </server>
    <server>
    <id>nexus-snapshots</id>
    <username>admin</username>
    <password>admin123</password>
    </server>
    </servers>

    <!--设置 Nexus 镜像,后面只要本地没对应的以来,则到 Nexus 去找-->
    <mirrors>
    <mirror>
    <id>nexus-releases</id>
    <mirrorOf>*</mirrorOf>
    <url>http://ip:8081/repository/maven-releases/</url>
    </mirror>
    <mirror>
    <id>nexus-snapshots</id>
    <mirrorOf>*</mirrorOf>
    <url>http://ip:8081/repository/maven-snapshots/</url>
    </mirror>
    <mirror>
    <id>aliyun-maven</id>
    <name>aliyun maven</name>
    <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
    <mirrorOf>central</mirrorOf>
    </mirror>
    </mirrors>

    <profiles>
    <profile>
    <id>nexus</id>
    <repositories>
    <repository>
    <id>nexus-releases</id>
    <url>http://nexus-releases</url>
    <releases>
    <enabled>true</enabled>
    </releases>
    <snapshots>
    <enabled>true</enabled>
    </snapshots>
    </repository>
    <repository>
    <id>nexus-snapshots</id>
    <url>http://nexus-snapshots</url>
    <releases>
    <enabled>true</enabled>
    </releases>
    <snapshots>
    <enabled>true</enabled>
    </snapshots>
    </repository>
    </repositories>
    <pluginRepositories>
    <pluginRepository>
    <id>nexus-releases</id>
    <url>http://nexus-releases</url>
    <releases>
    <enabled>true</enabled>
    </releases>
    <snapshots>
    <enabled>true</enabled>
    </snapshots>
    </pluginRepository>
    <pluginRepository>
    <id>nexus-snapshots</id>
    <url>http://nexus-snapshots</url>
    <releases>
    <enabled>true</enabled>
    </releases>
    <snapshots>
    <enabled>true</enabled>
    </snapshots>
    </pluginRepository>
    </pluginRepositories>
    </profile>
    </profiles>

    <activeProfiles>
    <activeProfile>nexus</activeProfile>
    </activeProfiles>
    </settings>
  2. pom 文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <distributionManagement>
    <repository>
    <id>nexus-releases</id>
    <url>http://ip:8081/repository/maven-releases/</url>
    </repository>
    <snapshotRepository>
    <id>nexus-snapshots</id>
    <url>http://ip:8081/repository/maven-snapshots/</url>
    </snapshotRepository>
    </distributionManagement>
  3. mvn deploy