Maven私服(Nexus)部署
一. 概述
Nexus是一个maven私服,主要解决的问题有:
1. 由于国内的网络环境比较恶劣,从maven中央仓库下载依赖库比较慢,因此架设nexus私服从内网获取,大大提高下载速度。
2. 多个不同的项目存在多个相同的依赖,若每个项目都独自通过中央仓库或其他maven仓库中获取依赖,占用网络资源且效率低下,使用nexus私服仅在依赖库第一次获取时需要从中央仓库或其他maven仓库中获取,之后便可从内网获取。
3. 一些依赖库中央仓库没有,通常的做法是在项目的pom.xml文件中指定仓库的位置去获取依赖库,不太方便。在nexus私服上可以配置该仓库的代理,对项目来说是透明的,在项目的pom.xml中只需要指定私服仓库的位置即可。
4. 内部的一些公共包可以上传到nexus私服,供其他项目使用。
二. 环境
操作系统:windows7 x86-64
Java版本:jdk8+
IP: 10.10.10.131
端口: 8081
三. 安装
下载地址:http://www.sonatype.org/nexus/go
159专网下载
解压后进入D:\nexus\nexus-2.14.5-02\bin\jsw,根据操作系统类型选择windows-x86-64文件夹,进入后可看到如下所示bat文件。
双击console-nexus.bat运行。游览器中输入http://127.0.0.1:8081/nexus/,出现下图所示就代表nexus已经启动成功。
8081为默认的端口号,要修改端口号可进入nexus-2.14.5-02\conf\打开nexus.properties文件,修改application-port属性值就可以了。
默认的用户名和密码:admin/admin123(密码改成admin123$),登录后看到下所示:
四. 配置
4.1 maven项目索引
下载Maven项目索引,项目索引是为了使用者能够在私服站点查找依赖使用的功能
保存后后台会运行一个任务,点击菜单栏的Scheduled Tasks选项即可看到有个任务在RUNNING。下载完成后,Maven索引就可以使用了,在搜索栏输入要搜索的项,就可以查到相关的信息。例如hadoop
4.2 创建Proxy代理仓库
选择类型:maven2(proxy):
http://maven.aliyun.com/nexus/content/groups/public/
http://maven.oschina.net/content/groups/public/
https://repo1.maven.org/maven2/
https://repository.apache.org/content/repositories/releases/
https://repository.apache.org/content/repositories/snapshots
https://repo.spring.io/libs-release
https://repository.cloudera.com/artifactory/cloudera-repos/
4.3创建两个本地仓库
分别为release和snapshots,选择类型:maven2(hosted)。
4.4 创建一个仓库组
选择类型:maven2(group),将上述代理仓库全部添加到此仓库组。
五、使用私服
5.1项目发布
5.1.1手工发布
案例:发布一个jar包到3rd party宿主库中
选中宿主库——3rd party,之后选择ArtifactUpload上传至宿主空间。
最后点击上传
5.1.2通过配置发布
5.1.2.1 Pom.xml配置
首先在本项目pom.xml中添加
<distributionManagement>
<repository>
<id>user-release</id>
<name>User Project Release</name>
<url>http://106.6.10.131:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>user-snapshots</id>
<name>User Project SNAPSHOTS</name>
<url>http://106.6.10.131:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
5.1.2.2 settings.xml设置发布权限
5.1.2.3 运行发布
5.1.2.4 私服验证
然后进入到私服上的仓库中,看一下确实存在刚刚发布的项目
5.2 项目下载
5.2.1局部配置
5.2.1.1 Pom.xml配置
即只在本项目的pom.xml中配置,仅对本项目生效。
<!-- 指定仓库 -->
<repositories>
<repository>
<id>nexus</id>
<name>nexus</name>
<url>http://106.6.10.131:8081/nexus/content/groups/MyRepositories/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<!-- 指定插件仓库 -->
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<name>nexus</name>
<url>http://106.6.10.131:8081/nexus/content/groups/MyRepositories/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<!-- 指定要下载的依赖包 -->
<dependencies>
<dependency>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>com.test</groupId>
<artifactId>jfxswt</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
5.2.1.2 验证
5.2.2全局配置
5.2.2.1 settings.xml配置
修改settings.xml,对所有配置生效
maven提供了profile来配置仓库信息,如下所示:
<profiles>
<profile>
<id>myprofile</id>
<repositories>
<repository>
<id>central</id>
<url>http://106.6.10.131:8081/nexus/content/groups/MyRepositories/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://106.6.10.131:8081/nexus/content/groups/MyRepositories/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
**profile
<activeProfiles>
<activeProfile>myprofile</activeProfile>
</activeProfiles>
Settings.xml配置镜像
<mirror>
<id>nexus</id>
<url>http://106.6.10.131:8081/nexus/content/groups/MyRepositories/</url>
<mirrorOf>*</mirrorOf>
</mirror>
这里配置mirrorOf的值为*,代表maven的所有访问请求都会指向到Nexus仓库组。