欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

pom中更换阿里云仓库时不要忽略了pluginRepositories

程序员文章站 2022-10-28 08:20:51
用maven也大几年了,也一直在用阿里云的*仓库。 不喜欢在maven的settings.xml里改,更喜欢直接在pom.xml里改,因为受git管理,小伙伴们拉下来即可。 然而网上的大部分技术文章都只会指导你这么配置: 如果你只是配置了repositories,那么你会发现在mvn在下载依赖的时 ......

用maven也大几年了,也一直在用阿里云的*仓库。
不喜欢在maven的settings.xml里改,更喜欢直接在pom.xml里改,因为受git管理,小伙伴们拉下来即可。

然而网上的大部分技术文章都只会指导你这么配置:

<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 )下载。

# mvn clean install
[info] scanning for projects...
downloading from aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/springframework/boot/spring-boot-starter-parent/2.0.2.release/spring-boot-s
tarter-parent-2.0.2.release.pom
downloaded from aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/springframework/boot/spring-boot-starter-parent/2.0.2.release/spring-boot-st
arter-parent-2.0.2.release.pom (12 kb at 3.1 kb/s)
...
...
...
[info]
[info] --------------------------< com.zy:zy-parent >--------------------------
[info] building zy-parent 1.0.0-snapshot
[info] --------------------------------[ pom ]---------------------------------
downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/3.0.0/maven-clean-plugin-3.0.0.pom
downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/3.0.0/maven-clean-plugin-3.0.0.pom (4.8 kb at 2.2 k
b/s)
...
...
...
[info]
[info] --- maven-clean-plugin:3.0.0:clean (default-clean) @ zy-parent ---
downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-plugin-api/3.0/maven-plugin-api-3.0.pom
downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-plugin-api/3.0/maven-plugin-api-3.0.pom (2.3 kb at 1.5 kb/s)
...
...
...
[info]
[info] --- maven-install-plugin:2.5.2:install (default-install) @ zy-parent ---
downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-plugin-api/2.2.1/maven-plugin-api-2.2.1.pom
downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-plugin-api/2.2.1/maven-plugin-api-2.2.1.pom (1.5 kb at 1.2 kb/s)
...
...
...
[info] installing e:\work\scratch\lily\src\zy-parent\pom.xml to d:\tools\maven\repo\com\zy\zy-parent\1.0.0-snapshot\zy-parent-1.0.0-snapshot.pom
[info] ------------------------------------------------------------------------
[info] build success
[info] ------------------------------------------------------------------------
[info] total time: 04:25 min
[info] finished at: 2019-11-14t10:13:53+08:00
[info] ------------------------------------------------------------------------

原来,只有项目本身的依赖,走了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>https://maven.aliyun.com/repository/public</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>
<pluginrepositories>
    <pluginrepository>
        <id>aliyun-plugin</id>
        <url>https://maven.aliyun.com/repository/public</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </pluginrepository>
</pluginrepositories>

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