Change Maven Local Repository Location
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>
<localRepository>
is ${user.home}/.m2/repository
.
settings.xml
Thesettings.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
/home/<user_name>/.m2
~/.m2
/Users/<user_name>/.m2
~/.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>
<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
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 MavenSettings Reference