1 . 私服简介java
私服是架设在局域网的一种特殊的远程仓库,目的是代理远程仓库及部署第三方构件。有了私服以后,当 Maven 须要下载构件时,直接请求私服,私服上存在则下载到本地仓库;不然,私服请求外部的远程仓库,将构件下载到私服,再提供给本地仓库下载。web
咱们可使用专门的 Maven 仓库管理软件来搭建私服,好比:Apache Archiva,Artifactory,Sonatype Nexus。这里咱们使用 Sonatype Nexus。shell
2 . 安装Nexusapache
2 . 1 . 下载Nexus浏览器
Nexus 专业版是须要付费的,这里咱们下载开源版 Nexus OSS。Nexus 提供两种安装包,一种是包含 Jetty 容器的 bundle 包,另外一种是不包含容器的 war 包。下载地址:http://www.sonatype.org/nexus/go。oracle
2 . 2 . 使用bundle安装包安装Nexuseclipse
解压安装包nexus-2.8.1-bundle.zip,打开命令提示符,进入/nexus-2.8.1-01目录,键入nexus命令(为方便启动和退出Nexus,可将bin目录添加到环境变量):maven
执行 nexus install 将Nexus安装为Windows服务。可将服务启动方式设为手动,之后经过 nexus start 便可启动Nexus ,经过 nexus stop 退出Nexus:ui
打开浏览器,访问:http://localhost:8081/nexus/:this
点击右上角 Log In,使用用户名:admin ,密码:admin123 登陆,可以使用更多功能:
3 . Nexus预置的仓库
点击左侧 Repositories 连接,查看 Nexus 内置的仓库:
Nexus 的仓库分为这么几类:
4 . 添加代理仓库
以 Sonatype 为例,添加一个代理仓库,用于代理 Sonatype 的公共远程仓库。点击菜单 Add - Proxy Repository :
填写Repository ID - sonatype;Repository Name - Sonatype Repository;
Remote Storage Location - http://repository.sonatype.org/content/groups/public/ ,save 保存:
将添加的 Sonatype 代理仓库加入 Public Repositories 仓库组。选中 Public Repositories,在 Configuration 选项卡中,将 Sonatype Repository 从右侧 Available Repositories 移到左侧 Ordered Group Repositories,save 保存:
5 . 搜索构件
为了更好的使用 Nexus 的搜索,咱们能够设置全部 proxy 仓库的 Download Remote Indexes 为 true,即容许下载远程仓库索引。
索引下载成功以后,在 Browse Index 选项卡下,能够浏览到全部已被索引的构件信息,包括坐标、格式、Maven 依赖的 xml 代码:
有了索引,咱们就能够搜索了:
6 . 配置Maven使用私服
私服搭建成功,咱们就能够配置 Maven 使用私服,之后下载构件、部署构件,都经过私服来管理。
在 settings.xml 文件中,为全部仓库配置一个镜像仓库,镜像仓库的地址即私服的地址(这儿咱们使用私服公共仓库组 Public Repositories 的地址):
<mirrors> <mirror> <id>central</id> <mirrorOf>*</mirrorOf> <!-- * 表示让全部仓库使用该镜像--> <name>central-mirror</name> <url>http://localhost:8081/nexus/content/groups/public/</url> </mirror> </mirrors>
PS1:私有maven仓库Nexus使用http与https协议
Http协议:没有问题
Https协议:
Could not GET 'https://some_server.com/some/path/some.pom'. > peer not authenticated
解决:
If you get any other error like this:
Could not GET 'https://some_server.com/some/path/some.pom'. > peer not authenticated
Then you need to import a certificate:
JDK_HOME/jre/lib/security
folderJDK_HOME/jre/lib/security
folderkeytool -import -alias <the short name of the server> -file <cert_file_name_you_exported.cer> -keystore cacerts -storepass changeit
It will prompt you to import the certificate, type yes and press enter.
Then restart your eclipse and try building the project.