Home  >  Maven

Change Maven Local Repository Location

By Arvind Rai, May 19, 2021
To change Maven local repository we need to change the value of <localRepository> element in settings.xml file.
<localRepository>{path-to-local-repo}</localRepository> 
Default value of <localRepository> is ${user.home}/.m2/repository.

settings.xml

The settings.xml file can be found in two locations.
1. ${maven.home}/conf/settings.xml
2. ${user.home}/.m2/settings.xml

The ${maven.home} is Maven installation home and ${user.home} is location of user specific directory. Find the location of settings.xml for ${user.home} OS wise.
1. Windows
 C:\Users\<user_name>\.m2 
2. Linux
/home/<user_name>/.m2 
Or
~/.m2 
3. Mac
/Users/<user_name>/.m2 
Or
~/.m2 

The settings.xml file located in ${maven.home} is called global setting and settings.xml file located in ${user.home} is called user specific setting. If both file exists, their contents are merged and user specific settings.xml located in ${user.home} will be dominant.



<localRepository>

The location of local repository of a build system are configured using <localRepository> element in settings.xml file.
settings.xml
<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>${user.home}/.m2/repository</localRepository>
  
  ......

</settings> 
Default value of <localRepository> is ${user.home}/.m2/repository. We can change the location of local repository using <localRepository> element by passing our local repository location.
<localRepository>{path-to-local-repo}</localRepository> 

Purging Local Repository Dependencies

Use following command to purge local repository.
mvn dependency:purge-local-repository 
The default behavior of the above command is first resolve the entire dependency tree then delete the contents from the local repository and then re-resolve the dependencies.
By default, the plugin operates on all transitive dependencies. Plugin may download the certain missing dependencies to gather the full dependency information before beginning the purge process. We can avoid the pre-download step using following command.
mvn dependency:purge-local-repository -DactTransitively=false 

References

Configuring Maven
Settings Reference
POSTED BY
ARVIND RAI
ARVIND RAI








©2021 concretepage.com | Privacy Policy | Contact Us