創建Maven私服 - Nexus (七)

私服不是Maven的核心概念,它僅僅是一種衍生出來的特殊的Maven倉庫。經過創建本身的私服,就能夠下降中央倉庫負荷、節省外網帶寬、加速Maven構建、本身部署構建等,從而高效地使用Maven。Nexus也是當前最流行的Maven倉庫管理軟件。 html

1. 安裝Nexus
Nexus是典型的Java Web應用,它有兩種安裝包,一種是包含Jetty容器的Bundle包,另外一種是不包含Web容器的war包。

1)下載Nexus
讀者能夠從官網http://www.sonatype.org/nexus/ 下載最新的Nexus,也能夠到我分享的路徑下載http://download.csdn.net/detail/amuguelove/6578111(居然還要一個積分,服了本身了,沒積分的就忽略這個地址吧)。
瀏覽器

2)Bundle方式安裝Nexus
a. 首先看下解壓後的目錄,結構:
    
nexus-2.6.2-01: 該目錄包含了Nexus運行所須要的文件,如啓動腳本、依賴jar包等。
sonatype-work:該目錄包含Nenus生成的配置文件、日誌文件、倉庫文件等。
其中第一個目錄是運行Nexus必須的,而第二個不是必須的,Nexus會在運行的時候動態建立該目錄。
maven

b. 配置Path,啓動Nexus
首先在環境變量path下加入以下地址:D:\j2ee\nexus-2.6.2-01-bundle\nexus-2.6.2-01\bin;以後在cmd下啓動Nexus服務:

若是看到以上輸出,就說明啓動成功了。這時打開瀏覽器訪問:http://localhost:8081/nexus 就能夠看到Nexus的界面了,以下圖:

網站

這時你能夠單擊界面右上角的Login進行登陸,Nexus默認管理用戶名和密碼爲admin/admin123。 this

2. Nexus的索引
這時你使用Nexus搜索插件得不到任何結果,爲了可以搜索Maven中央庫,首先須要設置Nexus中的Maven Central倉庫下載遠程索引。以下圖:

url

單擊左邊導航欄的Repositories,能夠link到這個頁面,選擇Central,點擊Configuration,裏面有一個Download Remote Indexes配置,默認狀態是false,將其改成true,‘Save’後,單擊Administration==> Scheduled Tasks, 就有一條更新Index的任務,這個是Nexus在後天運行了一個任務來下載中央倉庫的索引。因爲中央倉庫的內容比較多,所以其索引文件比較大,Nexus下載該文件也須要比較長的時間。請讀者耐心等待把。若是網速很差的話,可使用其餘人搭建好的的Nexus私服。後面會介紹。下圖爲Nexus後臺運行的task圖:

spa

3. 配置Maven從Nexus下載構件

1)在POM中配置Nexus私服,這樣的配置只對當前的Maven項目有效。
.net

<repositories>
      <repository>
          <id>nexus</id>
          <name>Nexus Repository</name>
          <url>http://localhost:8081/nexus/content/groups/public/</url>
          <releases>
              <enabled>true</enabled>
          </releases>
          <snapshots>
              <enabled>true</enabled>
          </snapshots>
      </repository>
  </repositories>

2)在settings.xml中配置profile元素,這樣就能讓本機全部的Maven項目都使用本身的Maven私服。
<mirrors>
    <mirror>
      <id>central</id>
      <mirrorOf>*</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://localhost:8081/nexus/content/groups/public/</url>
    </mirror>
  </mirrors>
  <profiles>
    <profile>
      <id>nexus</id>
      <repositories>
        <repository>
          <id>nexus</id>
          <name>Nexus</name>
          <url>http://localhost:8081/nexus/content/groups/public/</url>
            <releases>
			<enabled>true</enabled>
		  </releases>
          <snapshots>
			<enabled>true</enabled>
		  </snapshots>
        </repository>
      </repositories>
    </profile>
</profiles>

以上配置全部Maven下載請求都僅僅經過Nexus,以全面發揮私服的做用。 插件

4. 部署構件到Nexus 日誌

1)在POM中配置

<project>
  ...  
  <distributionManagement>

    <snapshotRepository>
        <id>user-snapshots</id>
        <name>User Project SNAPSHOTS</name>
        <url>http://localhost:8081/nexus/content/repositories/MyUserReposSnapshots/</url>
    </snapshotRepository>
    
      <repository>
          <id>user-releases</id>
          <name>User Project Release</name>
          <url>http://localhost:8081/nexus/content/repositories/MyUserReposRelease/</url>
      </repository>
      
  </distributionManagement>
   ...
</project>
2)settings.xml中配置認證信息,Nexus的倉庫對於匿名用戶是隻讀的。
<servers>
  
    <server>
      <id>user-snapshots</id>
      <username>lb</username>
      <password>123456</password>
    </server>
	
    <server>
      <id>user-releases</id>
      <username>lb</username>
      <password>123456</password>
    </server>
	
  </servers>


最後,若是不想本身構建Nexus私服,或者更新Index很慢的話,可使用OSChina搭建的Nexus私服,地址以下:http://maven.oschina.net/index.html,以下圖:

建議你們能夠在瀏覽器中收藏這個網站,平時也能夠用來查看一下一些構件的依賴,如上圖的右下角顯示的。
相關文章
相關標籤/搜索