Maven Nexus私服搭建

Maven倉庫彙總

1、maven 倉庫地址:

公有的倉庫
http://repo1.maven.org/maven2/
http://repository.jboss.com/maven2/
http://repository.sonatype.org/content/groups/public/
http://mirrors.ibiblio.org/pub/mirrors/maven2/org/acegisecurity/

私有的倉庫
http://repository.codehaus.org/
http://snapshots.repository.codehaus.org/
http://people.apache.org/repo/m2-snapshot-repository
http://people.apache.org/repo/m2-incubating-repository/

 

2、收集的外部倉庫地址

 
Maven倉庫的搭建
訪問http://nexus.sonatype.org/downloads/下載Nexus。 
    啓動Nexus,就是啓動一個web服務器,它的默認地址是localhost:8081。Nexus在一個名爲Jetty的servlet容器中運行,它使用一個名爲Tanuki Java Service Wrapper的本地服務包裹器啓動。這個服務包裹器可以被配置成以Windows服務或Unix守護線程的形式運行Nexus。要啓動Nexus,你需要爲你的平臺找到合適的啓動腳本。要查看可用平臺的列表,查看${NEXUS_HOME}/bin/jsw目錄的內容。
    可執行文件在%nexus安裝目錄%\nexus-webapp-1.0.0\binjsw\windows-x86-32下: 
    InstallNexus.bat/UninstallNexus.bat是安裝/卸載nexus爲windows service。 
    Nexus.bat是直接在命令行中啓動Nexus,如果不想安裝Nexus爲windows service,可以用這個文件來手工控制Nexus的啓動退出。 
1.配置nexus 
    首先登錄,默認地址http://localhost:8081/nexus/,默認用戶名密碼爲admin/admin123. 
    nexus默認是關閉遠程索引下載功能的。開啓的方式: 
    點擊Administration菜單下面的Repositories,將這三個倉庫Apache Snapshots,Codehaus Snapshots,Maven Central的 
    Download Remote Indexes修改爲true。然後在這三個倉庫上分別右鍵,選擇Re-index,這樣Nexus就會去下載遠程的索引文件。 
2.管理倉庫 
以管理員用戶登陸然後點擊左邊導航菜單Administration下面的Repositories。Nexus提供了三種不同的倉庫。 
(1)代理倉庫 
一個代理倉庫是對遠程倉庫的一個代理。默認情況下,Nexus自帶了如下配置的代理倉庫: 
Apache Snapshots 
這個倉庫包含了來自於Apache軟件基金會的快照版本。http://people.apache.org/repo/m2-snapshot-repository 
Codehaus Snapshots 
這個倉庫包含了來自於Codehaus的快照版本。 http://snapshots.repository.codehaus.org/ 
Central Maven Repository 
這是中央Maven倉庫(發佈版本)。 http://repo1.maven.org/maven2/ 
(2)宿主倉庫 
一個宿主倉庫是由Nexus託管的倉庫。Maven自帶了如下配置的宿主倉庫。 
3rd Party 
這個宿主倉庫應該用來存儲在公共Maven倉庫中找不到的第三方依賴。這種依賴的樣例有:你組織使用的,商業的,私有的類庫如Oracle JDBC驅動。 
Releases 
這個宿主倉庫是你組織公佈內部發布版本的地方。 
Snapshots 
這個宿主倉庫是你組織發佈內部快照版本的地方。 
(3)虛擬倉庫 
一個虛擬倉庫作爲Maven 1的適配器存在。Nexus自帶了一個central-m1虛擬倉庫 
3. 管理組 
組是Nexus一個強大的特性,它允許你在一個單獨的URL中組合多個倉庫。Nexus自帶了兩個組:public和public-snapshots。public組中組合了三個宿主倉庫:3rd Party, Releases, 和Snapshots,還有中央Maven倉庫。而public-snapshots組中組合了Apache Snapshots和Codehaus Snapshots倉庫。 
4. 配置maven 
要讓maven使用Nexus作爲倉庫,要修改~/.m2/settings.xml. 
Xml代碼 
<profiles> 
   <profile> 
     <id>nexus</id> 
     <repositories> 
       <repository> 
           <id>nexus</id> 
           <name>local private nexus</name> 
           <url>http://localhost:8081/nexus/content/groups/public</url> 
       </repository> 
     </repositories> 
   </profile> 
   <profile> 
     <id>nexus-snapshots</id> 
     <repositories> 
       <repository> 
           <id>nexus-snapshots</id> 
           <name>local private nexus snapshots</name> 
           <url>http://localhost:8081/nexus/content/groups/public-snapshots</url> 
       </repository> 
     </repositories> 
   </profile> 
