本機環境
Win 8 JDK 7 Maven 3.2 Nexus 2.11版本選擇
http://www.sonatype.org/nexus/archivednexus 2.6 及 2.6+ 版本要求 jdk 1.7 或 1.7+ 版本(jdk 1.6 請選擇 nexus 2.5 及 以下版本)。根據你現有的環境來選擇合適的版本。
這里選擇 nexus-2.11.0 (如果無法打開下載, 可將新標簽頁地址欄中的地址復制到迅雷等下載工具中進行下載)。
下載得到 nexus-2.11.0-bundle.zip 壓縮包。解壓縮里面的 nexus-2.11.0-02 目錄,如我的 E:\setup\dev\servers\nexus, 最終得到
的目錄結構為 E:\setup\dev\servers\nexus\nexus-2.11.0-02。
配置 nexus 環境
NEXUS_HOME = E:\setup\dev\servers\nexus\nexus-2.11.0-02Path = .;%NEXUS_HOME%\bin
安裝 nexus 服務
如果你所使用的是 Win8 操作系統,以下操作需要管理員權限。在命令行中執行 nexus install(Win8 系統組合鍵為 Ctrl + X, A):
C:\WINDOWS\system32>nexus install
wrapper | nexus installed.
wrapper | nexus installed.
啟動 nexus
在命令行中執行 nexus start:
C:\WINDOWS\system32>nexus start
wrapper | Starting the nexus service...
wrapper | Waiting to start...
wrapper | Waiting to start...
wrapper | Waiting to start...
wrapper | nexus started.
頁面訪問的入口地址:http://localhost:8081/nexuswrapper | Starting the nexus service...
wrapper | Waiting to start...
wrapper | Waiting to start...
wrapper | Waiting to start...
wrapper | nexus started.

登錄 nexus
默認的管理員賬號是:admin, 密碼是:admin123停止 nexus
在命令行中執行 nexus stop:
C:\Users\fanlychie>nexus stop
wrapper | Stopping the nexus service...
wrapper | Waiting to stop...
wrapper | nexus stopped.
wrapper | Stopping the nexus service...
wrapper | Waiting to stop...
wrapper | nexus stopped.
修改 nexus 賬號密碼
先停止 nexus,打開 %NEXUS_HOME%\sonatype-work\nexus\conf\security.xml,修改即可。
nexus 的密碼采用 SHA1 加密算法 ( 在線加密工具 ),將加密后的 SHA1 串(小寫)拷貝覆蓋原來的。重啟 nexus:nexus restart 。
nexus 的密碼采用 SHA1 加密算法 ( 在線加密工具 ),將加密后的 SHA1 串(小寫)拷貝覆蓋原來的。重啟 nexus:nexus restart 。
<user>
<id>admin</id>
<firstName>Administrator</firstName>
<lastName>User</lastName>
<password>2899441143e28aee7287e8da3d7211258f5edbd3</password>
<status>active</status>
<email>fanlychie@yeah.net</email>
</user>
<id>admin</id>
<firstName>Administrator</firstName>
<lastName>User</lastName>
<password>2899441143e28aee7287e8da3d7211258f5edbd3</password>
<status>active</status>
<email>fanlychie@yeah.net</email>
</user>
修改 nexus 端口
打開 %NEXUS_HOME%\nexus-2.11.0-02\conf\nexus.properties, 修改 application-port 的值。重啟 nexus:nexus restart 。application-port=8081
application-host=0.0.0.0
nexus-webapp=${bundleBasedir}/nexus
nexus-webapp-context-path=/nexus
application-host=0.0.0.0
nexus-webapp=${bundleBasedir}/nexus
nexus-webapp-context-path=/nexus
倉庫類型
打開 Views/Repositories --> Repositories 視圖
nexus 倉庫分為 4 種,group(倉庫組)、hosted(宿主倉庫)、proxy(代理倉庫)、virtual(虛擬倉庫)。
我們自己開發的構件通常是發布到 hosted 倉庫,proxy 用來代理遠程的公共倉庫,一個 group 可以包含多個 hosted/proxy 倉庫。
倉庫組
打開 Views/Repositories --> Repositories --> Public Repositories --> Configuration 視圖
Configuration 欄的左邊是倉庫組倉庫列表,右邊是當前可選的倉庫,可以從右邊挑選合適的倉庫加入到左邊的倉庫組,點擊 Save 保存即可。
一個倉庫組通常包含了多個倉庫,倉庫組中的倉庫列表的順序決定了構件下載時遍歷的倉庫的先后次序,因此,建議將遠程中央倉庫(Central)放
到倉庫組的最后一項。
maven settings.xml 配置
在 settings.xml 配置文件中添加如下配置:<servers>
<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>thirdparty</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
<mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<name>nexus public repositories</name>
<url>http://127.0.0.1:8088/nexus/content/groups/public</url>
</mirror>
</mirrors>
<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>thirdparty</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
<mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<name>nexus public repositories</name>
<url>http://127.0.0.1:8088/nexus/content/groups/public</url>
</mirror>
</mirrors>
<server> 節點配置服務的賬戶密碼,用于發布構件時進行身份和權限的認證。<mirror> 節點用于鏡像的配置,具體用法參考 maven 鏡像配置
發布構件
在 nexus 中找到如下的視圖頁面:
復制以上的配置,粘貼到你的 pom.xml 配置文件中:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.fanlychie</groupId>
<artifactId>proj</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>proj</name>
<url>http://maven.apache.org</url>
<distributionManagement>
<repository>
<id>releases</id>
<url>http://localhost:8088/nexus/content/repositories/releases</url>
</repository>
</distributionManagement>
</project>
這里需要注意的是,repository 節點的 id 需與 settings.xml 中配置的 server 節點的 id 相同,如果不相同,修改任意一方都可以,只xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.fanlychie</groupId>
<artifactId>proj</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>proj</name>
<url>http://maven.apache.org</url>
<distributionManagement>
<repository>
<id>releases</id>
<url>http://localhost:8088/nexus/content/repositories/releases</url>
</repository>
</distributionManagement>
</project>
要讓它們保持一致即可,否則,發布構件的時候會報 401,ReasonPhrase:Unauthorized 的錯誤,原因是無法認證用戶的身份。
右鍵項目,Run As --> Maven build...,在 Goals 欄輸入 deploy 或在命令行(cmd)執行 mvn deploy,在控制臺若能看到
BUILD SUCCESS,表明構件發布成功。你可以在 nexus 的 Releases 倉庫中找到這個構件:

