Linux 下使用 Nexus3.x 搭建 Maven 私服指南

Nexus 是 Sonatype 提供的仓库管理平台,能够支持 Docker、Maven、npm 等格式数据的存储和发布,使用 Nexus 可以快速搭建自己公司的 Maven 私服。

本篇 Nexus 选择 3.x 版本,Nexus3.x 相较 2.x 版本有很大的改变:

  1. 从底层重构,从而提高性能,增强扩展能力,并改善用户体验;
  2. 升级界面,增加更多的浏览,搜索和管理功能;
  3. 提供安装包,使部署更简单(安装完自动添加成服务,省去手动添加的麻烦);
  4. 增加 Docker、NuGet、npm、Bower 的支持;
  5. 提供新的管理接口,从而能自动管理任务。

这里 Linux 选择 CentOS 7.2,环境要求:jdk1.8(Nexus 3.x 只能运行在 JVM8 及以上)。

私服

1. Nexus3.x安装

$ wget -P /usr/local http://sonatype-download.global.ssl.fastly.net/nexus/3/nexus-3.6.0-02-unix.tar.gz
$ tar -zxvf /usr/local/nexus-3.6.0-02-unix.tar.gz -C /usr/local/nexus        # 包含nexus-3.6.0-02和sonatype-work
$ cd /usr/local/nexus

如果 Linux 硬件配置比较低的话,建议修改 JVM 的堆内存参数(nexus-3.6.0-02/bin/nexus.vmoptions),否则会出现运行崩溃的现象。

Nexus 的启停命令:

$ /usr/local/nexus/nexus/bin/nexus {start|stop|run|run-redirect|status|restart|force-reload}

启动比较慢,稍等会儿才能正常访问。

如果是 root 用户启动,可能会出现如下警告,不推荐使用 root 用户启动,这个警告不影响 Nexus 的正常访问和使用。

WARNING: ************************************************************
WARNING: Detected execution as "root" user.  This is NOT recommended!
WARNING: ************************************************************

到此安装完毕。下面访问服务器 http://127.0.0.1:8081,可以看到:

nexus1

2. Nexus3.x基本使用

2.1 创建自己的代码仓库

点击 Nexus“Log in”,输入默认用户名(admin)和默认密码(admin123)登录。可以点击上面的“设置”图标,在“设置”里可以添加用户、角色,对接 LDAP 等的设置,如下:

nexus2

这里我们创建用户 user:

nexus3

创建完成后退出 admin,登录 user。
我们点击 Repository 下 Repositories 创建仓库:

nexus4

然后这里填写相关信息,创建仓库 java:

nexus5

到此仓库就创建成功了。

nexus6

2.2 Maven发布私服

2.2.1 发布私服

首先需要编辑 .m2/settings.xml 配置文件,在 settings 节点下新增配置:

<servers>
    <server>
        <id>releases</id>
        <username>用户名</username>
        <password>密码</password>
    </server>
    <server>
        <id>snapshots</id>
        <username>用户名</username>
        <password>密码</password>
    </server>
</servers>

然后编辑 Maven Project 的 pom.xml 文件,在 project 节点下新增配置:

<distributionManagement>
    <repository>
        <id>releases</id>
        <url>仓库地址</url>
    </repository>
    <snapshotRepository>
        <id>snapshots</id>
        <url>仓库地址</url>
    </snapshotRepository>
</distributionManagement>
<build>
    <plugins>
        <!-- 打jar包插件 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.2.0</version>
            <configuration>
                <excludes>
                    <exclude>**/*.properties</exclude>
                </excludes>
            </configuration>
        </plugin>
        <!-- 打包源码插件 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>3.2.1</version>
            <configuration>
                <attach>true</attach>
            </configuration>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

仓库地址如下 copy:

nexus7

最后使用 Maven 命令打包发布到 Nexus 即可:

mvn clean source:jar package
mvn deploy -e

nexus8

2.2.2 引用

编辑 Maven Project 的 pom.xml 文件,在 project 节点下新增如下配置即可:

<!-- maven私服 -->
<repositories>
    <repository>
        <id>my-releases</id>
        <url>仓库地址</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>my-snapshots</id>
        <url>仓库地址</url>
        <releases>
            <enabled>false</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>

<dependencies>
    <!-- 添加maven私服的依赖 -->
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>utils</artifactId>
        <version>1.0</version>
    </dependency>
</dependencies>

2.3 Gradle发布私服

2.3.1.发布私服

编辑 Gradle Project 的 build.gradle 文件,新增如下配置:

uploadArchives {
    repositories {
        mavenDeployer {
            // 远程
            repository(url: '仓库地址') {
                authentication(userName: "用户名", password: "密码")
            }
            snapshotRepository(url: '仓库地址') {
                authentication(userName: "用户名", password: "密码")
            }
        }
    }
}

仓库地址如下copy:

nexus7

然后双击 uploadArchives 命令打包发布到 Nexus 即可:

nexus9

等待 module 的上传,当全部显示ok时上传成功。

nexus8

2.3.2 引用

编辑 Gradle Project 的 build.gradle 文件,在 repositories 配置下新增仓库地址:

repositories {
    maven { url '仓库地址' }
    maven { url 'https://maven.aliyun.com/repository/public/' }
    mavenCentral()        
}

然后在 module 的 build.gradle 中添加依赖即可:

compile 'com.example:utils:1.0'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值