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

20次阅读

共计 3820 个字符,预计需要花费 10 分钟才能阅读完成。

用 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.h…),可以看到 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,看看是不是都走了阿里云的仓库了。

正文完
 0