發布構件的時候,如果想把源碼也一起發布出去(執行 mvn dependency:sources 可獲得源碼),配置如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.fanlychie</groupId>
<artifactId>proj</artifactId>
<version>1.0.1</version>
<packaging>jar</packaging>
<name>proj</name>
<url>http://maven.apache.org</url>
<distributionManagement>
<repository>
<id>releases</id>
<url>http://localhost:8088/nexus/content/repositories/releases</url>
</repository>
</distributionManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>install</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
同一個構件是不允許發布兩次的,先修改一下構件的版本,然后 deploy,結果如圖:
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.fanlychie</groupId>
<artifactId>proj</artifactId>
<version>1.0.1</version>
<packaging>jar</packaging>
<name>proj</name>
<url>http://maven.apache.org</url>
<distributionManagement>
<repository>
<id>releases</id>
<url>http://localhost:8088/nexus/content/repositories/releases</url>
</repository>
</distributionManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>install</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

發布快照
快照用于區分穩定與不穩定的構件(發布的版本就是穩定的,快照版本是不穩定的)。構件的升級通常會帶來許多的不穩定性,需要不斷的修復,快照可以避免由于升級帶來的不穩定性迫使的不停的升級版本號,最終造成版本號的泛濫的問題。快照允許重新發布,而不需要變更構件的版本號。
在 nexus 中找到如下的視圖頁面:

復制以上的配置,粘貼到你的 pom.xml 配置文件中:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.fanlychie</groupId>
<artifactId>proj</artifactId>
<version>2.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>proj</name>
<url>http://maven.apache.org</url>
<distributionManagement>
<repository>
<id>releases</id>
<url>http://localhost:8088/nexus/content/repositories/releases</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://localhost:8088/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>install</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
版本號含有 SNAPSHOT 的構件將發布到 Snapshots 倉庫,否則,將發布到 Releases 倉庫。執行發布命令:mvn deploy,結果如圖:
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.fanlychie</groupId>
<artifactId>proj</artifactId>
<version>2.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>proj</name>
<url>http://maven.apache.org</url>
<distributionManagement>
<repository>
<id>releases</id>
<url>http://localhost:8088/nexus/content/repositories/releases</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://localhost:8088/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>install</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

發布第三方構件
步驟見下圖的標注:
命令行方式發布:
mvn deploy:deploy-file -DgroupId=自定義groupId -DartifactId=自定義artifactId -Dversion=版本號 -Dpackaging=jar
-Dfile=JAR文件的路徑 -Durl=http://127.0.0.1:8088/nexus/content/repositories/thirdparty -DrepositoryId=thirdparty
-Dfile=JAR文件的路徑 -Durl=http://127.0.0.1:8088/nexus/content/repositories/thirdparty -DrepositoryId=thirdparty
遠程索引
在 nexus 中找到如下的視圖頁面:
保存之后,在左側面板中選擇 Administration --> Scheduled Tasks,如下圖:

可以看到這里有一個 Repair Repositories Index 任務,任務完成之后會自動從面板中移除,點擊左上角的 Refresh 按鈕來查看。任務完成
之后回到 Repositories 面板,選擇 Central 倉庫右鍵,選擇 Update Index,再回到 Scheduled Tasks 面板,可以看到有一個 Update
Repositories Index 任務,這個任務花時較長,任務完成之后,回到 Repositories 面板,在 Browse Index 中可以看到從遠程倉庫下載回
來的索引文件,有了索引,即使 nexus 私服還沒有你想要構件,你也可以搜索出你想要查找的構件。
