用两种方式来配置阿里云中央仓库,不要忽略了pluginRepositories关键节点

方法一:在setting中配置

在maven的setting配置中搜索mirrors添加上下面的配置即可

		<mirror>
			<id>nexus-aliyun</id>
			<mirrorOf>*</mirrorOf>
			<name>Nexus aliyun</name>
			<url>http://maven.aliyun.com/nexus/content/groups/public</url>
		</mirror> 

方法二:在pom中配置

因为平时可以在git管理,不喜欢在maven的settings.xml里改的,可以直接在pom.xml里改,小伙伴们拉下来即可使用。
但是网上的大部分技术文章大部分都是这么配置:

<repositories>
    <repository>
        <id>aliyun</id>
        <url>https://maven.aliyun.com/repository/public</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

如果你只是配置了repositories,那么你会发现在mvn在下载依赖的时候,一部分从阿里云下载,一部分还是从默认的仓库(https://repo.maven.apache.org )下载。因为只有项目本身的依赖,才会走aliyun这个repository,maven命令需要的插件(比如clean、install都是maven的插件),走的还是默认的repository。
查看maven的官方文档(http://maven.apache.org/pom.html#Plugin_Repositories ),可以看到pom中除了repositories节点之外,还有一个关于仓库的节点是pluginRepositories:

Repositories are home to two major types of artifacts. The first are artifacts that are used as dependencies of other artifacts. These are the majority of plugins that reside within central. The other type of artifact is plugins. Maven plugins are themselves a special type of artifact. Because of this, plugin repositories may be separated from other repositories (although, I have yet to hear a convincing argument for doing so). In any case, the structure of the pluginRepositories element block is similar to the repositories element. The pluginRepository elements each specify a remote location of where Maven can find new plugins.

所以我们还需要在pom中增加pluginRepositories才可以。最终的pom文件如下:

<repositories>
    <repository>
        <id>aliyun</id>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>aliyun-plugin</id>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

现在,你可以清空本地maven仓库中的包,然后再次执一下mvn clean install,看看是不是都走了阿里云的中央仓库了。

猜你喜欢

转载自blog.csdn.net/qq_40136782/article/details/110007120