Maven私服Nexus的搭建
私服存在的合理性
Maven中的依賴是從服務器倉庫中下載的,Maven的倉庫只有兩大類:
- 1) 本地倉庫
- 2) 遠程倉庫,其中在遠程倉庫中又分成了3種:中央倉庫 、私服、其它公共庫。
Maven用戶直接連接遠程倉庫下載構件的做法是Maven不建議使用的(尤其是對一個開發團隊來說),Maven的最佳實踐就是使用Maven私服來構建整個團隊的項目部署和管理。
私服是一種特殊的遠程倉庫,它是架設在局域網內的倉庫服務,私服代理廣域網上的遠程倉庫,供局域網內的Maven用戶使用。當Maven需要下載構件的時候,它從私服請求,如果私服上不存在該構件,則從外部的遠程倉庫下載,緩存在私服上之后,再為Maven的下載請求提供服務。
構建Maven私服使用Nexus,Nexus是一個強大的Maven倉庫管理器,它極大地簡化了自己內部倉庫的維護和外部倉庫的訪問。利用Nexus你可以只在一個地方就能夠完全控制訪問 和部署在你所維護倉庫中的每個Artifact。Nexus是一套“開箱即用”的系統不需要數據庫,它使用文件系統加Lucene來組織數據。Nexus 使用ExtJS來開發界面,利用Restlet來提供完整的REST APIs,通過m2eclipse與Eclipse集成使用。Nexus支持WebDAV與LDAP安全身份認證。
構建你的Nexus
說完了私服的好處,你是不是已經等不及開始構建你的maven私服了,那么我們開始一起構建我們的私服。首先進入Nexus的網站http://www.sonatype.org/nexus...,找到你需要的包,下載(演示在CentOS上安裝):

如果你希望用一些歷史版本的包,https://help.sonatype.com/rep...,里面自行查找。
首先下載對應的包到服務器上:
mkdir tools #新建tools目錄 cd tools # 進入tools目錄 wget http://download.sonatype.com/nexus/3/nexus-3.14.0-04-unix.tar.gz # 下載對應的安裝包 tar zxvf nexus-3.14.0-04-unix.tar.gz # 解壓縮 mv nexus-3.14.0-04/ /usr/local/nexus cd /usr/local/nexus/bin
安裝java運行環境:
yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
配置私服
修改nexus.rc,讓root可以啟動nexus,nexus.rc在/usr/local/nexus/bin/下:
vim nexus.rc,刪除run_as_user前面的注釋,后面加上root:run_as_user=root
然后按esc按鍵,輸入:wq回車。然后啟動nexus
./nexus run &
出現如下內容,表示啟動成功

通過http://localhost:8081就可以訪問了。

手動同步索引(非必選項)
首先:前往maven中央倉庫下載 indexer-cli-5.1.1.jar解壓工具
其次下載:nexus-maven-repository-index.properties和nexus-maven-repository-index.gz
再次,將上面下載的3個文件放到同一個路徑下,通過如下命令解壓:
java -jar indexer-cli-5.1.1.jar -u nexus-maven-repository-index.gz -d indexer
最后,拷貝索引
nexus3.x拷貝到/sonatype-work/nexus3/blobs/default,3.x
web的使用
首先訪問對應的地址,然后輸入默認用戶名 admin、密碼 admin123:

點擊左側的browse,可以看到各種repository的type,那么這些類型有什么區別呢:
- Group:這是一個倉庫聚合的概念,用戶倉庫地址選擇Group的地址,即可訪問Group中配置的
所有倉庫資源,訪問順序取決於配置順序3.x默認Releases,Snapshots,Central,可在web頁面配置
在web界面點開
- Hosted:私有倉庫,專門用來存儲我們自己生成的jar文件
- 3rd party:未發布到公網的第三方jar (3.x去除了)
- Snapshots:本地項目的快照倉庫
- Releases: 本地項目發布的正式版本
- Proxy:公網上發布的jar 例如:spring
- Central:中央倉庫
- Apache Snapshots:Apache專用快照倉庫(3.x去除了)
進入設置頁面

作如下操作:

配置maven的setting.xml(本地的全局配置)
在maven的setting.xml文件中配置私服配置,這種方式配置后所有本地使用該配置的maven項目的pom文件都無需配置私服下載相關配置(下文中192.179.101.1:8081需要替為你自己的)
<?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <servers> <server> <id>nexus-releases</id> <username>deployment</username> <password>deployment123</password> </server> <server> <id>nexus-snapshots</id> <username>deployment</username> <password>deployment123</password> </server> </servers> <mirrors> <mirror> <id>nexus-releases</id> <mirrorOf>*</mirrorOf> <url>http://192.168.101.1:8081/content/groups/public/</url> <!-- <url>http://repo1.maven.org/maven2/</url> --> </mirror> <mirror> <id>nexus-snapshots</id> <mirrorOf>*</mirrorOf> <url>http://192.168.101.1:8081/content/repositories/snapshots/</url> <!-- <url>http://repo1.maven.org/maven2/</url> --> </mirror> </mirrors> <profiles> <profile> <id>nexus</id> <repositories> <repository> <id>nexus-releases</id> <url>http://192.168.101.1:8081/content/groups/public/</url> <!-- <url>http://repo1.maven.org/maven2/</url> --> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> </repository> <repository> <id>nexus-snapshots</id> <url>http://192.168.101.1:8081/content/repositories/snapshots/</url> <!-- <url>http://repo1.maven.org/maven2/</url> --> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>nexus-releases</id> <url>http://192.168.101.1:8081/content/groups/public/</url> <!-- <url>http://repo1.maven.org/maven2/</url> --> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> </pluginRepository> <pluginRepository> <id>nexus-snapshots</id> <url>http://192.168.101.1:8081/content/repositories/snapshots/</url> <!-- <url>http://repo1.maven.org/maven2/</url> --> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> </pluginRepository> </pluginRepositories> </profile> </profiles> <activeProfiles> <activeProfile>nexus</activeProfile> <!--<activeProfile>dev</activeProfile>--> </activeProfiles> </settings>
捷徑:docker部署Nexus
docker確實是個好東西,快速,方便,使用docker部署Nexus那就是幾分鍾的事情具體如下命令:
docker pull sonatype/nexus3 docker run -d -p 8081:8081 --name nexus sonatype/nexus3
啟動完成后,方位http://localhost:8081就可以進入web頁面了,其他操作和配置和上面的內容一致,因此這部分就不在這里描述了。
關注我,關注測試
