Maven私服架設(nexus / on windows)

位元流發表於2014-06-06
Maven私服可以用多個不同的產品可供選擇,下面我們演示使用最為廣泛的nexus來架設maven本地私服
 
Nexus的下載及安裝請見官方下載頁: http://www.sonatype.org/nexus/go
我們可以有兩種安裝方式:1. 獨立伺服器方式執行,nexus內建netty, 2: 下載.war檔案,放到我們已有的容器(比如:tomcat)中執行
 
第一種方式:
我們可以下載.zip檔案,然後直接解壓到bin檔案下找到nexus.bat這個批處理檔案,然後執行> nexus install將nexus安裝成windows服務,然後再services.msc裡設定該伺服器隨作業系統自動啟動,或執行在命令列執行> nexus.bat start也可以啟動這個nexus服務
 
第二種方式:
下載.war檔案(目前版本nexus-2.8.0-05),重新命名為nexus.war,然後放到web容器webapp目錄下執行
 
建議採用第二種方式,將nexus.war放到web容器中執行
 
nexus預設的工作目錄地址為:${user.home}/sonatype-work/nexus,如果我們需要設定成其他目錄請到nexus/WEB_INF/plexus.properties檔案中找到改行並修改
 
一切準備好了之後,輸入http://localhost/nexus, 如果顯示正常表示安裝成功
 
 
點選右上角login,預設賬戶:admin/admin123
 
預設情況下,nexus已經內建建立好了各種我們需要的repositories,這基本上已經夠用了。具體如何使用nexus自己去baidu
 
然後我們需要修改{user.home}/.m2/settings.xml檔案設定本地倉庫
 
首先我們需要在<mirrors>節點中新增一個<mirror>節點配置,用本地倉庫作為外部遠端倉庫的映象,讓maven先從本地倉庫查詢看是否有我們需要的依賴
         
 <mirror>
               <id>nexus</id>
               <url>http://localhost/nexus/content/groups/public/</url>
               <mirrorOf>*</mirrorOf>
          </mirror>

上述配置表示使用本地的public倉庫組作為所有外部倉庫的映象,換句話說,maven在查詢依賴的時候首先從本地找,找不到才去外部倉庫找,需要注意的是,即使我們這裡配置了本地倉庫作為外部所有倉庫的依賴,但是此時maven仍然還會去連線central(中央倉庫),如果想要徹底阻止maven訪問遠端central倉庫,我們還需要做如下配置:

 
在<profiles>節點下新增一個<profile>節點,並且在<activeProfiles>中啟用這個profile
<profile>
               <id>nexus</id>
               <repositories>
                    <repository>
                         <id>central</id>
                         <url>http://central</url>
                         <releases>
                              <enabled>true</enabled>
                         </releases>
                         <snapshots>
                              <enabled>true</enabled>
                         </snapshots>
                    </repository>
               </repositories>
               <pluginRepositories>
                    <pluginRepository>
                         <id>central</id>
                         <url>http://central</url>
                         <releases>
                              <enabled>true</enabled>
                         </releases>
                         <snapshots>
                              <enabled>true</enabled>
                         </snapshots>
                    </pluginRepository>
               </pluginRepositories>
          </profile>
     </profiles>
     <activeProfiles>
          <activeProfile>nexus</activeProfile>
     </activeProfiles>

這樣maven就可以訪問本地倉庫來替代訪問外部倉庫了

 
如何將專案部署到本地倉庫

在pom.xml檔案中新增以下配置節點
<distributionManagement>
             <repository>
                   <id> nexus-releases</id >
                   <name> Local nexus releases repository</name >
                   <url> http://localhost/nexus/content/repositories/releases/ </url>
             </repository>
             <snapshotRepository>
                   <id> nexus-snapshots</id >
                   <name> Local nexus snapshots repository</name >
                   <url> http://localhost/nexus/content/repositories/snapshots/ </url>
             </snapshotRepository>
       </distributionManagement>

說明:上面的配置檔案中指定的是將打包檔案部署到本地的一個倉庫,對外部或本地倉庫的訪問一般都需要配置許可權,所以還需要在settings.xml檔案中配置訪問許可權

<servers>
          <server>
               <id>nexus-releases</id>
               <username>admin</username>
               <password>admin123</password>
          </server>
          <server>
               <id>nexus-snapshots</id>
               <username>admin</username>
               <password>admin123</password>
          </server>
     </servers>

上面的<server>.<id>節點中的名稱需要和<repository>的<id>中定義的名稱保持一致

相關文章