前言
随着我们在开发中所需构件越来越多,需要通过maven的中央仓库和第三方的Maven仓库下载到本地,而一个团队中的所有人都重复的从maven仓库下载构件无疑加大了仓库的负载和浪费了外网带宽。很多情况下项目的开发都是在内网进行的,连接不到maven仓库怎么办呢?开发的公共构件怎么让其它项目使用?这个时候我们不得不为自己的团队搭建属于自己的maven私服,这样既节省了网络带宽也会加速项目搭建的进程。
一、Nexus是什么?
-
nexus是一个强大的maven仓库管理器,它极大的简化了本地内部仓库的维护和外部仓库的访问。
-
nexus是一套开箱即用的系统不需要数据库,它使用文件系统加Lucene来组织数据。
-
nexus使用ExtJS来开发界面,利用Restlet来提供完整的REST APIs,通过IDEA和Eclipse集成使用。
-
nexus支持webDAV与LDAP安全身份认证。
-
nexus提供了强大的仓库管理功能,构件搜索功能,它基于REST,友好的UI是一个extjs的REST客户端,占用较少的内存,基于简单文件系统而非数据库。
二、使用步骤
1.下载Nexus:
-
下载地址:https://www.sonatype.com/download-oss-sonatype
-
下载解压后在这个文件夹下输入cmd启动nexus
-
在浏览器输入127.0.0.1:8081 就可以访问了,登陆后是这样,点击Sign in 进行登陆 默认账号:admin 密码:admin123
2.配置参数
Maven用到的Repositories说明:
-
maven-central:maven中央库,默认从https://repo1.maven.org/maven2/拉取jar
-
maven-releases:私库发行版jar
-
maven-snapshots:私库快照(调试版本)jar
-
maven-public:仓库分组,把上面三个仓库组合在一起对外提供服务,在本地maven基础配置settings.xml中使用。
-
aliyun:是我自己创建的仓库,url的地址写的是阿里云镜像的地址:
http://maven.aliyun.com/nexus/content/groups/public/
- 下面这些是在maven的settig.xml中的配置,
<servers>
<server>
<id>Nexus Mirror</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>maven-public</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>maven-releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>maven-snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
<mirrors>
<mirror>
<id>Nexus Mirror</id>
<name>Nexus Mirror</name>
<url>http://127.0.0.1:8081/repository/maven-public/</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>
<profiles>
<profile>
<id>nexus</id>
<repositories>
<!--包含需要连接到远程仓库的信息 -->
<repository>
<!--远程仓库唯一标识 -->
<id>maven-public</id>
<!--远程仓库URL,按protocol://hostname/path形式 -->
<url>http://localhost:8081/repository/maven-public/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>maven-releases</id>
<url>http://localhost:8081/repository/maven-releases/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>maven-snapshots</id>
<url>http://localhost:8081/repository/maven-snapshots/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<!--远程仓库唯一标识 -->
<id>maven-public</id>
<!--远程仓库URL,按protocol://hostname/path形式 -->
<url>http://localhost:8081/repository/maven-public/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
<pluginRepository>
<id>maven-releases</id>
<url>http://localhost:8081/repository/maven-releases/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
<pluginRepository>
<id>maven-snapshots</id>
<url>http://localhost:8081/repository/maven-snapshots/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
- 下面是pom.xml文件的配置
<distributionManagement>
<repository>
<!--这个id要和setting.xml文件里配置的一样 -->
<id>maven-releases</id>
<name>release</name>
<url>http://localhost:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>maven-snapshots</id>
<name>snapshots</name>
<url>http://localhost:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
3.上传到Nexus私服
- 点击maven中的deploy就上传到私服就行了
- 在私服上就可以看到了,所下jar都从私服展示了