How to change maven repository folder in windows? - windows

In windows, maven downloads everything in the C:\Documents And Settings\MyUser\.m2 folder (or C:\Users\MyUser\.m2). There exists some way to change the folder that it uses? Specially I want to set it to download anywhere BUT in the Documents And Settings/Users folder.

Look at your settings.xml in ${maven.home}/conf or, preferrably, ${user.home}/.m2/settings.xml (see this for details about the settings.xml). You can add (or uncomment) the following section:
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ~/.m2/repository
-->
<localRepository>/path/to/local/repo</localRepository>
as suggested by the commented out section already there by default. There, you should be able to change the path to achieve what you want.

For Eclipse IDE
If you are using your local maven as the Maven Installation in M2 plugin in Eclispe, editing the settings.xml wasn't enough for me. The plugin was still using the default {user.home}/.m2 as the repository location.
I had to go to : Windows > Preferences > Maven > User Settings and point to my local maven's settings.xml file in the Global Settings and/or User Settings input fields. Clicking apply will then start storing artifacts in the correct destination.

Related

Use default settings.xml file instead of one in ~/.m2

Is there possibility for Maven to ignore settings.xml contained in .m2 folder?
I want it to use default repository, but my laptop has configured development environment for other repositories and I don't want to lose these settings. What's more, these repositories have priority over repos in my pom.xml and fail if they can't download artifact.
What are the options? Should I choose other settings.xml file or is there option to use none?
You don't have to have a settings.xml in the .m2 folder. If there is none Maven uses <Maven installation folder>/conf/settings.xml, which has everything commented out by default.
See also Settings Reference, Quick Overview:
There are two locations where a settings.xml file may live:
The Maven install: ${maven.home}/conf/settings.xml
A user’s install: ${user.home}/.m2/settings.xml
You can use custom settings files with the command line options:
-s,--settings
Alternate path for the user settings file
-gs, --global-settings
Alternate path for the global settings file
And, BTW, .m2 is not the Maven home. It's the folder for user-specific configuration and local repository. I corrected the question's title acordingly.

How to prevent Maven from dowloading content into default user Repository folder?

I recently installed Maven and I want to avoid it from downloading content into the Windows OS hard drive. I added the following line in the global settings.xml file:
<localRepository>D:\path\to\maven\local\repository</localRepository>
And as soon as I run mvn clear on a sample project it creates content into the %USERPROFILE%\.m2\repository folder.
How to configure Maven for storing content at D:\path\to\maven\local\repository only?
If you are using eclipse to build your project then eclipse comes with its own embedded Maven. Default location for the maven repository would be ~/.m2/repository. You can change the Default maven installation to your maven installation from Preferences --> Maven --> Installations and then add location of your maven installation.

Point maven settings file to refer from \.m3\settings.xml instead of \.m2\settings.xml

I am upgrading from maven 2 to maven 3. I have changed the maven home from M2_HOME to M3_HOME, created .m3 folder put the settings.xml inside it. Removed the .m2 folder also.
I see in the maven 3 global settings.xml, the default location of settings.xml is .m2\settings.xml. I could force maven to read the settings.xml from inside .m3 folder by supplying
mvn clean install -Dmaven.repo.local=C:/Users/tabeme/.m3/repository --settings C:\Users\tabeme\.m3\settings.xml
But I dont want to do this every time I build. Is there a way I can permanently make maven to read settings.xml from .m3 folder?
Maven 3 is a drop in replacement for maven 2.
It uses M2_HOME and .m2 folders as normal.
You should only change settings if you for some reason want to run multiple maven instances.

How to specify different local maven repositories for different projects in their pom files?

I have one .m2 repository. I have 2 projects. JavaProject and HibernateProject. I want them to reference different .m2 repositories. How is this done?
You can't specify the local repository location in your POM file. If you were able to, it would break Maven: the whole point is to be able to distribute your POM file to other people and have the code just work. If the POM file contains a reference to ~seanmc/localrepos/hibernateproject then it's not going to work on my machine.
The place where you specify the local repository is in your settings.xml. Typically you have one global settings and one user settings. You can pass the -s flag to Maven to specify a settings file to use. So you could make a settings file for each project. Inside the settings file (reference) you'll want a <localRepository> tag with the path to the local Maven repository you want to use.
Be aware, though, that doing this is contrary to Maven's design. Probably, you have a bug in your architecture or your thinking.

M2E setting file

I just have installed M2E for my Eclipse IDE, I'd like to know how to create a setting file, which can be used to config maven for eclipse, contains infomation such as remote repository, etc. Whether I need to download maven and set maven_home to do that?
Thanks!
in Eclipse go to: Window -> Preferences -> Maven -> Installation: select a file (your settings.xml) for global settings for the embedded installation. The file can be located everywhere you want (the .m2 repository folder is a good location) and must fit the settings.xml style you can find in the documentation.

Resources