如何在 Eclipse 中更改 Maven 本地存储库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10235374/
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-09-10 21:00:36  来源:igfitidea点击:

How to change Maven local repository in eclipse

eclipsemaven

提问by David Zhao

I'm importing a multiple-module Maven project from the root directory on a shared drive on another server A. On server A, mvn installwill run successfully, and Maven is set up properly with repository/and settings.xmlin /home/user/.m2.

我从另一台服务器A.在服务器A上的共享驱动器的根目录中导入一个多模块Maven项目,mvn install将成功运行,和Maven设置正确使用repository/settings.xml/home/user/.m2

However, after I imported the project into Eclipse (STS actually) on my workstation PC, I can't get mvn build(I have the m2e plugin installed) to run, mostly due to missing dependencies from other modules. I realized that Eclipse is using settings.xmland repository/on my workstation PC, so there won't be any module build installed in the .m2 cache.

但是,在我的工作站 PC 上将项目导入 Eclipse(实际上是 STS)后,我无法运行mvn build(我安装了 m2e 插件),主要是因为缺少其他模块的依赖项。我意识到 Eclipse 正在我的工作站 PC 上使用settings.xmlrepository/,因此不会在 .m2 缓存中安装任何模块构建。

I'm trying to modify Maven to use the .m2 directory on server A. I can only set settings.xml to the one on server A, but I cannot change the local repository. The only option I have is to "re-index".

我正在尝试修改 Maven 以使用服务器 A 上的 .m2 目录。我只能将 settings.xml 设置为服务器 A 上的那个,但我无法更改本地存储库。我唯一的选择是“重新索引”。

Is there a way to change the local repository to the one on server A, or is there a better way to have code on one server, and edit/build/test on Tomcat on another PC?

有没有办法将本地存储库更改为服务器 A 上的存储库,或者是否有更好的方法将代码放在一台服务器上,并在另一台 PC 上的 Tomcat 上编辑/构建/测试?

enter image description here

在此处输入图片说明

回答by David Zhao

location can be specified in Maven->Installation -> Global Settings: settings.xml

位置可以在Maven->Installation -> Global Settings: settings.xml 中指定

enter image description here

在此处输入图片说明

回答by sjngm

In newer versions of Eclipse the global configuration file can be set in

在较新版本的 Eclipse 中,全局配置文件可以设置在

Windows > Preferences > Maven > User Settings > Global Settings

Windows > 首选项 > Maven > 用户设置 > 全局设置

Don't beat me why global settings can be configured in user settings... Probably because of the same reason why you need to press "Start" to shutdown your PC on Windows...:D

不要打败我为什么可以在用户设置中配置全局设置...可能是因为您需要按“开始”以在 Windows 上关闭 PC 的相同原因...:D

回答by mdfst13

In general, these answer the question: How to change your user settings file? But the question I wanted answered was how to change my local maven repository location. The answer is that you have to edit settings.xml. If the file does not exist, you have to create it. You set or change the location of the file at Window > Preferences > Maven > User Settings. It's the User Settingsentry at

通常,这些回答了以下问题:如何更改您的用户设置文件?但我想回答的问题是如何更改我的本地 Maven 存储库位置。答案是您必须编辑settings.xml。如果该文件不存在,则必须创建它。您可以在Window > Preferences > Maven > User Settings 中设置或更改文件的位置。这是用户设置条目

Maven User Settings dialog

Maven 用户设置对话框

It's the second file input; the first with information in it.

这是第二个文件输入;第一个包含信息。

If it's not clear, [redacted]should be replaced with the local file path to your .m2 folder.

如果不清楚,[redacted]应替换为 .m2 文件夹的本地文件路径。

If you click the "open file" link, it opens the settings.xmlfile for editing in Eclipse.

如果单击“打开文件”链接,它会打开settings.xml文件以在 Eclipse 中进行编辑。

If you have no settings.xmlfile yet, the following will set the local repository to the Windows 10 default value for a user named mdfst13:

