從Nexus下載構(gòu)件

Nexus 作為最流行的 Maven 私服之一,使用它主要目的之一:代理遠程倉庫,即當 Maven 需要下載構(gòu)件到本地倉庫使用時,不再請求外部的遠程倉庫,而直接從 Nexus 中下載。本節(jié)我們將介紹如何配置 Maven 從 Nexus 下載構(gòu)件。

將 Nexus 的代理倉庫配置到 Maven 項目中,用它們代替外部的遠程倉庫,即可實現(xiàn) Maven 從 Nexus 下載構(gòu)件。

  • 在 pom.xml 中配置

  • 在 setting.xml 中配置

在 pom.xml 中配置

在 Maven 項目的 pom.xml 中增加以下配置,可以為當前項目配置 Nexus 上的 bianchengbang_central_proxy 代理倉庫。

<!-- 聲明一個或多個遠程倉庫 -->
<repositories>
    <!-- 聲明一個 Nexus 私服上的倉庫 -->
    <repository>
    <!-- 倉庫id -->
    <id>nexus<id>
    <!-- 倉庫的名稱 -->
    <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 元素對應(yīng)一個遠程倉庫。

repository 各個子元素含義如下表。

子元素 含義
id 倉庫的唯一標識。需要注意的是,Maven 中央倉庫的 id 為 central,如果其他的倉庫也使用該 id,就會覆蓋中央倉庫的配置。
name 倉庫的名稱。
url 倉庫的地址,該地址一般都是基于 HTTP 協(xié)議的,通過瀏覽器即可訪問該地址,瀏覽倉庫中的構(gòu)件。
releases/snapshots 用來控制 Maven 對于發(fā)布和快照版本構(gòu)件的下載。它們都有一個 enabled 子元素,enabled 的取值為 true,表示開啟發(fā)布版或快照版的下載支持,否則表示關(guān)閉下載支持。

在 pluginRepositories 元素下可以使用 pluginRepository 子元素聲明一個或多個遠程插件倉庫(包括私服上的倉庫),一個 pluginRepository 元素對應(yīng)一個遠程插件倉庫。pluginRepository 下所有子元素的含義均與與 repository 的子元素相同,這里就不再做過多贅述了。

示例 1

將以上配置加入 Maven 項目 Root 的 POM 文件中,并手動清空本地倉庫的所有依賴構(gòu)件(目的是為了讓 Maven 重新獲取構(gòu)件)。

打開命令行窗口,跳轉(zhuǎn)到 Root 所在的目錄下,執(zhí)行以下 mvn 命令。

mvn clean install

執(zhí)行結(jié)果如下。

使用 Nexus 私服構(gòu)建結(jié)果
使用 Nexus 私服構(gòu)建結(jié)果

由圖 1 的構(gòu)建過程可以看出,Root 項目構(gòu)建所需的構(gòu)建是從 Nexus 私服的 banchengbang_central_proxy 代理倉庫中下載的,而不是從 Maven 中央倉庫去下載。

在 setting 中配置

在 pom.xml 中配置 Nexus ,只對當前項目有效,在實際應(yīng)用中,我們往往希望能夠通過一次配置就能讓本機中的所有 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 中包含了與倉庫相關(guān)的配置,同時配置中還使用了一個 activeProfiles 元素將 id 為 nexus 的 profile 激活。當本機有 Maven 項目構(gòu)建時,profile 中的倉庫配置就會應(yīng)用到項目中。

示例 2

將以上配置分別添加到 Maven 安裝目錄\conf 和本地倉庫目錄下的 setting.xml 中,并將 Root 項目 POM 文件中的 repositories 和 pluginRepositories 等倉庫設(shè)置刪除。

打開命令行窗口,跳轉(zhuǎn)到 Root 項目下,執(zhí)行以下 mvn 命令。

mvn clean install

執(zhí)行結(jié)果如下。

Nexus 全局配置倉庫
Nexus 全局配置倉庫

setting.xml 中添加鏡像

Nexus 私服通常會與鏡像(mirror)結(jié)合使用,使 Nexus 成為所有遠程倉庫的私服,這樣不僅可以從 Nexus 中獲取所有所需構(gòu)件,還能將配置集中到 Nexus 私服中,簡化 Maven 本身的配置。 我們可以創(chuàng)建一個匹配任何倉庫的鏡像,鏡像的地址為 Nexus 中倉庫的地址,這樣 Maven 對于任何構(gòu)件的下載請求都會被攔截跳轉(zhuǎn)到 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,即它們的設(shè)置覆蓋了 Maven 中央倉庫,但此時這里的 URL 已經(jīng)無法起到任何作用,因為鏡像的匹配規(guī)則是 *,所有的請求都已經(jīng)被攔截,跳轉(zhuǎn)到 Nexus 私服的地址。

丰满人妻一级特黄a大片,午夜无码免费福利一级,欧美亚洲精品在线,国产婷婷成人久久Av免费高清