阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

使用Nexus搭建私服

161次阅读
没有评论

共计 8196 个字符,预计需要花费 21 分钟才能阅读完成。

Nexus 是一个强大的 Maven 仓库管理器,它极大地简化了自己内部仓库的维护和外部仓库的访问。利用 Nexus 你可以只在一个地方就能够完全控制访问和部署在你所维护仓库中的每个 Artifact。Nexus 是一套“开箱即用”的系统不需要数据库,它使用文件系统加 Lucene 来组织数据。Nexus 使用 ExtJS 来开发界面,利用 Restlet 来提供完整的 REST APIs,通过 m2eclipse 与 Eclipse 集成使用。Nexus 支持 WebDAV 与 LDAP 安全身份认证。

安装 Nexus

下载

Nexus 有两种安装包,一种是包含 Jetty 容器的 Bundle 包,另一种是不包含 Web 容器的 war 包。我们可以从 http://www.sonatype.org/downloads/nexus-latest-bundle.zip 下载最新版本的 Nexus 的 Bundle 包。

解压 nexus-latest-bundle.zip,会发现有两个文件夹。如图 1:

使用 Nexus 搭建私服

 

nexus-2.7.2-03 目录包含了 Nexus 运行所需要的文件。是 Nexus 运行必须的。

 

sonatype-work 目录包含 Nexus 生成的配置文件、日志文件、仓库文件等。当我们需要备份 Nexus 的时候默认备份此目录即可。

 

安装

 

进入目录 nexus-2.7.2-03\bin\jsw,你会发现有很多的安装环境。我的操作系统是 64 位的 Windows 8,那么进入 windows-x86-64 目录。有以下文件,如图 2:

 

使用 Nexus 搭建私服

 

图 2

 

install-nexus.bat:将 Nexus 服务安装到 Windows 系统。

 

start-nexus.bat:启动 Nexus 服务。

 

stop-nexus.bat:停止 Nexus 服务。

 

uninstall-nexus.bat: 卸载 Nexus 服务。

 

右击 install-nexus.bat“以管理员身份运行”运行文件,右击 start-nexus.bat“以管理员身份运行”运行文件。在“服务”中,我们可以看到 Nexus 已经安装并且运行起来了,图 3:

 

使用 Nexus 搭建私服

 

图 3

 

安装之前要先安装 JDK,并在环境变量中配置 JAVA_HOME 和 PATH。

 

登录

 

在浏览器中输入:http://localhost:8081/nexus/ 得到以下界面,见图 4:

 

使用 Nexus 搭建私服

 

图 4

 

单击右上角的“Log In”按钮进行登录,Nexus 的默认管理员账户密码为 admin/admin123。

 

配置 Nexus

 

Nexus 仓库分类

 

Nexus 包含了各种类型的仓库类型。在登录后的 Nexus 界面,单击左边的“Repositories”链接,可以看到图 5 所示:

 

使用 Nexus 搭建私服

 

图 5

 

我们可以从上图中看到四种仓库类型:group(仓库组)、hosted(宿主)、proxy(代理)和 virtual(虚拟)。其中每种类型的 Format 有 Maven1 或者 Maven2,这里我们不看 Maven1 的。仓库的 Policy(策略)表示该仓库为发布(Release)版本还是快照(Snapshot)版本仓库。

 

在图 5 中,Nexus 列出了默认的几个仓库:

 

Public Repositories:仓库组,将所有策略为 Release 的仓库聚合并通过一致的地址提供服务。

 

3rd party:一个策略为 Release 的宿主类型仓库,用来部署无法从公共仓库获得的第三方发布版本构件。

 

Apache Snapshots:策略为 Snapshots 的代理仓库,用来代理 Apache Maven 仓库的快照版本构件。

 

Central:该仓库代理 Maven 的中央仓库,策略为 Release,只会下载和缓存中央仓库中的发布版本构件。

 

Central M1 shadow:maven1 格式的虚拟类型仓库。

 

Codehaus Snapshots:代理 Codehaus Maven 仓库快照版本的代理仓库。

 

Release:策略为 Release 的宿主类型仓库,用来部署组织内部的发布版本构件。

 

