Nexus Maven 私服

基于Nexus搭建私服

Posted on 2018-11-15,5 min read

环境安装

  1. 安装jdk

  2. 安装maven

  3. 安装nexus

    nexus工具是此次maven私服的构建工具

    下载nexus

    官网地址:https://www.sonatype.com/download-oss-sonatype

    上传到服务器

    可以使用FTP工具上传或者使用scp命令上传

    解压安装包到指定位置,修改配置

    将启动内存和最大内存都设置为1G(此处配置根据服务器配置而定)

    -Xms1024m
    -Xmx1024m
    -XX:MaxDirectMemorySize=2073m
    -XX:+UnlockDiagnosticVMOptions
    -XX:+LogVMOutput
    -XX:LogFile=../sonatype-work/nexus3/log/jvm.log
    -XX:-OmitStackTraceInFastThrow
    -Djava.net.preferIPv4Stack=true
    

    启动nexus

    进入bin目录 启动nexus 出现如下效果即启动成功

    ./nexus start
    WARNING: ************************************************************
    WARNING: Detected execution as "root" user.  This is NOT recommended!
    WARNING: ************************************************************
    Starting nexus
    

    启动成功后即可访问: http://ip:8081/

    说明

    ​ Nexus默认的端口是8081,可以在sonatype-work/nexus3/etc/nexus.properties中修改

    ​ 右上角那个登录会有一个默认的admin账号,初始密码保存在/usr/software/nexus/sonatype-work/nexus3/admin.password文件中,登录进去会让你重置密码。

    ​ 出现访问拒绝的情况,有可能是防火墙端口未开放 参考文章:https://blog.csdn.net/beekimlin/article/details/104551278/

默认仓库

安装好了Nexus后,会内置几个maven的默认仓库。可自定义仓库。

maven-central

proxy类型。maven中央库,默认从https://repo1.maven.org/maven2/拉取jar。

将私服的中央仓库的地址改为aliyun的中央仓库地址http://maven.aliyun.com/nexus/content/groups/public

maven-releases

hosted类型。releases发行版版本仓库。

maven-snapshots

hosted类型。snapshots快照版版本仓库。

maven-public

group类型。默认把上面3个仓库组合在一起。

注意:Nexus安装好以后需要更新远程仓库项目构建的索引文件。进入仓库就可以看到相关的按钮。

项目配置

Maven配置文件更改,修改<Maven安装目录>/conf/settings.xml文件

<server>
  <id>nexus-releases</id>
  <username>admin</username>
  <password>admin123</password>
</server>
<server>
  <id>nexus-snapshots</id>
  <username>admin</username>
  <password>admin123</password>
</server>

用户名和密码是搭建私服时创建的用户的密码,或者也可以使用管理员的用户名和密码

配置私服镜像

<mirror>
  <id>maven-public</id>
  <mirrorOf>*</mirrorOf>
  <url>http://ip:8081/repository/maven-public/</url>
</mirror>

Maven项目发布jar到Nexus私服

pom.xml配置

<profiles>
    <profile>
        <id>nexus</id>
        <repositories>
            <repository>
                <!--覆盖掉中央仓库-->
                <id>central</id>
                <name>nexus central</name>
                <url>http://xxx/repository/maven-public/</url>
                <layout>default</layout>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <!--快照版本,不从中央仓库拿-->
                    <enabled>false</enabled>
                </snapshots>
            </repository>
            <!--另外的仓库,会按照顺序进行查找-->
            <repository>
                <id>releases</id>
                <url>http://xxx/repository/maven-releases/</url>
            </repository>
            <repository>
                <id>snapshots</id>
                <url>http://xxx/repository/maven-snapshots/</url>
            </repository>
        </repositories>
        <!--配置插件下载的仓库-->
        <pluginRepositories>
            <pluginRepository>
                <id>central</id>
                <url>http://maven.aliyun.com/nexus/content/groups/public</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </pluginRepository>
            <pluginRepository>
                <id>snapshots</id>
                <url>http://maven.aliyun.com/nexus/content/groups/public</url>
                <releases>
                    <enabled>false</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </pluginRepository>
        </pluginRepositories>
    </profile>
</profiles>
<activeProfiles>
    <activeProfile>nexus</activeProfile>
</activeProfiles>

使用mvn deploy命令即可发布jar到Nexus 私服