使用Nexus搭建Maven私有倉庫

為什么要搭建私有倉庫?


  • 避免所有人都從Maven中央倉庫重復下載所需構件,節省外網帶寬。
  • 當項目開發在內網環境下進行的時候,仍然可以使用Maven。

Nexus倉庫的搭建


準備工作

  • 安裝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. 配置環境變量并使配置生效:
> 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默認的最高權限用戶是admin,默認密碼admin123。

倉庫和倉庫組管理

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

hosted倉庫

Nexus自帶倉庫:

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

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

proxy倉庫

Nexus自帶倉庫:

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

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

virtual倉庫

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

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

倉庫組

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

用戶管理

權限

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

角色

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

用戶

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

在項目中使用私有倉庫


添加私服依賴

在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>

配置私服發布

在pom.xml中配置快照版本和穩定版本的發布地址:

<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中配置要發布到的倉庫的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相比,它更加輕量級,適合中小型的應用。
在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> 

然后在項目根目錄下執行命令:

mvn jetty:run

Cargo

使用Cargo可以將項目部署到已經在運行狀態的容器上。
首先,需要在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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容