Snapshots:策略为 Snapshots 的宿主类型仓库,用来部署组织内部的快照版本构件。

 

用《Maven 实战》中一张图来解释下仓库的分类:

 

使用 Nexus 搭建私服

 

图 6

私有仓库配置

Nexus 的索引与构件搜索

点击图 5 中列表上的“Public Repositories”行,在下方的“Configuration”中我们可以看到,在“Ordered Group Repositories”中包含了 Release、Snapshots、3rd party、Central 等仓库。

为了构建 Nexus 的 Maven 中央库索引,首先需要设置 Nexus 中 Maven Cencal 代理仓库下载远程索引,将“Download Remote Indexes”的值从默认值 false 改为 true。如图 7:

使用 Nexus 搭建私服

图 7

点击“Save”后,点击左边的“Scheduled Tasks”链接,会出现图 8 界面,说明 Nexus 后台在下载 Maven 中央仓库的索引。

使用 Nexus 搭建私服

图 8

如果没有出现图 8 界面,那么要在“Public Repositories”行右击,点击“Update Index”。

使用 Nexus 搭建私服

图 9

在“Repositories”界面中,选择 Browse Index 选项卡,可以看到 Maven 中央仓库内容的树形结构,如图 10。

使用 Nexus 搭建私服

图 10

如果还是构建不成功,参考:

http://www.linuxidc.com/Linux/2014-03/98708.htm

http://www.linuxidc.com/Linux/2014-03/98709.htm

 

在左边的搜索框中输入“spring”关键字,会出现一大堆与 spring 相关的结果。

如果要对宿主仓库和代理仓库建立索引。只需要在对应的仓库上右击,从弹出的快捷菜单中选择“ReIndex”即可。

配置 Maven 从 Nexus 下载构件

在上面的基础上,我们可以用 Nexus 私服了。有 2 种方式:

1、在项目的 POM 中配置

<repositories>
 <repository>
  <id>local_nexus</id>
  <name>local_nexus</name>
  <url>http://localhost:8081/nexus/content/groups/public/</url>
  <releases>
   <enabled>true</enabled>
  </releases>
  <snapshots>
   <enabled>true</enabled>
  </snapshots>
 </repository>
</repositories>
<pluginRepositories>
 <pluginRepository>
  <id>local_nexus</id>
  <name>local_nexus</name>
  <url>http://localhost:8081/nexus/content/groups/public/</url>
  <releases>
   <enabled>true</enabled>
  </releases>
  <snapshots>
   <enabled>true</enabled>
  </snapshots>
 </pluginRepository>
</pluginRepositories>

Nexus 是一个强大的 Maven 仓库管理器,它极大地简化了自己内部仓库的维护和外部仓库的访问。利用 Nexus 你可以只在一个地方就能够完全控制访问和部署在你所维护仓库中的每个 Artifact。Nexus 是一套“开箱即用”的系统不需要数据库,它使用文件系统加 Lucene 来组织数据。Nexus 使用 ExtJS 来开发界面,利用 Restlet 来提供完整的 REST APIs,通过 m2eclipse 与 Eclipse 集成使用。Nexus 支持 WebDAV 与 LDAP 安全身份认证。

安装 Nexus

下载

Nexus 有两种安装包,一种是包含 Jetty 容器的 Bundle 包,另一种是不包含 Web 容器的 war 包。我们可以从 http://www.sonatype.org/downloads/nexus-latest-bundle.zip 下载最新版本的 Nexus 的 Bundle 包。

解压 nexus-latest-bundle.zip,会发现有两个文件夹。如图 1:

使用 Nexus 搭建私服

 

nexus-2.7.2-03 目录包含了 Nexus 运行所需要的文件。是 Nexus 运行必须的。

 

sonatype-work 目录包含 Nexus 生成的配置文件、日志文件、仓库文件等。当我们需要备份 Nexus 的时候默认备份此目录即可。

 

安装

 

进入目录 nexus-2.7.2-03\bin\jsw,你会发现有很多的安装环境。我的操作系统是 64 位的 Windows 8,那么进入 windows-x86-64 目录。有以下文件,如图 2:

 

使用 Nexus 搭建私服

 

