2024-01-07
藏龙卧虎
00
请注意,本文编写于 202 天前,最后修改于 23 天前,其中某些信息可能已经过时。

目录

前言
settings.xml实例
可能的报错
IDEA 灵活切换

前言

开发Java项目时,Maven是重要的项目管理工具,有时Maven需要配置多数据源,本文就来记录一下通过profiles配置Maven多数据源。

settings.xml实例

以下为 settings.xml 的全部内容,该配置文件中,设置了两个仓库镜像源,分别是 阿里云仓库 和 Maven 中央仓库。

实例使用的 Maven 版本 3.9.0,其他版本一样配置,可复制主要配置内容使用。

xml
<?xml version="1.0" encoding="UTF-8"?> <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>E:\dependencies\maven</localRepository> <pluginGroups> </pluginGroups> <proxies> </proxies> <servers> </servers> <mirrors> <mirror> </mirror> </mirrors> <profiles> <profile> <!-- id --> <id>aliyun</id> <repositories> <repository> <!-- id --> <id>aliyun</id> <!-- 仓库的url地址 --> <url>https://maven.aliyun.com/repository/public</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> </repository> </repositories> </profile> <profile> <!-- id --> <id>maven-center</id> <repositories> <repository> <!-- id --> <id>maven-center</id> <!-- 仓库url --> <url>https://repo.maven.apache.org/maven2/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> </repository> </repositories> </profile> </profiles> <activeProfiles> <activeProfile>aliyun</activeProfile> <activeProfile>maven-center</activeProfile> </activeProfiles> </settings>

可能的报错

直接使用上面的配置,有可能会因为一些情况报缺失 mirror 的错误,如果出现错误,可以随便填写一个mirror,不会影响多数据源的使用,下面是最新下载的 Maven 默认的 mirror 示例,也是可以的:

xml
<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>

IDEA 灵活切换

在IDEA中设置Maven的配置文件为以上文件时,你可以再Maven面板中看到以下内容:

maven

此时可以通过勾选来启用或停用指定仓库。

如果对你有用的话,可以打赏哦
打赏
ali pay
wechat pay

本文作者:DingDangDog

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!