Nexus 是 Sonatype 提供的仓库管理平台,能够支持 Docker、Maven、npm 等格式数据的存储和发布,使用 Nexus 可以快速搭建自己公司的 Maven 私服。
本篇 Nexus 选择 3.x 版本,Nexus3.x 相较 2.x 版本有很大的改变:
- 从底层重构,从而提高性能,增强扩展能力,并改善用户体验;
- 升级界面,增加更多的浏览,搜索和管理功能;
- 提供安装包,使部署更简单(安装完自动添加成服务,省去手动添加的麻烦);
- 增加 Docker、NuGet、npm、Bower 的支持;
- 提供新的管理接口,从而能自动管理任务。
这里 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,可以看到:
2. Nexus3.x基本使用
2.1 创建自己的代码仓库
点击 Nexus“Log in”,输入默认用户名(admin)和默认密码(admin123)登录。可以点击上面的“设置”图标,在“设置”里可以添加用户、角色,对接 LDAP 等的设置,如下:
这里我们创建用户 user:
创建完成后退出 admin,登录 user。
我们点击 Repository 下 Repositories 创建仓库:
然后这里填写相关信息,创建仓库 java:
到此仓库就创建成功了。
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:
最后使用 Maven 命令打包发布到 Nexus 即可:
mvn clean source:jar package
mvn deploy -e
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:
然后双击 uploadArchives 命令打包发布到 Nexus 即可:
等待 module 的上传,当全部显示ok时上传成功。
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'