Master Maven Mirrors: Configure Repositories, Mirrors, and Private Deployments
This guide explains how Maven repositories and mirrors work, the role of the mirrorOf attribute, how to set up central and third‑party repositories in pom.xml, configure mirrors in settings.xml with various matching rules, and properly configure private repository deployment with authentication and profile activation.
When developing microservice‑based projects, publishing third‑party packages to an internal private Maven repository is essential for iterative development.
Mirror URLs can replace repository URLs; the mirror configuration provides the repository address, while mirrorOf defines the replacement rule.
1. In pom.xml you can configure repository addresses with the <repositories> element. If none is defined, Maven uses the default central repository (id=central, https://repo.maven.apache.org/maven2/).
<repositories>
<repository>
<id>repo</id>
<url>https://repo.maven.apache.org/maven2/</url>
</repository>
</repositories>2. Maven resolves dependencies from top to bottom, so configure the central repository first and then third‑party repositories to avoid missing or slow downloads.
3. Mirrors are configured in settings.xml. The id and url define the mirror address, while mirrorOf specifies which repositories it replaces. Common rules are:
* – replace all repositories; the repositories defined in the pom become ineffective.
central – replace only the central repository.
*,!repo – replace all repositories except the one named repo.
<mirrors>
<mirror>
<id>aliyunmaven</id>
<name>阿里云公共仓库</name>
<url>https://maven.aliyun.com/repository/public</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>If a repository requires authentication, add a <server> entry with username and password in settings.xml.
4. When using a private repository, configure distributionManagement with separate release and snapshot repositories, for example:
<distributionManagement>
<repository>
<id>release</id>
<url>https://release‑maven_1/</url>
</repository>
<snapshotRepository>
<id>snapshot</id>
<url>https://snapshot‑maven_1/</url>
</snapshotRepository>
</distributionManagement>Two typical scenarios:
Only certain modules are uploaded to the private repository; the parent pom defines the release artifact, and the id must match the one in settings.xml.
All artifacts are uploaded to the private repository directly via settings.xml profiles.
<profiles>
<profile>
<id>rdc</id>
<repositories>
<repository>
<id>release</id>
<url>https://release‑maven_1/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>snapshot</id>
<url>https://snapshot‑maven_1/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<activeProfiles>
<activeProfile>rdc</activeProfile>
</activeProfiles>
</profile>
</profiles>Ultimately, configure the private repository address and activate the corresponding profile.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Lin is Dream
Sharing Java developer knowledge, practical articles, and continuous insights into computer engineering.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
