Nexus索引與構件搜索
我們知道,Maven 中央倉庫為用戶提供了多達數十萬構件,而 Nexus 可以代理所有的遠程倉庫(包括 Maven 中央倉庫),可見 Nexus 倉庫中構件的數量相當龐大。用戶想要在這么多構件中,快速的查找自己所需的構件,一個最直接有效的方式就是:搜索。
Nexus 作為一款成熟的倉庫管理工具,它通過維護倉庫的索引提供了構件搜索功能,以便幫助用戶方便快速地找到所需構件。
本節我們將詳細為您介紹 Nexus 索引以及構件搜索功能。
索引
Nexus 能夠遍歷倉庫的所有內容,搜集它們的坐標,校驗和以及所包含的 Java 類等信息,然后以索引( nexus-indexer) 的形式保存起來。Nexus 索引保存在 Nexus 安裝目錄下 \sonatype-work\nexus\indexer 目錄中,該目錄下每個子目錄都代表 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\test-upload-snapshot\target [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\test-upload-snapshot\target\test-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\test-upload-snapshot\target\test-upload-snapshot-0.0.1-SNAPSHOT.jar to D:\myRep ository\repository\net\biancheng\www\test-upload-snapshot\0.0.1-SNAPSHOT\test-upload-snapshot-0.0.1-SNAPSHOT.jar [INFO] Installing D:\eclipse workSpace 3\test-upload-snapshot\pom.xml to D:\myRepository\repository\net\biancheng\www\te st-upload-snapshot\0.0.1-SNAPSHOT\test-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\target 目錄,可以看到 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