</profiles>
<activeProfiles> 
    <activeProfile>nexus</activeProfile> 
    <activeProfile>nexus-snapshots</activeProfile> 
</activeProfiles>
5.部署構件至Nexus 
    要部署構件至Nexus,在distributionManagement中提供倉庫URL,然後運行mvn deploy。Maven會通過一個簡單的HTTP PUT將項目POM和構件推入至你的Nexus安裝。需要配置你項目POM中distributionManagement部分的repository。 
Xml代碼 
<distributionManagement> 
<repository> 
    <id>releases</id> 
    <name>Internal Releases</name> 
    <url>http://localhost:8081/nexus/content/repositories/releases</url> 
</repository> 
<snapshotRepository> 
    <id>Snapshots</id> 
    <name>Internal Snapshots</name> 
    <url>http://localhost:8081/nexus/content/repositories/snapshots</url> 
</snapshotRepository> 
</distributionManagement>
這樣還沒完,這時如果部署會報錯,還要在~/.m2/settings.xml中添加如下的服務器登錄信息: 
Xml代碼 
<server> 
<id>releases</id> 
<username>admin</username> 
<password>admin123</password> 
</server> 
<server> 
<id>Snapshots</id> 
<username>admin</username> 
<password>admin123</password> 
</server>
部署第三方構件: 
構件可能是私有數據庫的JDBC驅動如Oracle,或者你依賴於另一個JAR,它既不開源也無法免費獲得。在這樣的情況下,你就需要手動拿來這些構件然後發佈到你自己的倉庫中。Nexus提供宿主的"third-party"倉庫,就是爲了這個目的。 
使用以下命令發佈該文件至Nexus: 
Java代碼 
mvn deploy:deploy-file -DgroupId=com.oracle -DartifactId=ojdbc14 
-Dversion=10.2.0.3.0 -Dpackaging=jar -Dfile=ojdbc.jar   
-Durl=http://localhost:8081/nexus/content/repositories/thirdparty   
-DrepositoryId=thirdparty
6.Nexus監聽端口 
默認情況下,Nexus監聽端口8081。你可以更改這個端口,通過更改${NEXUS_HOME}/conf/plexus.properties的值,爲此,停止Nexus,更改文件中applicationPort的值,然後重啓Nexus。 
7.Maven Profiles 
   Maven中的profile是一組可選的配置,可以用來設置或者覆蓋配置默認值。有了profile,你就可以爲不同的環境定製構建。profile可以在pom.xml中配置,並給定一個id。然後你就可以在運行Maven的時候使用的命令行標記告訴Maven運行特定profile中的目標。以下pom.xml使用production profile覆蓋了默認的Compiler插件設置。 
Xml代碼 
<profiles> 
   <profile> 
     <id>production</id> 
     <build> 
       <plugins> 
         <plugin> 
           <groupId>org.apache.maven.plugins</groupId> 
           <artifactId>maven-compiler-plugin</artifactId> 
           <configuration> 
             <debug>false</debug> 
             <optimize>true</optimize> 
           </configuration> 
         </plugin> 
       </plugins> 
     </build> 
   </profile> 
</profiles> 
要使用production profile來運行mvn install,你需要在命令行傳入-Pproduction參數。要驗證production profile覆蓋了默認的Compiler插件配置,可以像這樣以開啓調試輸出(-X) 的方式運行Maven。 
    如果你開始大量使用Maven profile,你會希望將profile從POM中分離,使用一個單獨的文件如profiles.xml。你可以混合使用定義在pom.xml中和外部profiles.xml文件中的profile。只需要將profiles元素放到${basedir}目錄下的profiles.xml文件中,然後照常運行Maven就可以。profiles.xml文件的大概內容如下: 
Xml代碼 
<profiles> 
    <profile> 
      <id>development</id> 
      <build> 
        <plugins> 
          <plugin> 
            <groupId>org.apache.maven.plugins</groupId> 
            <artifactId>maven-compiler-plugin</artifactId> 
            <configuration> 
              <debug>true</debug> 
              <optimize>false</optimize> 
            </configuration> 
          </plugin> 
        </plugins> 
      </build> 
    </profile> 
    <profile> 
      <id>production</id> 
      <build> 
        <plugins> 
          <plugin> 
            <groupId>org.apache.maven.plugins</groupId> 
            <artifactId>maven-compiler-plugin</artifactId> 
            <configuration> 
              <debug>false</debug> 
              <optimize>true</optimize> 
            </configuration> 
          </plugin> 
        </plugins> 
      </build> 
    </profile> 
</profiles>
settings profile可以應用到所有你使用Maven構建的項目。你可以在兩個地方定義settings profile:定義在~/.m2/settings.xml中的用戶特定settings profile,或者定義在${M2_HOME}/conf/settings.xml中的全局settings profile。

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