maven倉庫管理器Nexus

醉面韋陀發表於2010-08-18

  訪問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.

 <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。

  <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中新增如下的伺服器登入資訊:

    <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:

 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外掛設定。

 
 <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檔案的大概內容如下:

 
<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。

相關文章