Mac 安裝配置nexus2.6 搭建Maven的中央倉庫


今天配置java 環境,安裝nexus 百度了好久才安裝好,所以特別寫下來 分享給同樣遇到問題的你。
廢話不多說,直接上步驟

前置條件 :已經安裝了JDK

  1. 下載nexus(http://www.sonatype.com/download-oss-sonatype) 最新版本3.0

 但是這個網站在國內有時候無法訪問,所以我給大家提供一個百度雲的地址:

https://pan.baidu.com/s/1bRvLYQ

2.配置環境變量

首先編輯這個文件  vi ~/.bash_profile

然后增加下面的一行

export PATH=${PATH}:/Users/xiaoyuwang/java/resources/nexus-2.6.0-bundle/nexus-2.6.0-05/bin

 

變成如下

 

然后保存並退出,鍵入命令:wq

然后用source ~/.bash_profile讓文件即時生效

3、啟動nexus

進入nexus-2.2-01/bin/jsw/目錄,然后根據OS的版本進入相應的目錄,在linux下,運行./nexus start即啟動了服務,直接運行./nexus會看到提示信息,運行./nexus console 可以以控制台方式啟動nexus,方便我們觀察啟動時的相關工作。neuxs默認監聽端口是8081,此時在瀏覽器中運行訪問http://127.0.0.1:8081/nexus或者http://localhost:8081/nexus或者http://0.0.0.0:8081/nexus應該可以看到neuxs的界面。

如果你不會,你可以只鍵入nexus,系統會給出命令使用說明

Usage: /Users/xiaoyuwang/java/resources/nexus-2.6.0-bundle/nexus-2.6.0-05/bin/nexus { console | start | stop | restart | status | dump }

 

新搭建的neuxs環境只是一個空的倉庫,需要手動和遠程中心庫進行同步,nexus默認是關閉遠程索引下載,最重要的一件事情就是開啟遠程索引下載。登陸nexus系統,默認用戶名密碼為admin/admin123。 點擊左邊Views/Repositories菜單下面的Repositories,找到右邊倉庫列表中的三個倉庫Apache Snapshots,Codehaus Snapshots和Central,然后再沒有倉庫的Configuration下把Download Remote Indexes修改為true。然后在這三個倉庫上分別右鍵,選擇Repari Index,這樣Nexus就會去下載遠程的索引文件。

    新建公司的內部倉庫,步驟為Repositories –> Add –> Hosted Repository,在頁面的下半部分輸入框中填入Repository ID和Repository Name即可,比如分別填入myrepo和 my repository,另外把Deployment Policy設置為Allow Redeploy,點擊save就創建完成了。

    修改nexus倉庫組

    Nexus中倉庫組的概念是Maven沒有的,在Maven看來,不管你是hosted也好,proxy也好,或者group也好,對我都是一樣的,我只管根據groupId,artifactId,version等信息向你要構件。為了方便Maven的配置,Nexus能夠將多個倉庫,hosted或者proxy合並成一個group,這樣,Maven只需要依賴於一個group,便能使用所有該group包含的倉庫的內容。

    neuxs-2.2中默認自帶了一個名為“Public Repositories”組,點擊該組可以對他保護的倉庫進行調整,把剛才建立的公司內部倉庫加入其中,這樣就不需要再在maven中明確指定內部倉庫的地址了。同時創建一個Group ID為public-snapshots、Group Name為Public Snapshots Repositories的組,把Apache Snapshots、Codehaus Snapshots和Snapshots加入其中。

    到這里neuxs的安裝配置就完成了,下面介紹如何在mavne中使用自己的私服。

    maven安裝好默認是沒有配置倉庫信息,此時mavne都默認去遠程的中心倉庫下載所依賴的構件。既然使用了私服,就需要明確告訴maven去哪里下載構件,可以在三個地方進行配置,分別是是mavne的安裝目錄下的conf下的settings.xml文件,這個全局配置,再一個是在userprofile/.m2目錄下的settings.xml,如果不存在該文件,可以復制conf下settings.xml,這個是針對當前用戶的,還有一個是在pom.xml中指定,其只對該項目有效,三個文件的優先級是pom.xml大於.m2,.m2大於conf下。

    settings.xml的配置如下:

    在profiles段增加一個profile

 
<profile>
      <id>nexus</id>
      <repositories>
        <repository>
            <id>nexus</id>
            <name>local private nexus</name>
            <url>http://192.168.1.68:8081/nexus/content/groups/public</url>
            <releases><enabled>true</enabled></releases>
            <snapshots><enabled>false</enabled></snapshots>
        </repository>
        <repository>
            <id>nexus-snapshots</id>
            <name>local private nexus</name>
            <url>http://192.168.1.68:8081/nexus/content/groups/public-snapshots</url>
            <releases><enabled>false</enabled></releases>
            <snapshots><enabled>true</enabled></snapshots>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
            <id>nexus</id>
            <name>local private nexus</name>
            <url>http://192.168.1.68:8081/nexus/content/groups/public</url>
            <releases><enabled>true</enabled></releases>
            <snapshots><enabled>false</enabled></snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>nexus-snapshots</id>
            <name>local private nexus</name>
            <url>http://192.168.1.68:8081/nexus/content/groups/public-snapshots</url>
            <releases><enabled>false</enabled></releases>
            <snapshots><enabled>true</enabled></snapshots>
        </pluginRepository>
       </pluginRepositories>
    </profile>
 

 

    上述的id,name可以根據自己的喜好來定義,保證多個repository的id不重復即可,主要是url的配置,可以在nexus的repositories列表中找到對應的url,就是repositories的Repository Path,剛才我們已經在neuxs中定於了倉庫組,這里就取對應組的Repository Path信息即可。

    另外,倉庫是兩種主要構件的家。第一種構件被用作其它構件的依賴。這是中央倉庫中存儲的大部分構件類型。另外一種構件類型是插件。如果不配置pluginRepositories,那么執行maven動作時,還是會看到去遠程中心庫下載需要的插件構件,所以這里一定要加上這個。

    在settings.xml中指使配置了profile還不行,需要激活它,在activeProfiles段,增加如下配置:

<activeProfile>nexus</activeProfile>

 

    此處activeProfile中的內容就上面定義profile的id。
    pom.xml配置如下:

<repositories>
        <repository>
            <id>nexus</id>
            <name>local private nexus</name>
            <url>http://192.168.1.68:8081/nexus/content/groups/public</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>nexus-snapshots</id>
            <name>local private nexus</name>
            <url>http://192.168.1.68:8081/nexus/content/groups/public-snapshots</url>
            <releases>
                <enabled>false</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>nexus</id>
            <name>local private nexus</name>
            <url>http://192.168.1.68:8081/nexus/content/groups/public</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>nexus-snapshots</id>
            <name>local private nexus</name>
            <url>http://192.168.1.68:8081/nexus/content/groups/public-snapshots</url>
            <releases>
                <enabled>false</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

  

 

    其中id,name都無所謂,關鍵不能把url弄錯。

    有了上述配置,當在項目執行maven操作時,如果本地庫中沒有依賴的構件,maven會去私服下載,如果私服也沒有,私服會去遠程中心庫下載,同時會在私服的本地緩存,這樣如果第二個人再要用到這個構件,私服就直接提供下載。

 參考資料:

http://blog.csdn.net/sxdtzhaoxinguo/article/details/46895013


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM