settings.xml

In the Maven ecosystem, settings.xml serves as the user-specific configuration layer that governs how the build tool behaves on a particular machine. While pom.xml defines the project itself—its dependencies, modules, and build lifecycle—settings.xml controls the environment in which Maven operates. It stores local repository paths, remote repository credentials, proxy settings, mirror definitions, and build profiles. Getting this file right can dramatically improve build reliability and speed up dependency resolution.

File Locations and Precedence

Maven looks for settings.xml in two standard locations:

  • Global installation settings: Found in the conf/ directory of the Maven installation. This file applies to every user on the machine.
  • User settings: Located in the user's home directory under .m2/settings.xml (for example, C:\Users\<username>\.m2\settings.xml on Windows or ~/.m2/settings.xml on Linux and macOS). This file applies only to the current user.

When both files exist, Maven merges them, with user-level settings taking precedence over global settings. For most developers, editing the user-level file is the preferred approach. It keeps personal configuration separate from the Maven installation, so upgrading Maven does not overwrite custom settings.

Core Structure

The root element of settings.xml is <settings>. Inside it, several top-level elements organize different aspects of Maven's behavior:

<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 http://maven.apache.org/xsd/settings-1.2.0.xsd">
    <localRepository>${user.home}/.m2/repository</localRepository>
    <interactiveMode>true</interactiveMode>
    <offline>false</offline>
    <pluginGroups></pluginGroups>
    <servers></servers>
    <mirrors></mirrors>
    <proxies></proxies>
    <profiles></profiles>
    <activeProfiles></activeProfiles>
</settings>

The most commonly configured elements include localRepository, servers, mirrors, proxies, and profiles. Elements such as interactiveMode and offline control whether Maven prompts for input and whether it attempts to reach remote repositories during a build.

Configuring the Local Repository

By default, Maven stores downloaded dependencies and plugins in ${user.home}/.m2/repository. If the system drive has limited disk space, or if you want to centralize all Maven artifacts on a dedicated volume, you can redirect the local repository:

<localRepository>D:/maven-repository</localRepository>

This change affects only the machine where settings.xml is edited. Other team members must apply the same setting independently if they want the same directory layout.

Using Mirrors to Accelerate Dependency Downloads

Direct access to Maven Central can be slow or unreliable depending on network geography. A mirror acts as a proxy for a remote repository, allowing Maven to download artifacts from a closer or faster endpoint. For example, to route requests for the central repository through a public mirror:

<mirrors>
    <mirror>
        <id>public-mirror</id>
        <name>Public Mirror Repository</name>
        <url>https://mirror.example.com/repository/public</url>
        <mirrorOf>central</mirrorOf>
    </mirror>
</mirrors>

The <mirrorOf> element identifies which repository the mirror replaces. The value central targets Maven Central. Other possible values include a specific repository ID or the wildcard * to mirror all remote repositories.

Managing Server Credentials

Private repositories such as Nexus, Artifactory, or an internal Maven server often require authentication. Instead of embedding usernames and passwords in pom.xml, which would expose secrets in version control, credentials belong in settings.xml under <servers>:

<servers>
    <server>
        <id>internal-releases</id>
        <username>deploy-user</username>
        <password>{encrypted-password}</password>
    </server>
</servers>

The <id> must match the repository ID declared in pom.xml under <repositories> or <distributionManagement>. Maven uses this ID to look up the correct username and password when downloading dependencies or deploying artifacts.

Storing passwords in plain text is a security risk. Maven provides password encryption through the command line:

mvn --encrypt-password my_secret_password

The generated ciphertext can be placed inside the <password> element. Maven decrypts it at runtime using the master password stored in ~/.m2/security-settings.xml.

Defining Build Profiles

Profiles in settings.xml let you activate different configurations based on the environment. Unlike profiles in pom.xml, which are project-specific, settings-level profiles apply globally to the user. A typical use case is activating a JDK-specific profile:

<profiles>
    <profile>
        <id>jdk-17</id>
        <activation>
            <activeByDefault>true</activeByDefault>
            <jdk>17</jdk>
        </activation>
        <properties>
            <maven.compiler.source>17</maven.compiler.source>
            <maven.compiler.target>17</maven.compiler.target>
        </properties>
    </profile>
</profiles>

When activeByDefault is set to true, the profile activates automatically. You can also conditionally activate profiles based on the JDK version, operating system, or file presence. The <activeProfiles> element allows explicit activation without relying on automatic detection.

Proxies and Plugin Groups

If the build machine sits behind a corporate firewall, Maven needs proxy settings to reach external repositories:

<proxies>
    <proxy>
        <id>corporate-proxy</id>
        <active>true</active>
        <protocol>http</protocol>
        <host>proxy.example.com</host>
        <port>8080</port>
        <username>proxy-user</username>
        <password>proxy-password</password>
        <nonProxyHosts>localhost|*.example.com</nonProxyHosts>
    </proxy>
</proxies>

The <pluginGroups> element lists additional group IDs that Maven searches when resolving plugin prefixes. For example, adding org.apache.maven.plugins allows running mvn compiler:compile instead of the fully qualified mvn org.apache.maven.plugins:maven-compiler-plugin:compile.

Best Practices

Keep secrets out of version control. Never commit settings.xml containing plain-text passwords to Git or other SCM systems. Use encrypted passwords and restrict file system permissions on the .m2 directory.

Standardize across teams. For organizations with private repositories, architects often distribute a canonical settings.xml template. Team members place it in their .m2 directory to ensure consistent repository URLs, mirror settings, and authentication details.

Validate effective configuration. When dependency downloads fail or Maven behaves unexpectedly, inspect the merged configuration with:

mvn help:effective-settings

This command prints the final settings.xml after merging global and user-level files, making it easier to spot incorrect mirror IDs, missing server credentials, or unintended profile activation.

Use offline mode sparingly. Setting <offline>true</offline> forces Maven to rely exclusively on the local repository. This is useful in air-gapped environments or during travel, but it prevents resolution of new dependencies and plugin updates.

Conclusion

settings.xml is the control center for Maven's interaction with the local machine and remote artifact repositories. Proper configuration of local storage, mirrors, server credentials, proxies, and profiles removes friction from the build process and helps teams maintain consistent environments. By treating settings.xml as a critical piece of infrastructure rather than an afterthought, developers can avoid common dependency issues and keep builds running smoothly.