图 2

 

install-nexus.bat:将 Nexus 服务安装到 Windows 系统。

 

start-nexus.bat:启动 Nexus 服务。

 

stop-nexus.bat:停止 Nexus 服务。

 

uninstall-nexus.bat: 卸载 Nexus 服务。

 

右击 install-nexus.bat“以管理员身份运行”运行文件,右击 start-nexus.bat“以管理员身份运行”运行文件。在“服务”中,我们可以看到 Nexus 已经安装并且运行起来了,图 3:

 

使用 Nexus 搭建私服

 

图 3

 

安装之前要先安装 JDK,并在环境变量中配置 JAVA_HOME 和 PATH。

 

登录

 

在浏览器中输入:http://localhost:8081/nexus/ 得到以下界面,见图 4:

 

使用 Nexus 搭建私服

 

图 4

 

单击右上角的“Log In”按钮进行登录,Nexus 的默认管理员账户密码为 admin/admin123。

 

配置 Nexus

 

Nexus 仓库分类

 

Nexus 包含了各种类型的仓库类型。在登录后的 Nexus 界面,单击左边的“Repositories”链接,可以看到图 5 所示:

 

使用 Nexus 搭建私服

 

图 5

 

我们可以从上图中看到四种仓库类型:group(仓库组)、hosted(宿主)、proxy(代理)和 virtual(虚拟)。其中每种类型的 Format 有 Maven1 或者 Maven2,这里我们不看 Maven1 的。仓库的 Policy(策略)表示该仓库为发布(Release)版本还是快照(Snapshot)版本仓库。

 

在图 5 中,Nexus 列出了默认的几个仓库:

 

Public Repositories:仓库组,将所有策略为 Release 的仓库聚合并通过一致的地址提供服务。

 

3rd party:一个策略为 Release 的宿主类型仓库,用来部署无法从公共仓库获得的第三方发布版本构件。

 

Apache Snapshots:策略为 Snapshots 的代理仓库,用来代理 Apache Maven 仓库的快照版本构件。

 

Central:该仓库代理 Maven 的中央仓库,策略为 Release,只会下载和缓存中央仓库中的发布版本构件。

 

Central M1 shadow:maven1 格式的虚拟类型仓库。

 

Codehaus Snapshots:代理 Codehaus Maven 仓库快照版本的代理仓库。

 

Release:策略为 Release 的宿主类型仓库,用来部署组织内部的发布版本构件。

 

Snapshots:策略为 Snapshots 的宿主类型仓库,用来部署组织内部的快照版本构件。

 

用《Maven 实战》中一张图来解释下仓库的分类:

 

使用 Nexus 搭建私服

 

图 6

这样的配置只对当前的 Maven 项目有效。但是,实际的项目开发中,我们都想着一次配置就能让本机的所有 Maven 项目都使用 Maven 私服,那么我们就要用到下面的方法。

2、在 settings.xml 中配置远程仓库。

Maven 提供的 profile 是一组可选的配置,可以用来设置或者覆盖配置默认值。有了 profile,你就可以为不同的环境定制构建。那么在 settings.xml 中加入以下代码:

<settings>
  …
 <profiles>
  <profile>
   <id>local_nexus</id>
   <repositories>
    <repository>
     <id>local_nexus</id>
     <name>local_nexus</name>
     <url>http://localhost:8081/nexus/content/groups/public/</url>
     <releases>
      <enabled>true</enabled>
     </releases>
     <snapshots>
      <enabled>true</enabled>
     </snapshots>
    </repository>
   </repositories>
   <pluginRepositories>
    <pluginRepository>
     <id>local_nexus</id>
     <name>local_nexus</name>
     <url>http://localhost:8081/nexus/content/groups/public/</url>
     <releases>
      <enabled>true</enabled>
     </releases>
     <snapshots>
      <enabled>true</enabled>
     </snapshots>
    </pluginRepository>
   </pluginRepositories>
  </profile>
 </profiles>
 <activeProfiles>
  <activeProfile>local_nexus</activeProfile>
 </activeProfiles>
  …
</settings>

