Java 在 pom.xml 文件中设置 Maven 本地存储库位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51686612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:12:29  来源:igfitidea点击:

Set the maven local repository location in a pom.xml file?

javamaven

提问by drew

It's possible to set the maven local repository in settings.xml:

可以在 settings.xml 中设置 Maven 本地存储库:

<localRepository>${user.home}/.m2/repository</localRepository>

And it's possible to set the maven local repository on the command line:

并且可以在命令行上设置 maven 本地存储库:

mvn clean install -Dmaven.repo.local=repository

Is it possible to specify within the pom.xml itself?

是否可以在 pom.xml 本身中指定?

Note: I'd like a way to specify, in the pom.xml, where maven initially searches for artifacts (by default, ~/.m2/repository) and where maven installs artifacts via mvn install(by default, ~/.m2/repository).

注意:我想要一种在 pom.xml 中指定 maven 最初搜索工件的位置(默认情况下,~/.m2/repository)以及 maven 安装工件的位置mvn install(默认情况下,~/.m2/repository)的方法。

采纳答案by juzraai

According to the Maven POM Referenceand the Guide to using multiple repositories, you can specify repositories in pom.xmltoo.

根据Maven POM ReferenceGuide to using multiple repositories,您也可以指定存储库pom.xml

There are two different ways that you can specify the use of multiple repositories. The first way is to specify in a POM which repositories you want to use

您可以通过两种不同的方式指定使用多个存储库。第一种方法是在 POM 中指定要使用的存储库

And according to Introduction to repositories, you can use the file://protocol in <url>.

根据存储库介绍,您可以file://<url>.

Remote repositories refer to any other type of repository, accessed by a variety of protocols such as file:// and http://.

远程存储库是指任何其他类型的存储库,可通过各种协议访问,例如 file:// 和 http://。

So the following works:

所以以下工作:

<project>
  ...
  <repositories>
    <repository>
      <id>example-repo</id>
      <name>Example Repository</name>
      <url>file://path/to/your/local/repository</url>
    </repository>
  </repositories>
</project>


Edit:

编辑:

Based on your comment and edit, you need to override the default repository and Maven home directory in pom.xml.

根据您的评论和编辑,您需要覆盖pom.xml.

I've found a topic about disabling central repository, and tried out the answers, but Maven still uses the values from settings.xml. This answerin another thread explains why:

我找到了一个关于禁用中央存储库主题,并尝试了答案,但 Maven 仍然使用settings.xml. 另一个线程中的这个答案解释了原因:

settings.xmlallows you to override definitions in pom.xml, not the other way round.

settings.xml允许您覆盖 中的定义pom.xml,而不是相反。

So it's seems it is not possible to override the default mechanism from pom.xml, Maven will search for dependencies in repositories configured in settings.xmland will install to Maven home specified in that file.

因此,似乎不可能从 覆盖默认机制pom.xml,Maven 将在配置的存储库中搜索依赖项settings.xml,并将安装到该文件中指定的 Maven 主目录。