如果您还没有settings.xml文件,以下内容会将本地存储库设置为名为 mdfst13 的用户的 Windows 10 默认值:

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                              https://maven.apache.org/xsd/settings-1.0.0.xsd">

  <localRepository>C:\Users\mdfst13\.m2\repository</localRepository>
</settings>

You should set this to a value appropriate to your system. I haven't tested it, but I suspect that in Linux, the default value would be /home/mdfst13/.m2/repository. And of course, you probably don't want to set it to the default value. If you are reading this, you probably want to set it to some other value. You could just delete it if you wanted the default.

您应该将其设置为适合您系统的值。我还没有测试过,但我怀疑在 Linux 中,默认值为/home/mdfst13/.m2/repository. 当然,您可能不想将其设置为默认值。如果您正在阅读本文,您可能希望将其设置为其他值。如果你想要默认值,你可以删除它。

Credit to this commentby @ejaenvfor the name of the element in the settings file: <localRepository>. See Maven — Settings Referencefor more information.

感谢此评论通过@ejaenv在设置元素的文件的名称: <localRepository>。有关更多信息,请参阅Maven — 设置参考

Credit to @Ajinkya's answerfor specifying the location of the User Settings value in Eclipse Photon.

感谢@ Ajinkya的回答指定在Eclipse中光子的用户设置值的位置。

If you already have a settings.xml file, you should merge this into your existing file. I.e. <settingsand <localRepository>should only appear once in the file, and you probably want to retain any settings already there. Or to say that another way, edit any existing local repository entry if it exists or just add that line to the file if it doesn't.

如果您已有 settings.xml 文件,则应将其合并到现有文件中。即<settings<localRepository>应该只在文件中出现一次,并且您可能希望保留已经存在的任何设置。或者换一种说法,编辑任何现有的本地存储库条目(如果存在),或者仅将该行添加到文件中(如果不存在)。

I had to restart Eclipse for it to load data into the new repository. Neither "Update Settings" nor "Reindex" was sufficient.

我必须重新启动 Eclipse 才能将数据加载到新存储库中。“更新设置”和“重新索引”都不够。

回答by Ajinkya

In Eclipse Photonnavigate to Windows > Preferences > Maven > User Settings > User Setting

Eclipse Photon 中导航到Windows > Preferences > Maven > User Settings > User Setting

For "User settings" Browse to the settings.xml of the maven. ex. in my case maven it is located on the path C:\Program Files\Apache Software Distribution\apache-maven-3.5.4\conf\Settings.xml

对于“用户设置”,浏览到 Maven 的 settings.xml。前任。在我的情况下 maven 它位于路径上C:\Program Files\Apache Software Distribution\apache-maven-3.5.4\conf\Settings.xml

Depending on the Settings.xml the Local Repository gets automatically configured to the specified location. Click here for screenshot

根据 Settings.xml,本地存储库会自动配置到指定位置。 单击此处获取屏幕截图

回答by Adam Gripton

I found that even after following all the steps above, I was still getting errors saying that my Maven dependencies (i.e. pom.xml) were pointing to jar files that didn't exist.

我发现即使按照上述所有步骤,我仍然收到错误消息,说我的 Maven 依赖项(即 pom.xml)指向不存在的 jar 文件。

Viewing the errors in the Problems tab, for some reason these were still pointing to the old location of my repository. This was probably because I'd changed the location of my Maven repository since creating the workspace and project.

查看问题选项卡中的错误,出于某种原因,这些错误仍然指向我的存储库的旧位置。这可能是因为我在创建工作区和项目后更改了 Maven 存储库的位置。

This can be easily solved by deleting the project from the Eclipse workspace, and re-adding it again through Package Explorer -> R/Click -> Import... -> Existing Projects.

这可以通过从 Eclipse 工作区中删除项目并通过 Package Explorer -> R/Click -> Import... -> Existing Projects 再次重新添加来轻松解决。

回答by Alexánder Rojas

Here is settings.xml--> C:\maven\conf\settings.xml

这是settings.xml--> C:\maven\conf\settings.xml