Maven settings.xml mirrors: send every build through one repository
Serving developers since 2016 Founder-run, no venture capital MFA, SSO and audit log on every plan
Every feature on every plan, with unlimited users. 14 days free. No credit card required.
Start free trialStarting at $99/month billed annually.
A <mirror> entry in settings.xml tells Maven to fetch a repository’s files from a different URL. Set <mirrorOf>*</mirrorOf>, and every download your builds make goes through one repository.
Where settings.xml lives
Maven reads two settings.xml files and merges them. When both set the same option, your user file wins.
| File | Path |
|---|---|
| User settings | ~/.m2/settings.xml (${user.home}/.m2/settings.xml) |
| Global settings | conf/settings.xml inside the Maven install (${maven.home}/conf/settings.xml) |
To point a single build at another file, pass -s path/to/settings.xml for user settings or -gs path/to/settings.xml for global settings. In CI, commit a settings file to the repository and pass -s .mvn/settings.xml so every job gets the same settings.
Here is a minimal file that sends everything to one repository:
<settings> <mirrors> <mirror> <id>internal</id> <name>Internal repository</name> <url>https://repo.example.com/maven/</url> <mirrorOf>*</mirrorOf> </mirror> </mirrors></settings>There is one condition from Maven’s docs: that repository “must contain all of the desired artifacts, or be able to proxy the requests to other repositories.”
What mirrorOf matches
<mirrorOf> matches repository ids, not URLs. Maven Central’s id is central.
<mirrorOf> |
Sends this through the mirror |
|---|---|
central |
Maven Central only. Repositories declared in your POMs still go direct. |
* |
Every repository, including ones declared in POMs. |
external:* |
Everything except repositories on localhost or file: URLs. |
external:http:* |
Every repository using plain HTTP, except localhost (Maven 3.8.0 and later). |
repo1,repo2 |
Exactly those ids. |
*,!repo1 |
Everything except repo1. |
When you define more than one mirror, two rules decide which one wins:
- A repository gets at most one mirror. Maven picks one instead of combining them.
- Maven checks for an exact id match first. If none exists, it takes the first mirror in the file whose pattern matches. With wildcards, the order of your
<mirror>entries matters.
Credentials: a server with the same id
If your mirror requires a login, add a <server> block with the same <id> as the mirror. Pull the credentials from environment variables so the file holds no secrets:
<settings> <servers> <server> <id>internal</id> <username>${env.MAVEN_USERNAME}</username> <password>${env.MAVEN_TOKEN}</password> </server> </servers> <mirrors> <mirror> <id>internal</id> <url>https://repo.example.com/maven/</url> <mirrorOf>*</mirrorOf> </mirror> </mirrors></settings>“Blocked mirror for repositories”
Starting in Maven 3.8.1, the default conf/settings.xml that ships with Maven includes this mirror:
<mirror> <id>maven-default-http-blocker</id> <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></mirror>It catches any repository using plain http://. If a POM anywhere in your dependency tree still names an HTTP repository, your build fails with Blocked mirror for repositories: [...], showing the repository id and URL. Maven’s two fixes:
- Move to a dependency version whose POM uses an
https://repository URL. - Stay on that version, and define your own mirror for that repository id with an
https://URL.
Check it
mvn help:effective-settingsThis prints the merged settings from both files that Maven will actually use. Passwords stay hidden unless you add -DshowPasswords=true. Run a build next and check the download lines. Every one should name your mirror’s id and URL, as Downloading from internal: followed by your mirror’s URL.
Offline builds. mvn dependency:go-offline resolves your project’s dependencies, plugins, and reports so a later mvn -o package can build without the network. Run that offline build once to verify nothing is missing before relying on it.
This is where CloudRepo helps
Point your mirror at a CloudRepo Maven group with two members: your hosted repository and a proxy of Maven Central. One <mirrorOf>*</mirrorOf> URL then serves both your own artifacts and Central’s. The first time a build requests a file from Central, CloudRepo fetches it and keeps a copy. Every build after that gets it from us, putting in place the caching Sonatype asks heavy users of Maven Central to run.
<settings> <servers> <server> <id>cloudrepo</id> <username>you@example.com</username> <password>${env.CLOUDREPO_TOKEN}</password> </server> </servers> <mirrors> <mirror> <id>cloudrepo</id> <mirrorOf>*</mirrorOf> <url>https://your-org.mycloudrepo.io/repositories/maven-public</url> </mirror> </mirrors></settings>More: Pull through a Maven group, Fix Maven Central 429 Too Many Requests, Proxy repositories.
Every feature on every plan, with unlimited users
14 days free. No credit card required.
Starting at $99/month billed annually.
Founder-run since 2016, with no venture capital. A real person answers within one business day. Usually it's me.
Chris, founder of CloudRepo