Maven 使用 Nexus 內部庫 代理


反正任由總理怎么強調,在中國的當前的網絡環境下,中央庫的訪問速度總是令人心碎。建一個nexus內部庫可以建立緩存,只要有人通過它下載了相關的maven依賴,那么別人需要時可以馬上從本地網絡的服務器上返回而不需要在從中央庫去下了。內部也可以將jar發布到本地服務器上便於隊友共享使用。有時候隊友只是想直接用你編譯好的包,而不想花時間去配什么本地環境。所以這比單單代碼庫又方便快捷了許多。

1. nexus服務器

基本開箱可用,改一下管理員密碼即可。已經預先為用戶把中央倉庫以代理的方式加進來了,改一下打開下載索引的配置即可。不過因為糟糕的網絡環境,這通常是成功不了的。

 

2. maven的settings.xml文件

可以從maven安裝目錄里復制一份到用戶目錄下得.m2文件夾下作為用戶settings(確保eclipse中的maven插件中settings文件路徑正確),也可以直接在里面改作為global settings

<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">
  <pluginGroups>
  </pluginGroups>
<proxies> </proxies> <mirrors> <mirror> <id>local-nexus</id> <mirrorOf>*</mirrorOf> <name>database lab local maven repo</name> <url>http://10.214.208.144:8080/nexus/content/groups/public/</url> </mirror> </mirrors> <profiles> <profile> <id>dev</id> <repositories> <repository> <id>central</id> <name>local-maven-main-repository</name> <url>http://10.214.208.144:8080/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>central</id> <name>local-maven-plugin-repository</name> <url>http://10.214.208.144:8080/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </profile> </profiles> <activeProfiles> <activeProfile>dev</activeProfile> </activeProfiles> </settings>

profile中的的repository最好把pluginRepositories也寫上。不過感覺只配個mirror也可以了,不用配profiles和activeProfiles。不過可能沒有遇到特殊的情況,因為profile里把releases和snapshots兩選項打開了,真是搞不明白,配置夠煩得。

 

3. eclipse

新建一個maven項目,然后如果eclipse有對pom.xml打了個叉,提示有什么依賴包下載不到什么的,先不用管它。

到項目所在的目錄執行:

mvn clean

mvn package

第一個先做個清理,第二個進行一次打包,相應的maven需要的插件就會去下載了,一般成功的話會顯示BUILD SUCCESS。然后多刷新eclipse的pom.xml文件就可以了,或者關閉eclipse再打開,那些錯誤應該就沒了。

 

 一些包無法下載

1. nexus剛剛運行的時候,有些包可能無法下載,在管理界面里可以對比browse index和browse remote里面的項,有些只有在remote里有本地index里還沒有。

另外實在碰到一些奇葩包無法下載,可以把本地maven庫緩存里的包全刪了(.m2/repository),然后mvn clean再來就行了。最好是等服務器搭好,過個一天再嘗試。可以把repo.spring.io這個倉庫加進來,里面有很多東西。

2. 在庫的配置選項Routing頁中把Discovery關閉(去掉選擇)

默認情況下是打開的,但因為好像有的庫中會有一個prefix文件,

點可以查看prefix文件內容:

## repository-prefixes/2.0
#
# Prefix file generated by Sonatype Nexus
# Do not edit, changes will be overwritten!
/archetype-catalog.xml.sha1
/archetype-catalog.xml.md5
/monetdb/monetdb-jdbc
/archetype-catalog.xml
/pentaho/json
/org/json

打開discovery后不在prefix里的包好像都下載不到,在瀏覽器中直接輸入對應地址的話會出現類似以下的錯誤:

404 - ItemNotFoundException

Request is marked as local-only, remote access not allowed from M2Repository(id=pentaho-snapshot)

 org.sonatype.nexus.proxy.ItemNotFoundException: Request is marked as local-only, remote access not allowed from M2Repository(id=pentaho-snapshot)

把discovery禁用的話,就不會按照prefix文件的限定,可以下載任何庫里能看到的包

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM