学而实习之 不亦乐乎

为 Gradle 和 maven 配置国内镜像

2023-09-28 08:12:23

一、配置 Gradle

方法一:项目级配置(推荐)

在项目的根目录下找到如下文件:build.gradle,打开编辑,修改如下代码:

在 apply 上面,添加如下代码:

repositories {
    maven {
        url 'https://maven.aliyun.com/repository/public/'
    }
    maven {
        url 'https://maven.aliyun.com/repository/spring/'
    }
    mavenLocal()
    mavenCentral()
}

方法二:全局配置

在GRADLE_HOME/init.d/目录下新建文件:init.gradle:

allprojects {
    repositories {
 
        mavenLocal()

        maven { url 'https://maven.aliyun.com/repository/public/' }
        maven { url 'https://maven.aliyun.com/repository/spring/'}
        maven { url 'https://maven.aliyun.com/repository/google/'}
        maven { url 'https://maven.aliyun.com/repository/gradle-plugin/'}
        maven { url 'https://maven.aliyun.com/repository/spring-plugin/'}
        maven { url 'https://maven.aliyun.com/repository/grails-core/'}
        maven { url 'https://maven.aliyun.com/repository/apache-snapshots/'}
        
        mavenCentral()
    }
}

二、配置 maven

【idea自带maven插件路径:C:\Program Files\JetBrains\IntelliJ IDEA 2019.1\plugins\maven\lib\maven3\conf

C:\Users\{USER}\.m2\settings.xml

<mirrors>
<mirror>
    <id>aliyun-public</id>
    <mirrorOf>*</mirrorOf>
    <name>aliyun public</name>
    <url>https://maven.aliyun.com/repository/public</url>
</mirror>

</mirrors>
<profile>
    <repositories>
    <repository>
        <id>google</id>
        <url>https://maven.aliyun.com/repository/google</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>

    <repository>
        <id>gradle-plugin</id>
        <url>https://maven.aliyun.com/repository/gradle-plugin</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>

    <repository>
        <id>spring</id>
        <url>https://maven.aliyun.com/repository/spring</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>

    <repository>
        <id>spring-plugin</id>
        <url>https://maven.aliyun.com/repository/spring-plugin</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>

    <repository>
        <id>grails-core</id>
        <url>https://maven.aliyun.com/repository/grails-core</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository> 
    </repositories>
</profile>