Maven 私服配置

最近由于公司 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 没有写权限导致。

原文地址:https://www.cnblogs.com/java-linux/p/10263568.html