maven倉庫管理器Nexus

          Posted on 2011-01-28 22:06 Wesley Xie 閱讀(1111) 評論(0)  編輯  收藏 所屬分類: 工程構建

          訪問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代碼
          <servers>
          <server>
              <id>releases</id>
              <username>admin</username>
              <password>admin123</password>
          </server>
          <server>
              <id>Snapshots</id>
              <username>admin</username>
              <password>admin123</password>
          </server>
          </servers>

          部署第三方構件:
          構件可能是私有數據庫的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。


          本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/summertse/archive/2010/11/23/6030039.aspx


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 鸡泽县| 福泉市| 突泉县| 益阳市| 贵港市| 岳阳市| 同德县| 万年县| 兖州市| 都兰县| 尼勒克县| 信阳市| 钟山县| 石首市| 嵊泗县| 盱眙县| 方正县| 阿瓦提县| 平遥县| 留坝县| 栾川县| 华安县| 门头沟区| 谢通门县| 连州市| 葵青区| 常德市| 疏勒县| 岳阳县| 汉阴县| 拜泉县| 泌阳县| 芒康县| 祁门县| 来凤县| 金平| 望都县| 兴城市| 古田县| 太和县| 湖州市|