Local Repository & Configuration
About
Apache Maven uses a local repository and configuration files to manage dependencies, plugins, and build settings. Understanding the structure and customization options of Maven's local repository and configuration files is essential for effectively managing Maven-based projects.
What is the Local Repository?
The local repository is a directory on the developer's machine where Maven stores all the project dependencies, plugins, and artifacts that it downloads from remote repositories such as Maven Central.
Default Location
Windows:
C:\Users\<username>\.m2\repository
macOS/Linux:
/Users/<username>/.m2/repository
or~/.m2/repository
Purpose
Acts as a cache to avoid downloading dependencies multiple times.
Improves build performance and offline capabilities.
Custom-built artifacts and third-party JARs not available in public repositories can also be installed here using the
mvn install
command.
Global & User Level Settings
Maven supports two levels of configuration files: global settings and user-specific settings.
1. Global settings.xml
settings.xml
Location:
<MAVEN_HOME>/conf/settings.xml
(e.g.,/opt/apache-maven-3.9.6/conf/settings.xml
on Unix-based systems orC:\Program Files\Apache\maven-x.y.z\conf\settings.xml
on Windows)Scope: Applies system-wide, meaning it affects all users on the machine who run Maven using that installation.
Purpose:
Define default configurations for all users (e.g., corporate mirrors, proxies).
Useful in enterprise setups or build environments (e.g., CI/CD agents).
Avoid putting user-specific credentials or local paths here.
Editing Notes: Requires admin or write access to the Maven installation directory. Avoid making unnecessary changes to prevent affecting all builds globally.
2. User settings.xml
settings.xml
Location:
~/.m2/settings.xml
(e.g.,/Users/john/.m2/settings.xml
on macOS/Linux orC:\Users\john\.m2\settings.xml
on Windows)Scope: Applies only to the current user. Overrides corresponding values in the global
settings.xml
.Purpose:
Customize Maven behavior for a specific developer.
Store personal repository credentials, custom profiles, proxies, or environment-specific configurations.
Keeps user-level customizations isolated and easy to manage.
Editing Notes: Does not require admin access. Safe to modify. If the file does not exist, it can be created manually.
3. Precedence and Override Behavior
When both files are present:
Maven first reads the global
settings.xml
.Then it overlays values from the user-level
settings.xml
.This means values in the user file take priority over the global settings if both define the same element (e.g.,
mirrors
,servers
,profiles
).
The .m2
Directory
.m2
Directory.m2
is a hidden directory in our user’s home folder created by Maven. It stores configuration files and the local repository of downloaded Maven dependencies.
Default Location
Windows:
C:\Users\<username>\.m2\
macOS/Linux:
/Users/<username>/.m2/
or~/.m2/repository
Structure of .m2
Directory
.m2
Directoryrepository/
repository/
Contains all Maven artifacts (JARs, POMs) that Maven has downloaded.
Helps avoid downloading dependencies repeatedly from the internet.
We can safely delete this folder to force Maven to re-download all dependencies.
settings.xml
settings.xml
This file is used to override default Maven settings.
It is not created by default - we need to create it manually if customization is needed.
Full path:
Windows:
C:\Users\<username>\.m2\settings.xml
macOS/Linux:
~/.m2/settings.xml
settings-security.xml
settings-security.xml
This file is used by Maven to securely store the master password, which is required to decrypt encrypted passwords stored in the
settings.xml
file. It enhances security by avoiding plain text credentials.It is not created by default - we must generate it manually when using encrypted passwords in
settings.xml
.Full path:
Windows:
C:\Users\<username>\.m2\settings-security.xml
macOS/Linux:
~/.m2/settings-security.xml
Local Repository (repository/
)
repository/
)About
The repository/
directory is Maven’s local artifact cache. It stores all dependencies, plugins, and project-specific artifacts resolved during Maven builds. It contains -
Downloaded Dependencies: JARs, POMs, sources, and javadocs pulled from remote repositories like Maven Central.
Project Artifacts: If we run
mvn install
, our project’s JAR or WAR is saved here.Directory Structure:
Maven uses a groupId-path layout. For example:
Usage
Maven checks this directory before downloading anything from a remote repo.
Speeds up builds by caching artifacts locally.
Enables offline builds (
mvn -o
or--offline
).
Default Location
Windows:
C:\Users\<your-username>\.m2\repository
Linux/macOS:
/Users/<your-username>/.m2/repository
or~/.m2/repository
Customizing Location
We can change the default location by:
Adding this to
~/.m2/settings.xml
:Or using the CLI:
Maintenance
Clean unused artifacts periodically.
Use:
User-Specific Configuration File (settings.xml
)
settings.xml
)About
This is an optional XML configuration file for Maven that allows us to override default behaviors, set profiles, proxies, server credentials, mirrors, and more - specific to our user.
Default Location
Windows:
C:\Users\<username>\.m2\settings.xml
Linux/macOS:
~/.m2/settings.xml
Note: If not present, Maven uses only the global settings from
MAVEN_HOME/conf/settings.xml
.
Key Configurable Elements
a. localRepository
Override the default location of the local repository.
b. mirrors
Redirect all Maven traffic to a mirror, e.g., internal Artifactory or Nexus:
c. proxies
Needed in corporate environments where internet access is behind a proxy:
d. servers
Store credentials for repositories or deployment servers:
Use
id
values that match repository definitions inpom.xml
.
e. profiles
and activeProfiles
Define environment-specific properties and configurations:
Security
Avoid plain text passwords. Use
settings-security.xml
for encryption (explained below).
Sample settings.xml
File
settings.xml
FileSecurity Configuration (settings-security.xml
)
settings-security.xml
)About
settings-security.xml
allows you to store the master password that Maven uses to decrypt encrypted passwords in settings.xml
.
Default Location
Windows:
C:\Users\<username>\.m2\settings-security.xml
Linux/macOS:
~/.m2/settings-security.xml
How It Works ?
Generate Encrypted Password:
Example output:
Use Encrypted Password in
settings.xml
:Generate Master Password:
Save the output in
settings-security.xml
:
Maven will automatically use this master password to decrypt the encrypted password at runtime.
Purpose
Secure sensitive credentials without hardcoding them in plain text.
Required for organizations or CI systems dealing with private repositories.
Sample settings-security.xml
File
settings-security.xml
FileLast updated
Was this helpful?