Maven學習 (五) 使用Nexus搭建Maven私服(LINUX)

、安裝運行nexus

1、下載nexushttp://www.sonatype.org/nexus/go

選擇tgzzip格式以及war,選擇tgzzip不同版本可能啓動時存在一定問題,可能因爲jdk版本問題若無法啓動請選擇2.5更早的版本

注:nexus 2.6版本之後不再支持jdk1.6

2、安裝nexus

若下載war,則將其放置tomcat下的webapp目錄中,改名nexus,運行tomcat服務,即可訪問http://localhost:8081/nexus  默認用戶名:admin;密碼admin123

若下載tgz或者zip將其解壓至usr/local/目錄下(也可自己指定目錄),root/Download目錄下:

cp nexus-2.2-01-bundle.zip /usr/local

unzip nexus-2.2-01-bundle.zip

或者

cp nexus-2.2-01-bundle.tar.gz /usr/local

tar xvzf nexus-2.2-01-bundle.tar.gz

爲了方便更新和切換版本,創建鏈接,更新或者切換版本只需更新鏈接即可:

ln -s nexus-2.2-01 nexus

3運行nexus

沒有安裝爲service的情況下運行nexus

若爲較新版本

cd /usr/local/nexus

./bin/nexus start

啓動成功nexus監聽8081端口,在日誌可進行查看

tail -f logs/wrapper.log

