最近由於公司 maven 私服出現問題,在修復問題的過程中順便整理了下 maven 私服配置的兩種方式,在此記錄下。
以下配置均在 settings.xml 中配置,私服配置不建議配置在某個項目的 pom.xml 文件中。
鏡像方式配置
maven 在默認情況下是從中央倉庫下載構建,也就是 id 為 central 的倉庫。如果沒有特殊需求,一般只需要將私服地址配置為鏡像,同時配置其代理所有的倉庫就可以實現通過私服下載依賴的功能。鏡像配置如下:
<mirror>
<id>Nexus Mirror</id>
<name>Nexus Mirror</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
<mirrorOf>*</mirrorOf>
</mirror>
當按照以上方式配置之后,所有的依賴下載請求都將通過私服下載。
遠程倉庫方式配置
除了鏡像方式的配置,我們還可以使用覆蓋默認 central 倉庫的方式來配置私服。
此配置中的遠程倉庫 id 是 central,也就是中央倉庫的 id,這么配置的原因就是使用當前配置的私服地址覆蓋默認的中央倉庫地址,配置如下:
<profile>
<activation>
<!-- 默認激活此 profile -->
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>central</id>
<name>central</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<name>central</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
總結
以上兩種方式都可以實現配置 maven 私服的功能,個人建議使用鏡像的方式配置,最方便簡單,不易出錯,同時將訪問外部倉庫的職責全部丟給私服,方便私服統一管理。
PS:當碰到私服服務下載遠程倉庫 jar包,或者本地 deploy jar 到私服報 500 錯誤時,多半是因為私服的存儲目錄 nexus 沒有寫權限導致。