Nexus索引与构件搜索
我们知道,Maven 中央仓库为用户提供了多达数十万构件,而 Nexus 可以代理所有的远程仓库(包括 Maven 中央仓库),可见 Nexus 仓库中构件的数量相当庞大。用户想要在这么多构件中,快速的查找自己所需的构件,一个最直接有效的方式就是:搜索。
Nexus 作为一款成熟的仓库管理工具,它通过维护仓库的索引提供了构件搜索功能,以便帮助用户方便快速地找到所需构件。
本节我们将详细为您介绍 Nexus 索引以及构件搜索功能。
索引
Nexus 能够遍历仓库的所有内容,搜集它们的坐标,校验和以及所包含的 Java 类等信息,然后以索引( nexus-indexer) 的形式保存起来。Nexus 索引保存在 Nexus 安装目录下 sonatype-work exusindexer 目录中,该目录下每个子目录都代表 Nexus 中的一个仓库,用来存放各个仓库的索引 ,如下图所示。
大多数的远程公共仓库(例如,中央仓库)都维护了一个这样的索引,因此本地的 Nexus 在下载到这个索引后,就能在此基础上为用户提供构件搜索和浏览等服务。需要注意的是,并不是所有的公共仓库都提供了索引 ,对于那些没有提供索引的仓库来说,我们是无法对其进行搜索的。
下载索引
Nexus 索引下载功能默认是关闭的,如果想在 Nexus 中搜索远程仓库中的构件,就需要先开启索引下载功能。
以 bianchengbang_central_proxy 代理仓库(代理 Maven 中央仓库)为例,在图 2 所示的位置就可以控制它的索引下载功能,true 表示开启索引下载,false 表示关闭索引下载。
开启索引下载功能后,点击导航栏中 Scheduled Tasks 链接,查看调度任务列表。若 Nexus 正在下载索引,我们就能看到如图 3 所示的任务,其任务状态为 Running。在索引下载完毕后,该任务就会消失。
由于 Maven 中央仓库的内容较多,其索引文件比较大,因此 Nexus 下载该文件所需的时间较长,需要我们耐心等待。
索引下载完成后,点击仓库列表中的 bianchengbang_central_proxy 代理仓库,然后在列表下方选择 Browse Index 选项卡,我们可以看到该仓库内容的树形结构,如图 4 所示。
图4:Nexus 仓库索引树形结构图
构件搜索
Nexus 通过维护索引,为用户提供了关键字搜索、类名搜索、坐标搜索等多种搜索功能,通过这些功能,页面中能够清晰地展示出结果构件的坐标以及所属仓库。用户可以直接下载相应的构件,也可以直接复制构件的 XML 依赖声明,到项目中使用。
在 Nexus 界面左边导航栏中有一个搜索框,在搜索框内输入所需构件的关键字,单击搜索按钮就能快速得到搜索结果,如图 5 所示。
搜索结果页中的每一行代表一类构件,其中包含了 Group、Artifact、版本、流行版本、以及下载链接等信息。点击其中某一行,下方就会出现该构件的详细信息,其中不但包含构件的坐标等基本信息,还包含一段 XML 依赖声明,我们可以将这段 XML 依赖声明直接复制到项目的 POM 中使用,如图 6 所示。
点击右侧的 Artifact 选项卡,还能看到构件的大小、上传时间、最后修改时间、仓库中的相对位置、校验和等信息,如图 7 所示。
除了关键字搜索外,Nexus 还提供了一些高级搜索功能,例如类名搜索、GAV 搜索以及“校验和”搜索。我们可以通过点击搜索页左上角的下拉菜单,选择高级搜索功能。
Nexus 还具有以下高级搜索功能:
- 类名搜索(Keyword Search):搜索包含某个 Java 类的构件。
- GAV 搜索(GAV Search):通过设置 Group、Artifact、版本等信息进行搜索。
- 校验和搜索(Checksum Search):通过使用“校验和”搜索构件。
这些搜索功能的使用都十分的简单,我们可以根据自己的需求选择合适的搜索功能。
从Nexus下载构件
Nexus 作为最流行的 Maven 私服之一,使用它主要目的之一:代理远程仓库,即当 Maven 需要下载构件到本地仓库使用时,不再请求外部的远程仓库,而直接从 Nexus 中下载。本节我们将介绍如何配置 Maven 从 Nexus 下载构件。
将 Nexus 的代理仓库配置到 Maven 项目中,用它们代替外部的远程仓库,即可实现 Maven 从 Nexus 下载构件。
- 在 pom.xml 中配置
- 在 setting.xml 中配置
在 pom.xml 中配置
在 Maven 项目的 pom.xml 中增加以下配置,可以为当前项目配置 Nexus 上的 bianchengbang_central_proxy 代理仓库。
<!--声明一个或多个远程仓库 --> <repositories> <!-- 声明一个 Nexus 私服上的仓库 --> <repository> <!--仓库id --> <id>nexus</id> <!-- 仓库的名称 --> <name>nexus</name> <!--仓库的地址 --> <url>http://localhost:8082/nexus/content/repositories/bianchengbang_central_proxy/</url> <!-- 是否开启该仓库的 release 版本下载支持 --> <releases> <enabled>true</enabled> </releases> <!-- 是否开启该仓库的 snapshot 版本下载支持 --> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <!-- 声明一个或多个远程插件仓库 --> <pluginRepositories> <!--声明一个 Nexus 私服上的插件仓库 --> <pluginRepository> <!--插件仓库 id --> <id>nexus</id> <!--插件仓库 名称 --> <name>nexus</name> <!-- 配置的插件仓库的地址 --> <url>http://localhost:8082/nexus/content/repositories/bianchengbang_central_proxy/</url> <!-- 是否开启该插件仓库的 release 版本下载支持 --> <releases> <enabled>true</enabled> </releases> <!-- 是否开启该插件仓库的 snapshot 版本下载支持 --> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> </pluginRepositories>
其中,在 repositories 元素下可以使用 repository 子元素声明一个或多个远程仓库,一个 repository 元素对应一个远程仓库。
repository 各个子元素含义如下表。
子元素 | 含义 |
---|---|
id | 仓库的唯一标识。需要注意的是,Maven 中央仓库的 id 为 central,如果其他的仓库也使用该 id,就会覆盖中央仓库的配置。 |
name | 仓库的名称。 |
url | 仓库的地址,该地址一般都是基于 HTTP 协议的,通过浏览器即可访问该地址,浏览仓库中的构件。 |
releases/snapshots | 用来控制 Maven 对于发布和快照版本构件的下载。它们都有一个 enabled 子元素,enabled 的取值为 true,表示开启发布版或快照版的下载支持,否则表示关闭下载支持。 |
在 pluginRepositories 元素下可以使用 pluginRepository 子元素声明一个或多个远程插件仓库(包括私服上的仓库),一个 pluginRepository 元素对应一个远程插件仓库。pluginRepository 下所有子元素的含义均与与 repository 的子元素相同,这里就不再做过多赘述了。
示例 1
将以上配置加入 Maven 项目 Root 的 POM 文件中,并手动清空本地仓库的所有依赖构件(目的是为了让 Maven 重新获取构件)。
打开命令行窗口,跳转到 Root 所在的目录下,执行以下 mvn 命令。
mvn clean install
执行结果如下。
由图 1 的构建过程可以看出,Root 项目构建所需的构建是从 Nexus 私服的 banchengbang_central_proxy 代理仓库中下载的,而不是从 Maven 中央仓库去下载。
在 setting 中配置
在 pom.xml 中配置 Nexus ,只对当前项目有效,在实际应用中,我们往往希望能够通过一次配置就能让本机中的所有 Maven 项目都使用 Nexus。此时,您可能会想到在 Maven 的 setting.xml 文件中进行配置,但 setting.xml 是不支持直接配置 repositories 和 pluginRepositories 。所幸 Maven 提供了 profile 机制,能够让我们将仓库配置放在 profile 中。
<profiles> <profile> <id>nexus</id> <!--声明一个或多个远程仓库 --> <repositories> <!-- 声明一个 Nexus 私服上的仓库 --> <repository> <!--仓库id --> <id>nexus</id> <!-- 仓库的名称 --> <name>nexus</name> <!--仓库的地址 --> <url>http://localhost:8082/nexus/content/repositories/bianchengbang_central_proxy/</url> <!-- 是否开启该仓库的 release 版本下载支持 --> <releases> <enabled>true</enabled> </releases> <!-- 是否开启该仓库的 snapshot 版本下载支持 --> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <!-- 声明一个或多个远程插件仓库 --> <pluginRepositories> <!--声明一个 Nexus 私服上的插件仓库 --> <pluginRepository> <!--插件仓库 id --> <id>nexus</id> <!--插件仓库 名称 --> <name>nexus</name> <!-- 配置的插件仓库的地址 --> <url>http://localhost:8082/nexus/content/repositories/bianchengbang_central_proxy/</url> <!-- 是否开启该插件仓库的 release 版本下载支持 --> <releases> <enabled>true</enabled> </releases> <!-- 是否开启该插件仓库的 snapshot 版本下载支持 --> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> </pluginRepositories> </profile> </profiles> <activeProfiles> <activeProfile>nexus</activeProfile> </activeProfiles>
以上配置中使用了一个 id 为 nexus 的 profile,这个 profile 中包含了与仓库相关的配置,同时配置中还使用了一个 activeProfiles 元素将 id 为 nexus 的 profile 激活。当本机有 Maven 项目构建时,profile 中的仓库配置就会应用到项目中。
示例 2
将以上配置分别添加到 Maven 安装目录conf 和本地仓库目录下的 setting.xml 中,并将 Root 项目 POM 文件中的 repositories 和 pluginRepositories 等仓库设置删除。
打开命令行窗口,跳转到 Root 项目下,执行以下 mvn 命令。
mvn clean install
执行结果如下。
setting.xml 中添加镜像
Nexus 私服通常会与镜像(mirror)结合使用,使 Nexus 成为所有远程仓库的私服,这样不仅可以从 Nexus 中获取所有所需构件,还能将配置集中到 Nexus 私服中,简化 Maven 本身的配置。
我们可以创建一个匹配任何仓库的镜像,镜像的地址为 Nexus 中仓库的地址,这样 Maven 对于任何构件的下载请求都会被拦截跳转到 Nexus 私服中,其具体配置如下。
<mirrors> <mirror> <id>nexus</id> <name>nexus name</name> <mirrorOf>*</mirrorOf> <url>http://localhost:8082/nexus/content/groups/bianchengbang_repository_group/</url> </mirror> </mirrors> <profiles> <profile> <id>nexus</id> <repositories> <repository> <id>central</id> <url>http://localhost:8082/nexus/content/repositories/bianchengbang_central_proxy/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>central</id> <url>http://localhost:8082/nexus/content/repositories/bianchengbang_central_proxy/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> </pluginRepositories> </profile> </profiles> <activeProfiles> <activeProfile>nexus</activeProfile> </activeProfiles>
仓库和插件仓库的配置中,它们的 id 取值都是 central,即它们的设置覆盖了 Maven 中央仓库,但此时这里的 URL 已经无法起到任何作用,因为镜像的匹配规则是 *,所有的请求都已经被拦截,跳转到 Nexus 私服的地址。
部署构件到Nexus
如果仅仅是为了代理远程的公共仓库,那么 Nexus 的代理仓库就完全能够满足需要,但是我们知道,Nexus 除了代理仓库外,还有另一种仓库:宿主仓库。
宿主仓库的主要作用是存储公司或组织内部的构件,以及一些无法从公共仓库获取的第三方构件(JDBC 驱动),供用户下载使用。用户可以通过配置 Maven 将构件自动部署到 Nexus 宿主仓库,也可以在 Nexus 界面手动上传构件。
使用 Maven 部署构件到 Nexus
日常开发中,快照版本构件可以直接部署到 Nexus 中策略为 Snapshot 的宿主仓库中,而最终发布的版本则应该部署到 Nexus 中策略为 Release 的宿主仓库中。
使用 Maven 将构件部署到 Nexus 宿主仓库中主要分为 3 步:
- 配置项目的 POM 文件。
- 在 setting.xml 中配置认证信息。
- 使用 mvn 命令部署构件。
下面我们通过一个实例来详细介绍如何使用 Maven 部署构件到 Nexus 宿主仓库。
1. 配置项目的 POM 文件
创建一个名为 test-upload-snapshot 的 Maven 项目,并在其 POM 文件中添加如下配置。
<project> ... <distributionManagement> <repository> <id>bianchengbang_Release_hosted</id> <url>http://localhost:8082/nexus/content/repositories/bianchengbang_Release_hosted/</url> </repository> <snapshotRepository> <id>bianchengbang_Snapshot_hosted</id> <url>http://localhost:8082/nexus/content/repositories/bianchengbang_Snapshot_hosted/</url> </snapshotRepository> </distributionManagement> </project>
以上配置说明如下:
- distributionManagement 元素:负责将指定的构件部署到 Nexus 指定的仓库中。
- repository 元素:distributionManagement 的子元素,用于定义部署 Release 版本的构件被部署的仓库。它有 2 子元素:id 和 url ,其中 id 为 Nexus 仓库的唯一标识,url 为 Nexus 宿主仓库的 url。
- snapshotRepository 元素:distributionManagement 的子元素,用于定义部署 Snapshot 版本的构件被部署的仓库。它也有 2 子元素:id 和 url ,且与 repository 中含义一致。
在 setting.xml 中配置认证信息
最初版本的 Nexus 没有为宿主仓库提供任何的安全措施。如果宿主仓库开启了部署功能,任何人可以连接并部署构件至这个仓库,这显然是极不安全的。因此,现在的 Nexus 中增加了权限认证,Nexus 对于匿名用户是只读的,若想部署构件到 Nexus 中,则需要在 setting.xml 中配置如下认证信息。
<settings>
...
<servers>
<server>
<id>bianchengbang_Release_hosted</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>bianchengbang_Snapshot_hosted</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
</settings>
注意:以上配置中,server 元素中的 id 必须和 pom.xml 中 distributionManagement 元素对应仓库的 id 保持一致。Maven 在部署构件时,会先根据 id 查找用户名称和密码进行认证和登录,然后将构件部署到对应得宿主仓库。
3. 使用 mvn 命令部署构件
完成以上配置之后,就可以使用 Maven 命令将构件部署到宿主仓库了,操作步骤如下。
1)打开命令行窗口,跳转到 test-upload-snapshot 项目得目录下,执行如下 Maven 命令。
mvn clean deploy
2) Maven 命令执行结果如下图。
[INFO] Scanning for projects... [INFO] [INFO] ---------------< net.biancheng.www:test-upload-snapshot >--------------- [INFO] Building test-upload-snapshot 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ test-upload-snapshot --- [INFO] Deleting D:eclipse workSpace 3 est-upload-snapshot arget [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ test-upload-snapshot --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ test-upload-snapshot --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ test-upload-snapshot --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ test-upload-snapshot --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ test-upload-snapshot --- [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ test-upload-snapshot --- [INFO] Building jar: D:eclipse workSpace 3 est-upload-snapshot arget est-upload-snapshot-0.0.1-SNAPSHOT.jar [INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) @ test-upload-snapshot --- [INFO] Installing D:eclipse workSpace 3 est-upload-snapshot arget est-upload-snapshot-0.0.1-SNAPSHOT.jar to D:myRep ository epository etianchengwww est-upload-snapshot0.0.1-SNAPSHOT est-upload-snapshot-0.0.1-SNAPSHOT.jar [INFO] Installing D:eclipse workSpace 3 est-upload-snapshotpom.xml to D:myRepository epository etianchengwww e st-upload-snapshot0.0.1-SNAPSHOT est-upload-snapshot-0.0.1-SNAPSHOT.pom [INFO] [INFO] --- maven-deploy-plugin:2.7:deploy (default-deploy) @ test-upload-snapshot --- Downloading from bianchengbang_Snapshot_hosted: http://localhost:8082/nexus/content/repositories/bianchengbang_Snapshot_ hosted/net/biancheng/www/test-upload-snapshot/0.0.1-SNAPSHOT/maven-metadata.xml Downloaded from bianchengbang_Snapshot_hosted: http://localhost:8082/nexus/content/repositories/bianchengbang_Snapshot_h osted/net/biancheng/www/test-upload-snapshot/0.0.1-SNAPSHOT/maven-metadata.xml (787 B at 9.2 kB/s) Uploading to bianchengbang_Snapshot_hosted: http://localhost:8082/nexus/content/repositories/bianchengbang_Snapshot_host ed/net/biancheng/www/test-upload-snapshot/0.0.1-SNAPSHOT/test-upload-snapshot-0.0.1-20210322.030343-5.jar Uploaded to bianchengbang_Snapshot_hosted: http://localhost:8082/nexus/content/repositories/bianchengbang_Snapshot_hoste d/net/biancheng/www/test-upload-snapshot/0.0.1-SNAPSHOT/test-upload-snapshot-0.0.1-20210322.030343-5.jar (2.1 kB at 28 k B/s) Uploading to bianchengbang_Snapshot_hosted: http://localhost:8082/nexus/content/repositories/bianchengbang_Snapshot_host ed/net/biancheng/www/test-upload-snapshot/0.0.1-SNAPSHOT/test-upload-snapshot-0.0.1-20210322.030343-5.pom Uploaded to bianchengbang_Snapshot_hosted: http://localhost:8082/nexus/content/repositories/bianchengbang_Snapshot_hoste d/net/biancheng/www/test-upload-snapshot/0.0.1-SNAPSHOT/test-upload-snapshot-0.0.1-20210322.030343-5.pom (2.4 kB at 36 k B/s) Downloading from bianchengbang_Snapshot_hosted: http://localhost:8082/nexus/content/repositories/bianchengbang_Snapshot_ hosted/net/biancheng/www/test-upload-snapshot/maven-metadata.xml Downloaded from bianchengbang_Snapshot_hosted: http://localhost:8082/nexus/content/repositories/bianchengbang_Snapshot_h osted/net/biancheng/www/test-upload-snapshot/maven-metadata.xml (297 B at 11 kB/s) Uploading to bianchengbang_Snapshot_hosted: http://localhost:8082/nexus/content/repositories/bianchengbang_Snapshot_host ed/net/biancheng/www/test-upload-snapshot/0.0.1-SNAPSHOT/maven-metadata.xml Uploaded to bianchengbang_Snapshot_hosted: http://localhost:8082/nexus/content/repositories/bianchengbang_Snapshot_hoste d/net/biancheng/www/test-upload-snapshot/0.0.1-SNAPSHOT/maven-metadata.xml (787 B at 14 kB/s) Uploading to bianchengbang_Snapshot_hosted: http://localhost:8082/nexus/content/repositories/bianchengbang_Snapshot_host ed/net/biancheng/www/test-upload-snapshot/maven-metadata.xml Uploaded to bianchengbang_Snapshot_hosted: http://localhost:8082/nexus/content/repositories/bianchengbang_Snapshot_hoste d/net/biancheng/www/test-upload-snapshot/maven-metadata.xml (297 B at 5.2 kB/s) [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.342 s [INFO] Finished at: 2021-03-22T11:03:44+08:00 [INFO] ------------------------------------------------------------------------
在 Nexus 界面的仓库列表选中 bianchengbang_Snapshot_hosted 宿主仓库,在下方的 Browse Index 选项卡中,可以看到 test-upload-snapshot 构件已经被部署到该仓库中,如图 1 所示。
手动上传构件
有些 Jar 文件(如 Oracle 的 JDBC 驱动)由于许可证等原因,无法存放在公开仓库中。此外,还有一些小型的开源项目,它们没有将自己的构件分发到公共仓库中,也没有维护自己的仓库,因此这些构件是无法从公共仓库中获得的。若 Maven 项目中需要这类构件,我们就需要将构件下载到本地,然后手动上传到 Nexus 私服。
我们还是以 Maven 项目 App-Core-lib 为例,将其打包后手动上传到 Nexus 内置宿主仓库 3rd party 中,具体步骤如下。
1. 打开命令行窗口,跳转到 App-Core-lib 的目录下,执行如下 mvn 命令,对项目进行打包。
mvn clean package
2.打包完成后,进入 App-Core-lib arget 目录,可以看到 Maven 已经将该项目打包成了一个 jar 文件,如图 2 所示。
3. 在 Nexus 界面的仓库列表中选择 3rd party 仓库,在下方选择 Artifact Upload 选项卡,其中 GAV Definition 用于定义上传构件的坐标信息。
GAV Definition 中提供了两种定义构件坐标的方式:
- 若该构件通过 Maven 构建产生的,则可以选择 From POM,Nexus 会自动从 POM 中获取构件的坐标。
- 若该构件来自第三方,则只能选择 GAV Parameters,手动定义构件的坐标。
以上 2 种方式操作时会略有不同,下面我们分别对它们进行介绍。
From POM
若该构件是通过 Maven 构建产生的,那么可以在 GAV Definition 下拉列表中,选择 From POM,然后指定该构件所属项目的 pom.xml,Nexus 会自动从 pom.xml 中获取构件的坐标。最后点击页面最下方的 Upload Artifact(s) 按钮,将构件上传到仓库中,如图 3 所示。
GAV Paramters
若构件不是通过 Maven 构建的,而是来自第三方(例如 Oracle 的 JDBC 驱动),则只能在 GAV Definition 下拉列表中,选择 GAV Parameters 手动定义构件的坐标。
定义好坐标后,点击 Select Artifact(s) to Upload... 按钮选择要上传的构件,然后点击 Add Artifact 按钮将其加入上传列表中。最后点击页面最下方的 Upload Artifact(s) 按钮,将构件上传到仓库中,如图 4 所示。
4. 在仓库列表中选中 3rd party 宿主仓库,在下方的 Browse Index 选项卡中,可以看到构件已经被部署到该仓库中,如图 5 所示。
图5:第三方构件手动上传到Nexus