搭建nexus私服
以前我們用maven管理的方式的是中央倉庫<-->本地倉庫 的形式。這樣的缺點是不能共享第三方的包,當引入新包時,其他人需要手動新增到本地倉庫或新增到lib下。為了解決這個問題,使用nexus搭建內網私服。下面闡述搭建過程
一 安裝啟動nexus
步驟1 下載 不知道為什麼,nexus官網(http://www.sonatype.org/nexus/go/)無法下載最新的nexus。最後在csdn上下了一個2.4的版本。
步驟2 安裝 解壓就可以了。
步驟3 執行nexus 執行 ~/nexus-2.4.0-09/bin/jsw/linux-x86-64/nexus start
issue: (1)出現: If you insist running as root, then set the environment variable RUN_AS_USER=root before running this script.
編輯nexus檔案,找到RUN_AS_USER=這一行,取消註釋並修改為:RUN_AS_USER=root 。重啟
issue: (2)執行 ~/nexus-2.4.0-09/bin/jsw/linux-x86-64/nexus console 出現:jvm 1 | Error in WrapperListener.start callback. java.lang.StackOverflowError
把jdk版本換成1.7.(換jdk方式1:改環境變數;方式2:修改nexus/bin/jsw/conf/wrapper.conf 第15行 wrapper.java.command=java 改為 wrapper.java.command=/jdk路徑/bin/java )
二 配置本地maven
2.1 修改settings.xml
2.1.1配置私服映象地址
<mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf> <!-- * 表示讓所有倉庫使用該映象 -->
<url>http://localhost:8081/nexus/content/groups/public</url>
</mirror>
</mirrors>
2.1.2 <profile> <id>nexus</id> <repositories> <repository> <id>nexus</id> <url>http://localhost:8081/nexus/content/groups/public</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>nexus</id> <url>http://localhost:8081/nexus/content/groups/public</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> </pluginRepositories> </profile>
2.1.3 啟用id為nexus的配置
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
三 在eclipse中使用
在eclipse中使用私服的方式有兩種,分別是利用本地maven的settings.xml(就是第二節介紹的),和在pom.xml中宣告。
3.1利用本地maven的settings.xml
window-->perferences-->Maven-->User Settings 指定啟用的maven settings.xml 檔案
3.2 pom中宣告中央倉庫為私服地址
<project>
...
<repositories>
<repository>
<id>nexus</id>
<name>Nexus</name>
<url>http://localhost:8081/nexus/content/groups/public</url>
<release><enabled>true</enabled></release>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<name>Nexus</name>
<url>http://localhost:8081/nexus/content/groups/public</url>
<release><enabled>true</enabled></release>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
...
</project>
四 上傳本地jar
有些jar包在中央倉庫裡沒有的,需要我們手動匯入到nexus中,匯入方式:
1、登陸 (不登陸是沒有上傳許可權的)
2、在首頁上選擇左側選單 Repositories-->再點選3rd party(本地jar只能放在type為hosted的倉庫裡面)-->點選下方Artifact Upload。最後在裡面設定maven座標Group(artifact和version可以在上傳jar包後自動填充)
3、上傳jar包,點選按鈕 select aritfact(s) to Upload ,
4、點選add artifact
5、最後 點選 Upload artifact(s) 。會彈出上傳成功的彈出框
6、檢驗: 點選Brower Storage。可以看到在3rd party資料夾下多了一個子資料夾,選中對應的jar包時,右側可以看到xml檔案dependency的格式座標。
五 jar包在nexus儲存的位置
5.1 jar包儲存在 nexus/sonatype-work/nexus/storage
5.2 自己上傳的第三方jar儲存在storage/thirdparty 中。如果要把jar在私服間遷移,只需要把資料夾儲存到對應的目錄下,如把上傳的GroupId為saas-rep的jar包遷移,只需要把saas-rep放到storage/thirdparty目錄下。