使用Nexus搭建Maven私有倉庫

為什么要搭建私有倉庫?


  • 避免所有人都從Maven中央倉庫重復(fù)下載所需構(gòu)件,節(jié)省外網(wǎng)帶寬。
  • 當(dāng)項目開發(fā)在內(nèi)網(wǎng)環(huán)境下進行的時候,仍然可以使用Maven。

Nexus倉庫的搭建


準(zhǔn)備工作

  • 安裝CentOS 7.0
  • 安裝JDK
  • 安裝Maven
  • 下載Nexus

安裝和啟動Nexus

  1. 解壓Nexus,將解壓后的目錄名去掉版本號:
> tar -xzf nexus-2.14.4-bundle.tar.gz 
> mv nexus-2.14.4 nexus
  1. 配置環(huán)境變量并使配置生效:
> echo "export NEXUS_HOME=\$HOME/nexus" >> /etc/profile
> echo "export PATH=\$PATH:\$NEXUS_HOME/bin" >> /etc/profile
> source /etc/profile
  1. 啟動Nexus:
> nexus start
> firefox 127.0.0.1:8081/nexus
  1. 啟動Nexus之后,可以在瀏覽器界面中進行倉庫和用戶的管理。Nexus默認(rèn)的最高權(quán)限用戶是admin,默認(rèn)密碼admin123。

倉庫和倉庫組管理

倉庫的類型(type)主要可分為hosted、proxy、virtual三種類型。hosted倉庫是由倉庫管理者在私服本地創(chuàng)建的倉庫,proxy倉庫是遠程倉庫的代理。virtual倉庫可以理解為不同種類(format)倉庫之間的適配器。

hosted倉庫

Nexus自帶倉庫:

Repository Name Repository Policy Deployment Policy Remark
3rd Party Release Disable Redeploy 通常用于添加Maven倉庫中沒有的第三方依賴
Releases Release Allow Redeploy 用于項目組內(nèi)部的穩(wěn)定版本發(fā)布
Snapshots Snapshot Allow Redeploy 用于項目組內(nèi)部的快照版本發(fā)布

另外,也可以自建hosted倉庫。

proxy倉庫

Nexus自帶倉庫:

Repository Name Repository Policy Remark
Apache Snapshots Release Apache軟件基金會發(fā)布的快照版本組件
Codehaus Snapshots Release Codehaus發(fā)布的快照版本組件
Central Snapshot Maven中央倉庫中發(fā)布的組件

另外,也可以自建proxy倉庫,例如阿里云倉庫的代理等等。

virtual倉庫

Nexus自帶一個名為Central M1,將原本的format為Maven2的Central倉庫配置為format為Maven1的影子倉庫。它在配置時有一個provider屬性,可以選擇:

  • Maven2轉(zhuǎn)化Maven1
  • Maven1轉(zhuǎn)化Maven2
  • 轉(zhuǎn)化為NuGet(.NET平臺的包管理工具)

倉庫組

倉庫組可以將多個倉庫合并為一組,使用時作為單獨的組件來引用??梢栽赨I界面中配置同組各個倉庫的訪問順序。

用戶管理

權(quán)限

Nexus默認(rèn)創(chuàng)建了很多權(quán)限,例如UI:Basic UI Privileges包含了訪問Nexus界面必須的最基本的權(quán)限,Repo:All Repositories (Read)給予用戶讀取所有倉庫內(nèi)容的權(quán)限。沒有倉庫的讀權(quán)限用戶將無法再倉庫頁面看到實際的倉庫內(nèi)容,也無法使用Maven從倉庫下載構(gòu)件等等,我們也可以自己創(chuàng)建對某個具體倉庫增刪改查的權(quán)限。

角色

Nexus默認(rèn)創(chuàng)建了很多角色,每個角色擁有一個或多個權(quán)限,不同的角色權(quán)限不同。例如Nexus Administrator擁有Nexus的所有權(quán)限,Deployment只能夠訪問Nexus,瀏覽倉庫內(nèi)容、搜索、上傳部署構(gòu)件,但是不能對Nexus進行任何配置等等,我們也可以通過組合不同權(quán)限,創(chuàng)建自定義的角色。

用戶

Nexus默認(rèn)創(chuàng)建了一些用戶,每個用戶擁有一個或多個角色。我們也可以通過組合不同角色,創(chuàng)建新的用戶。

在項目中使用私有倉庫


添加私服依賴

在pom.xml文件中加入私服的地址:

