Solve the timeout problem of gradle download jar package

Error report

insert image description here

Original init.gradle configuration

allprojects{
    repositories {
        maven { url 'https://maven.aliyun.com/repository/public/' }
        maven { url 'https://maven.aliyun.com/repository/jcenter/' }
        maven { url 'https://maven.aliyun.com/repository/central/' }
        maven { url 'https://maven.aliyun.com/repository/google/' }
        maven { url 'https://maven.aliyun.com/repository/gradle-plugin/' }
    }
}

solution

The Maven public product library is overseas. Although it is configured to use Alibaba Cloud mirroring, it still checks overseas resources when pulling the jar package, so modify the configuration:

def repoConfig = {
    all { ArtifactRepository repo ->
        if (repo instanceof MavenArtifactRepository) {
            def url = repo.url.toString()
            if (url.contains('repo1.maven.org/maven2')
            	|| url.contains('repo.maven.org/maven2')
            	|| url.contains('repo.maven.apache.org/maven2')
                || url.contains('jcenter.bintray.com')
                || url.contains('maven.google.com')
                || url.contains('plugins.gradle.org/m2')
                || url.contains('repo.spring.io/libs-milestone')
                || url.contains('repo.spring.io/plugins-release')
                || url.contains('repo.grails.org/grails/core')
                || url.contains('repository.apache.org/snapshots')
            ) {
                println "gradle init: [buildscript.repositories] (${repo.name}: ${repo.url}) removed"
                remove repo
            }
        }
    }
 
    // Maven 镜像聚合了:central、jcenter、google、gradle-plugin
    maven { url 'https://maven.aliyun.com/repository/central' }
    maven { url 'https://maven.aliyun.com/repository/jcenter' }
    maven { url 'https://maven.aliyun.com/repository/google' }
    maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
    maven { url 'https://maven.aliyun.com/repository/public/' }
}
 
allprojects {
    buildscript {
        repositories repoConfig
    }
    repositories repoConfig
}

Precautions

  1. maven { url 'https://maven.aliyun.com/repository/central' } must be placed first, and there is a dependency order.
  2. The repositories in setting.gradle and build.gradle should also be configured as maven { url 'https://maven.aliyun.com/repository/gradle-plugin' } to replace the official one to avoid timeout.
  3. The pluginManagement in setting.gradle is added as follows:
repositories {
        maven {
            url 'https://maven.aliyun.com/repository/gradle-plugin'
        }
}
  1. The gradle-wrapper.properties configuration should use the domestic address instead of the official address to download gradle, otherwise it will also time out.
distributionUrl=https\://mirrors.cloud.tencent.com/gradle/gradle-7.3-bin.zip

Guess you like

Origin blog.csdn.net/qq_38685503/article/details/130323623