上面的配置中,使用了一个 id 为 local_nexus 的 profile,这个 profile 包含了相关的仓库配置,同时配置中又使用了 activeProfiles 元素将 nexus 这个 profile 激活,这样当执行 Maven 构建的时候,激活的 profile 会将仓库配置应用到项目中去。

通过上面的配置,我们会发现 Maven 除了从 Nexus 下载构件外还会从中央仓库下载构件构件。既然是私服,那么我们就只希望 Maven 下载请求都仅仅通过 Nexus。我们可以通过镜像实现这一需求。可以创建一个匹配任何仓库的镜像,镜像的地址是私服,这样 Maven 对任何仓库的构件下载请求都会转到私服中。把上面的配置修改为如下配置:

<settings>
  …
 <mirrors>
  <mirror>
   <id>local_mirror</id>
   <mirrorOf>*</mirrorOf>
   <name>local_mirror</name>
   <url>http://localhost:8081/nexus/content/groups/public/</url>
  </mirror>
 </mirrors>
 
 <profiles>
  <profile>
   <id>local_nexus</id>
   <repositories>
    <repository>
     <id>local_nexus</id>
     <name>local_nexus</name>
     <url>http://localhost:8081/nexus/content/groups/public/</url>
     <releases>
      <enabled>true</enabled>
     </releases>
     <snapshots>
      <enabled>true</enabled>
     </snapshots>
    </repository>
    <repository>
     <id>central</id>
     <!– 由于配置过镜像,这个 url 不起作用 –>
     <url>http://repo.maven.apache.org/maven2</url>
     <releases>
      <enabled>true</enabled>
     </releases>
     <snapshots>
      <enabled>true</enabled>
     </snapshots>
    </repository>
   </repositories>
   <pluginRepositories>
    <pluginRepository>
     <id>local_nexus</id>
     <name>local_nexus</name>
     <url>http://localhost:8081/nexus/content/groups/public/</url>
     <releases>
      <enabled>true</enabled>
     </releases>
     <snapshots>
      <enabled>true</enabled>
     </snapshots>
    </pluginRepository>
    <pluginRepository>
     <id>central</id>
     <url>http://repo.maven.apache.org/maven2</url>
     <releases>
      <enabled>true</enabled>
     </releases>
     <snapshots>
      <enabled>true</enabled>
     </snapshots>
    </pluginRepository>
   </pluginRepositories>
  </profile>
 </profiles>
 <activeProfiles>
  <activeProfile>local_nexus</activeProfile>
 </activeProfiles>
  …
</settings>

部署构件到私服

我们在实际开发过程是多个人的,那么总有一些公共模块或者说第三方构件是无法从 Maven 中央库下载的。我们需要将这些构件部署到私服上,供其他开发人员下载。用户可以配置 Maven 自动部署构件至 Nexus 的宿主仓库,也可以通过界面手动上传构件。

使用 Maven 部署构件到 Nexus 私服上

日常开发的快照版本部署到 Nexus 中策略为 Snapshot 的宿主仓库中,正式项目部署到策略为 Release 的宿主仓库中,POM 的配置方式如下:


<distributionManagement>
 <repository>
  <id>local_nexus_releases</id>
  <name>core Release Repository</name>
  <url>http://localhost:8081/nexus/content/repositories/releases/</url>
 </repository>
 <snapshotRepository>
  <id>local_nexus_snapshots</id>
  <name>core Snapshots Repository</name>
  <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
 </snapshotRepository>
</distributionManagement>

其中,验证信息中 serviceid应该与 POMrepositoryid 一致。

 

Nexus 界面上手动部署第三方构件至私服

 

我们除了自己的构件要部署到 Nexus 私服上外,我们有可能还要将第三方构件(如:SQLServiceJDBC)部署到Nexus 上。这个时候,在 Nexus 界面上选择一个宿主仓库(如 3rd party),再在页面下方选择Artifact Upload 选项卡。填写对应的 Maven 坐标。然后点击“Select Artifact(s) for Upload”按钮从本机选择要上传的构件,然后点击“Add Artifact”按钮将其加入到上传列表中。最后,单击页面底部的“Upload Artifact(s)”按钮将构件上传到仓库中。

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-20发表,共计8196字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中