Maven 私服配置

最近因爲公司 maven 私服出現問題,在修復問題的過程當中順便整理了下 maven 私服配置的兩種方式,在此記錄下。maven

如下配置均在 settings.xml 中配置,私服配置不建議配置在某個項目的 pom.xml 文件中。url

鏡像方式配置

maven 在默認狀況下是從中央倉庫下載構建,也就是 id 爲 central 的倉庫。若是沒有特殊需求,通常只須要將私服地址配置爲鏡像,同時配置其代理全部的倉庫就能夠實現經過私服下載依賴的功能。鏡像配置以下:代理

<mirror>
    <id>Nexus Mirror</id>
    <name>Nexus Mirror</name>
    <url>http://localhost:8081/nexus/content/groups/public/</url>
    <mirrorOf>*</mirrorOf>
</mirror>

當按照以上方式配置以後,全部的依賴下載請求都將經過私服下載。code

遠程倉庫方式配置

除了鏡像方式的配置,咱們還可使用覆蓋默認 central 倉庫的方式來配置私服。xml

此配置中的遠程倉庫 id 是 central,也就是中央倉庫的 id,這麼配置的緣由就是使用當前配置的私服地址覆蓋默認的中央倉庫地址,配置以下:it

<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 私服的功能,我的建議使用鏡像的方式配置,最方便簡單,不易出錯,同時將訪問外部倉庫的職責所有丟給私服,方便私服統一管理。io

PS:當碰到私服服務下載遠程倉庫 jar包,或者本地 deploy jar 到私服報 500 錯誤時,多半是由於私服的存儲目錄 nexus 沒有寫權限致使。class

相關文章
相關標籤/搜索