settings.xml, Repositories & Mirrors
The POM describes the project. Some configuration belongs to the machine or the person running the build instead: where the local repository is, which proxy to use, the password for the company repository. That lives in settings.xml, never in the POM, because the POM is committed and shared.
Two settings files
| File | Scope |
|---|---|
${maven.home}/conf/settings.xml |
the Maven installation, every user (global settings) |
~/.m2/settings.xml |
one user; merged over the global file |
-s path/settings.xml |
replaces the user file for one run (common in CI) |
Its main sections:
<settings>
<localRepository>/data/m2</localRepository> <!-- default ~/.m2/repository -->
<offline>false</offline> <!-- like -o -->
<mirrors>…</mirrors> <!-- reroute repositories -->
<servers>…</servers> <!-- credentials, by repository id -->
<proxies>…</proxies> <!-- HTTP proxy -->
<profiles>…</profiles> <!-- settings-level profiles (repositories, properties) -->
<activeProfiles>…</activeProfiles>
</settings>Repositories and mirrors
Out of the box Maven downloads from one repository, Maven Central (id central, from the Super POM). A POM can add more with <repositories>. In companies you almost never want builds to talk to the internet directly: a repository manager (Nexus, Artifactory, a cloud artifact registry) proxies Central, caches everything, and hosts the company's own artifacts. A mirror in settings.xml sends requests for some or all repositories there instead:
<mirrorOf> |
Mirrors |
|---|---|
* |
every repository |
central |
only Maven Central |
external:* |
every repository not on localhost or a file system |
*,!internal |
everything except the repository with id internal |
Here /srv/maven-repo plays the company repository. The session publishes a library org.acme:greet there, deletes it from the local repository, and then builds a project that needs it, first without settings, then with a mirror:
mkdir -p greet/src/main/java/acme && cd greet
cat > pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>org.acme</groupId>
<artifactId>greet</artifactId>
<version>1.0</version>
<properties><maven.compiler.release>21</maven.compiler.release></properties>
</project>
POM
echo 'package acme; public class Greet { public static String hi() { return "hi from the company repository"; } }' > src/main/java/acme/Greet.java
mvn -q -ntp deploy -DaltDeploymentRepository=company::file:///srv/maven-repo
rm -rf ~/.m2/repository/org/acme
mkdir -p ~/use/src/main/java/demo && cd ~/use
cat > pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>use</artifactId>
<version>1.0</version>
<properties><maven.compiler.release>21</maven.compiler.release></properties>
<dependencies>
<dependency><groupId>org.acme</groupId><artifactId>greet</artifactId><version>1.0</version></dependency>
</dependencies>
</project>
POM
echo 'package demo; public class Main { public static void main(String[] a) { System.out.println(acme.Greet.hi()); } }' > src/main/java/demo/Main.java
echo "--- without a mirror"
mvn compile | grep -o 'Could not transfer artifact .*'
cat > ~/.m2/settings.xml <<'XML'
<settings>
<mirrors>
<mirror>
<id>company</id>
<name>Company repository</name>
<url>file:///srv/maven-repo</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>
</settings>
XML
echo "--- with the mirror"
mvn -B compile | grep -E 'Download|BUILD'
java -cp target/classes:$HOME/.m2/repository/org/acme/greet/1.0/greet-1.0.jar demo.Main--- without a mirror
Could not transfer artifact org.acme:greet:pom:1.0 from/to central (https://repo.maven.apache.org/maven2): repo.maven.apache.org: Temporary failure in name resolution
--- with the mirror
[INFO] Downloading from company: file:///srv/maven-repo/org/acme/greet/1.0/greet-1.0.pom
[INFO] Downloaded from company: file:///srv/maven-repo/org/acme/greet/1.0/greet-1.0.pom (267 B at 27 kB/s)
[INFO] Downloading from company: file:///srv/maven-repo/org/acme/greet/1.0/greet-1.0.jar
[INFO] Downloaded from company: file:///srv/maven-repo/org/acme/greet/1.0/greet-1.0.jar (1.7 kB at 828 kB/s)
[INFO] BUILD SUCCESS
hi from the company repository
Without the mirror, Maven tried central (and failed, as this machine has no network). With it, the same request went to company; the project's POM didn't change at all. That's the point: where artifacts come from is a property of the environment, not of the project.
-B (batch mode) prints one Downloading/Downloaded line per file instead of animated progress; CI should always use it (or -ntp, which hides transfer messages completely).
Credentials: <servers>
A repository that needs a login gets its credentials from a <server> whose id matches the repository's or mirror's id. Never put passwords in a POM. Read them from the environment, and inspect the merged result with help:effective-settings (which masks passwords):
mkdir -p ~/.m2 && cat > ~/.m2/settings.xml <<'XML'
<settings>
<mirrors>
<mirror><id>company</id><name>Company repository</name><url>file:///srv/maven-repo</url><mirrorOf>*</mirrorOf></mirror>
</mirrors>
<servers>
<server><id>company</id><username>ci-bot</username><password>${env.REPO_TOKEN}</password></server>
</servers>
</settings>
XML
mvn -q -o org.apache.maven.plugins:maven-help-plugin:3.5.2:effective-settings -Doutput=/dev/stdout | sed -n '/<settings/,$p'
echo "--- the same goal by prefix"
mvn -o help:effective-settings | grep -o 'No plugin found.*' | sed 's/ -> \[Help 1\]//'<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 https://maven.apache.org/xsd/settings-1.2.0.xsd">
<localRepository>/home/learner/.m2/repository</localRepository>
<offline>true</offline>
<servers>
<server>
<username>ci-bot</username>
<password>***</password>
<id>company</id>
</server>
</servers>
<mirrors>
<mirror>
<mirrorOf>*</mirrorOf>
<name>Company repository</name>
<url>file:///srv/maven-repo</url>
<id>company</id>
</mirror>
<mirror>
<mirrorOf>external:http:*</mirrorOf>
<name>Pseudo repository to mirror external repositories initially using HTTP.</name>
<url>http://0.0.0.0/</url>
<blocked>true</blocked>
<id>maven-default-http-blocker</id>
</mirror>
</mirrors>
<pluginGroups>
<pluginGroup>org.apache.maven.plugins</pluginGroup>
<pluginGroup>org.codehaus.mojo</pluginGroup>
</pluginGroups>
</settings>
--- the same goal by prefix
No plugin found for prefix 'help' in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (/home/learner/.m2/repository), company (file:///srv/maven-repo)]
Three lessons in that output:
- The password shows as
***. Maven also supports encrypting passwords insettings.xml(mvn --encrypt-master-password, then--encrypt-password), but in CI an environment variable injected by the CI system's secret store is simpler and safer. - Maven adds a mirror of its own,
maven-default-http-blocker: since Maven 3.8.1, repositories reached over plain HTTP are blocked, because an attacker on the network could swap artifacts. Use HTTPS repositories. - Calling
help:by its prefix failed. Maven maps prefixes to plugins using metadata files cached per repository id (maven-metadata-central.xml); with everything mirrored tocompany, it looks formaven-metadata-company.xml, which this offline machine has never downloaded. With a real repository manager this is invisible. Here, the fullgroupId:artifactId:version:goalform avoids the lookup.
Per-project options: .mvn/
Options a project always needs go into .mvn/maven.config (one command-line option per line, for example -ntp or -Dstyle.color=never) and JVM options into .mvn/jvm.config (-Xmx2g). Both are committed, and every mvn and ./mvnw run in that project picks them up. They are for project-wide defaults, never credentials.
Test yourself
Why do credentials for a repository go into settings.xml rather than the POM?
- The POM can't hold XML
- The POM is committed and shared; settings.xml is per machine/user and stays private
- settings.xml is encrypted automatically
- Maven ignores passwords in POMs
What does <mirrorOf>*</mirrorOf> do?
- Mirrors only Maven Central
- Routes requests for every repository to the mirror's URL
- Copies the local repository
- Disables all repositories
Why does Maven 3.8.1+ block plain-HTTP repositories?
- HTTP is slower
- Artifacts fetched over HTTP can be replaced by an attacker on the network
- HTTP doesn't support authentication
- Maven Central dropped HTTP support for uploads only