How to change Maven local repository in eclipse

EclipseMaven

Eclipse Problem Overview


I'm importing a multiple-module Maven project from the root directory on a shared drive on another server A. On server A, mvn install will run successfully, and Maven is set up properly with repository/ and settings.xml in /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.xml and repository/ on my workstation PC, so there won't be any module build installed in the .m2 cache.

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".

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?

enter image description here

Eclipse Solutions


Solution 1 - Eclipse

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

enter image description here

Solution 2 - Eclipse

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

Windows > Preferences > Maven > User Settings > Global Settings

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

Solution 3 - Eclipse

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 Settings entry at

Maven User Settings dialog

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.

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

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

<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.

Credit to this comment by @ejaenv for the name of the element in the settings file: <localRepository>. See Maven — Settings Reference for more information.

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

If you already have a settings.xml file, you should merge this into your existing file. I.e. <settings and <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.

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

Solution 4 - Eclipse

In Eclipse Photon navigate to 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

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

Solution 5 - Eclipse

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.

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.

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.

Solution 6 - Eclipse

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid ZhaoView Question on Stackoverflow
Solution 1 - EclipseDavid ZhaoView Answer on Stackoverflow
Solution 2 - EclipsesjngmView Answer on Stackoverflow
Solution 3 - Eclipsemdfst13View Answer on Stackoverflow
Solution 4 - EclipseAjinkyaView Answer on Stackoverflow
Solution 5 - EclipseAdam GriptonView Answer on Stackoverflow
Solution 6 - EclipseAlexánder RojasView Answer on Stackoverflow