Where is the Maven Local Repository? – Maven本地仓库在哪里?

最后修改: 2016年 10月 14日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’ll focus on where Maven stores all the local dependencies locally, which is in the Maven local repository.

在本教程中,我们将重点讨论Maven在本地存储所有本地依赖项的地方,也就是Maven本地仓库。

Simply put, when we run a Maven build, the dependencies for our project (jars, plugin jars, other artifacts) are all stored locally for later use.

简单地说,当我们运行Maven构建时,项目的依赖性(罐子、插件罐子、其他工件)都被储存在本地,以便日后使用。

Also keep in mind that, beyond just this type of local repository, Maven supports three types of repositories:

另外请记住,除了这种类型的本地仓库,Maven还支持三种类型的仓库。

  • Local – Folder location on the local Dev machine
  • Central – Repository provided by Maven community
  • Remote – Organization owned custom repository

Now let’s focus on the local repository.

现在让我们关注一下本地仓库。

2. The Local Repository

2.本地存储库

Maven’s local repository is a directory on the local machine that stores all the project artifacts.

Maven的本地仓库是本地机器上的一个目录,储存了所有项目工件。

When we execute a Maven build, Maven automatically downloads all the dependency jars into the local repository. Usually, this directory is named .m2.

当我们执行Maven构建时,Maven会自动将所有依赖的jar下载到本地资源库。通常,这个目录被命名为.m2

Here’s where the default local repository is located based on OS:

这里是基于操作系统的默认本地资源库的位置。

Windows: C:\Users\<User_Name>\.m2
Linux: /home/<User_Name>/.m2
Mac: /Users/<user_name>/.m2

And for Linux and Mac, we can write in the short form:

而对于Linux和Mac,我们可以用简短的形式来写。

~/.m2

3. Custom Local Repository in settings.xml

3.在settings.xml中自定义本地存储库

If the repo isn’t present in this default location, it’s likely because of some pre-existing configuration.

如果 repo 没有出现在这个默认位置,很可能是因为一些预先存在的配置。

That config file is located in the Maven installation directory in a folder called conf, with the name settings.xml.

该配置文件位于Maven安装目录下一个名为conf的文件夹,settings.xml

Here’s the relevant configuration that determines the location of our missing local repo:

下面是确定我们丢失的本地 repo 的位置的相关配置。

<settings>
    <localRepository>C:/maven_repository</localRepository>
    ...

This is essentially how we can change the location of the local repo. Of course, if we change that location, we’ll no longer find the repo at the default location.

这实质上就是我们如何改变本地 repo 的位置。当然,如果我们改变了这个位置,我们就不会再在默认位置找到 repo。

The files stored in the earlier location won’t be moved automatically.

存储在早期位置的文件不会被自动移动。

4. Passing Local Repository Location via Command Line

4.通过命令行传递本地存储库的位置

Apart from setting the custom local repository in Maven’s settings.xml, the mvn command supports the maven.repo.local property, which allows us to pass the local repository location as a command-line parameter:

除了在Maven的settings.xml中设置自定义本地仓库外,mvn命令支持maven.repo.local属性,它允许我们将本地仓库的位置作为命令行参数传递。

mvn -Dmaven.repo.local=/my/local/repository/path clean install

In this way, we don’t have to change Maven’s settings.xml.

这样一来,我们就不必改变Maven的settings.xml

5. Conclusion

5.结论

In this brief article, we looked at the Maven local repository default setup.

在这篇简短的文章中,我们了解了Maven本地仓库的默认设置。

We also discussed how to tell Maven to work with a custom local repository location.

我们还讨论了如何告诉Maven使用自定义的本地版本库位置。