<repositories>
        <repository>
            <id>MavenPrivateServer</id>
            <name>MavenPrivateServer</name>
            <url>${repositoriesIp}</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <layout>default</layout>
        </repository>
    </repositories>

配置私服發(fā)布

在pom.xml中配置快照版本和穩(wěn)定版本的發(fā)布地址:

<distributionManagement>
        <snapshotRepository>
            <!--<id>testSSH_snopshot</id>-->
            <id>test_hosted_snapshot</id>
            <!--<name>User Project SNAPSHOTS</name>-->
            <name>test_hosted_snapshot</name>
            <url>http://${commonServerIp}:${commonServerPort}/nexus/content/repositories/test_hosted_snapshot</url>
        </snapshotRepository>

        <repository>
            <!--<id>testSSH_snopshot</id>-->
            <id>test_hosted_releases</id>
            <!--<name>User Project SNAPSHOTS</name>-->
            <name>test_hosted_releases</name>
            <url>http://${commonServerIp}:${commonServerPort}/nexus/content/repositories/test_hosted_releases/</url>
        </repository>
    </distributionManagement>

在settings.xml中配置要發(fā)布到的倉庫的id和Nexus用戶名和密碼:

 <server>
      <id>test_hosted_snapshot</id>
      <username>user</username>
      <password>passwd</password>
    </server>

插件的管理

在pom.xml中配置插件:

<build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.cargo</groupId>
                <artifactId>cargo-maven2-plugin</artifactId>
                <version>${cargo.version}</version>

                <configuration>
                    <!-- Container configuration -->
                    <container>
                        <containerId>tomcat8x</containerId>
                        <type>remote</type>
                    </container>

                    <configuration>
                        <type>runtime</type>
                        <properties>
                            <cargo.tomcat.manager.url>http://${commonServerIp}:${remoteTomcatPort}/manager/</cargo.tomcat.manager.url>
                            <cargo.remote.username>tomcat</cargo.remote.username>
                            <cargo.remote.password>tomcat</cargo.remote.password>
                            <cargo.servlet.port>${remoteTomcatPort}</cargo.servlet.port>
                            <cargo.hostname>${commonServerIp}</cargo.hostname>
                            <cargo.tomcat.ajp.port>8009</cargo.tomcat.ajp.port>
                        </properties>
                    </configuration>
                </configuration>

            </plugin>
        </plugins>

其它


Jetty

和Tomcat類似,Jetty也是一個開源的Servlet容器,和Tomcat相比,它更加輕量級,適合中小型的應(yīng)用。
在Maven項目中使用Jetty,首先要增加jetty-maven-plugin到你的pom.xml:

<plugin>  
  <groupId>org.eclipse.jetty</groupId>  
  <artifactId>jetty-maven-plugin</artifactId>  
  <configuration>
    <!-- 配置端口 -->
    <httpConnector>
           <port>8787</port>
           <idleTimeout>300000</idleTimeout>
    </httpConnector>
       <webApp>
          <contextPath>/${project.artifactId}
 </contextPath>
       </webApp>
   <configuration>
</plugin> 

然后在項目根目錄下執(zhí)行命令:

mvn jetty:run

Cargo

使用Cargo可以將項目部署到已經(jīng)在運行狀態(tài)的容器上。
首先,需要在settings.xml中增加server:

 <server>
      <id>tomcat7</id>
      <username>user</username>
      <password>passwd</password>
</server>

然后,在pom.xml中加入:

<build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.cargo</groupId>
                <artifactId>cargo-maven2-plugin</artifactId>
                <version>${cargo.version}</version>

                <configuration>
                    <!-- Container configuration -->
                    <container>
                        <containerId>tomcat8x</containerId>
                        <type>remote</type>
                    </container>

                    <configuration>
                        <type>runtime</type>
                        <properties>
                            <cargo.tomcat.manager.url>http://${commonServerIp}:${remoteTomcatPort}/manager/</cargo.tomcat.manager.url>
                            <cargo.remote.username>tomcat</cargo.remote.username>
                            <cargo.remote.password>tomcat</cargo.remote.password>
                            <cargo.servlet.port>${remoteTomcatPort}</cargo.servlet.port>
                            <cargo.hostname>${commonServerIp}</cargo.hostname>
                            <cargo.tomcat.ajp.port>8009</cargo.tomcat.ajp.port>
                        </properties>
                    </configuration>
                </configuration>
            </plugin>
        </plugins>
</build>

最后,通過命令啟動Cargo并部署項目:

mvn cargo:run
mvn cargo:deploy
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容