較早的版本需要進入平臺目錄下進行啓動(較新版本也可以進行啓動

cd /usr/local/nexus/

./bin/jsw/linux-x84-64/nexus start

瀏覽器中輸入http://localhost:8081/nexus即可進入

4安裝nexusservice

root用戶身份,設置爲系統服務 

  cd /etc/init.d 

cp /usr/local/nexus/bin /nexus ./nexus 

chmod 755 /etc/init.d/nexus

Red Hat, Fedora, and CentOS增加nexus服務

$ cd /etc/init.d

增加nexus服務

$ chkconfig --add nexus

添加運行級別345

$ chkconfig --levels 345 nexus on

啓動nexus

$ service nexus start

驗證關係成功啓動

$ tail -f /usr/local/nexus/logs/wrapper.log

編輯/etc/init.d/nexus的文本

NEXUS_HOME="/usr/local/nexus"

PIDDIR="${NEXUS_HOME}"  

RUN_AS_USER "nexus"   此處用戶名可隨意選擇,最好不要是root

①如果使用root創建其它用戶,則改爲root(不建議使用root

②如果使用該設置,需要:

•  nexus 用戶存在

useradd  nexus

• 更改nexus 安裝目錄的OwnerGroupnexus

chown nexus nexus nexus

啓動服務 

service nexus start

二、配置maven使用nexus

這裏先對Maven的安裝作一簡單介紹:

下載maven安裝包,apache-maven-3.1.0-bin.tar.gz,並解壓

cp apache-maven-3.1.1-bin.tar.gz /usr/local/

tar –zxvf apache-maven-3.1.1-bin.tar.gz

ln –s apache-maven-3.1.1 maven

編輯/etc/profile文件,添加如下代碼

export MAVEN_HOME=/opt/apache-maven-3.2.1

export PATH=$JAVA_HOME/bin:$MAVEN_HOME/bin:$PATH

運行以下命令檢查maven安裝:

  顯示路徑

echo $MAVEN_HOME

  顯示信息即成功

mvn -v

接下來要使用Nexus,則需要配置Maven去檢查Nexus而非公共的倉庫。

創建~/.m2/settings.xml(初始狀態文件不存在)編輯內容:

<settings>

  <profiles>

    <profile>

      <id>nexus</id>

      <repositories>

        <repository>

          <id>central-snapshots</id>

          <url>http://localhost:8081/nexus/content/groups/public-snapshots</url>

          <releases><enabled>false</enabled></releases>

          <snapshots><enabled>true</enabled></snapshots>

        </repository>

        <repository>

          <id>central-releases</id>

<!-- url原爲http://central,指向http://repos.d.xxx.com/nexus/content/groups/public   其中xxx一般爲公司名--->

          <url>http://localhost:8081/nexus/content/groups/public</url>

<!-- 表示可以從此倉庫下載release版本 -->

<releases><enabled>true</enabled></releases>

<!-- 表示不可以從此倉庫下載snapshot版本 -->

<snapshots><enabled>false</enabled></snapshots>

        </repository>

      </repositories>

<!--  插件倉庫 -->

      <pluginRepositories>

        <pluginRepository>

          <id>central-snapshots</id>

          <url>http://localhost:8081/nexus/content/groups/public-snapshots</url>

          <releases><enabled>false</enabled></releases>

          <snapshots><enabled>true</enabled></snapshots>

        </pluginRepository>

        <pluginRepository>

          <id>central-releases</id>

          <url>http://localhost:8081/nexus/content/groups/public</url>

          <releases><enabled>true</enabled></releases>

          <snapshots><enabled>false</enabled></snapshots>

        </pluginRepository>

      </pluginRepositories>

<servers>  

  <server> 

<!--配置所有的releases版本構件部署Nexusreleases倉庫中--> 

    <id>nexus-releases</id>  

    <username>admin</username>  

    <password>admin123</password>  

  </server>  

  <server>  

<!--配置所有的snapshot版本構件部署到NexusSnapshots倉庫中--> 

    <id>nexus-snapshots</id>  

    <username>admin</username>  

    <password>admin123</password>  

  </server>    

</servers>  

  </profile>

<!--  配置activeProfiles後,此處添加snapshots,可以使snapshots被搜索到。-->

  </profiles>

<!--添加id,激活profile-->

  <activeProfiles>

    <activeProfile>nexus</activeProfile>

  </activeProfiles>

</settings>

nexus配置成所有倉庫的鏡像之後,maven會從本地的nexus安裝查閱,而非去外面查閱中央maven倉庫。如果對nexus有一個構件請求,本地的nexus安裝會提供這個構件;如果Nexus沒有這個構件,nexus會從遠程倉庫獲取這個構件,然後添加至遠程倉庫的本地鏡像。

三、配置nexus倉庫

Nexus有許多默認倉庫:CentralReleasesSnapshots,和3rd Party

1.配置central倉庫

 Nexus內置了Maven中央代理倉庫Central選擇倉庫列表中的 Central如圖3.1

3.1

點擊Configuration進行配置

Repository Policyrelease不會代理遠程倉庫的snapshot構件snapshot版本構件不穩定且不受控制,使用這樣的構件含有潛在的風險

“Remote Storage Location”爲遠程倉庫地址,http://repo.maven.org/maven2/ 

“Download Remote Indexes”是否下載遠程索引文件,默認爲False,(中央倉庫有大量的構件,其索引文件也很大,容易消耗大量的 中央倉庫帶寬)。將其設置爲True(其他代理倉庫也需要改成True,然後點擊Save。在Nexus下載的中央倉庫索引文件之後,我們就可以在本地搜索中央倉庫的所有構件。

2.Releases用於部署自己的release構件,Snapshots用於自己的snapshot構件3rd Party用於部署第三方構件,修改ConfigurationDeployment PolicyAllow Redeploy,則可以在Artifact Upload中上傳本地文件。

 

新增代理倉庫

1.新增代理倉庫。如圖3.2

3.2 nexus新增代理倉庫

 

2.添加信息具體。如圖3.3

 

3.3 添加倉庫信息

3.添加倉庫至組

添加倉庫到公共nexus倉庫組。如圖3.4

        Ordered Group Repositories:已在使用中的倉庫。檢索順序與排序一致

    Available Repositories:已有未被使用

如圖3.4

四、用戶使用

1.在開發環境中替換/rhome/用戶名/.m2 目錄下的文件settings.xml(新環境中可能沒有此文件)

2.修改maven安裝目錄下/conf/settings.xml文件。修改成乾淨的文件

<localRepository>/workspace/repository</localRepository>此標籤可以修改本地倉庫路徑,也可以在eclipseUser Settings中設置。

3.在【工程名】下面執行 mvn  clean  install,即從私服裏面下載pom文件裏的 jar 包;

4.eclipse開發工具中  clean 【工程名】,刷新jar包目錄即可;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章