Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change Maven local repository in eclipse

Tags:

eclipse

maven

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

like image 497
David Zhao Avatar asked Apr 19 '12 19:04

David Zhao


People also ask

How do I update my local Maven repository?

Click settings and search for "Repositories", then select the local repo and click "Update". That's all.

Where is the .m2 folder in Eclipse?

Eclipse IDE, menu bar. Select Window > Preferences. Select Java > Build Path > Classpath Variables. Click on the new button > defined a new M2_REPO variable and point it to your local Maven repository.

How do I access my local Maven repository?

The Maven local repository is located in the /home/. m2 directory, the folder is probably hidden.


4 Answers

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

enter image description here

like image 146
David Zhao Avatar answered Oct 13 '22 18:10

David Zhao


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

like image 39
sjngm Avatar answered Oct 13 '22 19:10

sjngm


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.

like image 21
mdfst13 Avatar answered Oct 13 '22 19:10

mdfst13


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

like image 1
Ajinkya Avatar answered Oct 13 '22 19:10

Ajinkya