Configure Multiple Maven Repositories and Mirrors for Faster Dependency Management
This guide explains why the default Maven central repository is slow in China, how to use Alibaba and other domestic mirrors, and provides step‑by‑step XML configurations for single and multiple repositories, profile activation, and project‑level repository definitions.
When managing jar dependencies with Maven, developers in China often face four problems: the default remote central mirror is very slow, Alibaba Cloud mirrors are used as a replacement, some jars are missing from Alibaba mirrors, and both private and public repositories need to be used simultaneously. To solve these issues, Maven must be configured to support multiple repositories.
Single Repository Configuration
For a single repository, add a mirror entry to the global settings.xml. The following example configures Alibaba Cloud as the mirror for the central repository:
<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>The mirrorOf element determines which artifacts are fetched from this mirror. Using * matches all artifacts, while specific names refer to particular repository IDs. Other attributes are:
id : unique identifier of the mirror.
name : human‑readable description.
url : the mirror address.
mirrorOf : rule that decides when the mirror is used (e.g., *, external:*, repo,repo1, *,!repo1).
Multiple Repository Configuration
Adding several mirrors directly under mirrors does not work. Instead, define multiple profile entries under the profiles node and activate the desired profiles.
<profiles>
<profile>
<id>boundlessgeo</id>
<repositories>
<repository>
<id>boundlessgeo</id>
<url>https://repo.boundlessgeo.com/main/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled><updatePolicy>always</updatePolicy></snapshots>
</repository>
</repositories>
</profile>
<profile>
<id>aliyun</id>
<repositories>
<repository>
<id>aliyun</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled><updatePolicy>always</updatePolicy></snapshots>
</repository>
</repositories>
</profile>
<profile>
<id>maven-central</id>
<repositories>
<repository>
<id>maven-central</id>
<url>http://central.maven.org/maven2/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled><updatePolicy>always</updatePolicy></snapshots>
</repository>
</repositories>
</profile>
</profiles>Activate the profiles by adding an activeProfiles section:
<activeProfiles>
<activeProfile>boundlessgeo</activeProfile>
<activeProfile>aliyun</activeProfile>
<activeProfile>maven-central</activeProfile>
</activeProfiles>When using an IDE such as IntelliJ IDEA, the activated profiles appear in the Maven tool window, and you can select the desired profile during packaging. From the command line, the profile is chosen with the -P option, e.g., mvn -Paliyun ….
If the id of the Alibaba repository is set to central, it overrides Maven’s default remote repository. Alternatively, you can keep the default central ID and configure a mirror with mirrorOf set to central to achieve the same effect.
Project‑Level Repository Configuration
To add multiple repositories directly in a project, edit the project's pom.xml and insert repository entries under the repositories node:
<!-- Custom Maven repository -->
<repositories>
<repository>
<id>central-repo1</id>
<name>Maven Repository Switchboard</name>
<url>http://repo1.maven.org/maven2/</url>
<layout>default</layout>
<releases><enabled>true</enabled></releases>
</repository>
</repositories>The id used here must match a corresponding mirror entry in settings.xml so that Maven knows which mirror to apply:
<mirror>
<id>central</id>
<name>Maven Repository Switchboard</name>
<url>https://repo1.maven.org/maven2/</url>
<mirrorOf>central-repo1</mirrorOf>
</mirror>Useful Domestic Mirrors
Below are several commonly used Chinese Maven mirrors that can be added to settings.xml:
<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/mvn/view</url>
<mirrorOf>central</mirrorOf>
</mirror>
<mirror>
<id>jboss-public-repository-group</id>
<mirrorOf>central</mirrorOf>
<name>JBoss Public Repository Group</name>
<url>http://repository.jboss.org/nexus/content/groups/public</url>
</mirror>
<mirror>
<id>ibiblio</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>https://maven.aliyun.com/mvn/view</url>
</mirror>
<mirror>
<id>central</id>
<name>Maven Repository Switchboard</name>
<url>http://repo1.maven.org/maven2/</url>
<mirrorOf>central</mirrorOf>
</mirror>
<mirror>
<id>repo2</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://repo2.maven.org/maven2/</url>
</mirror>
</mirrors>Properly configuring Maven mirrors and repositories speeds up dependency resolution and makes development more convenient.
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.
Senior Brother's Insights
A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.
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.
