Gradle更換阿里雲映象源

有悟还有迷發表於2024-07-17

1、下載Gradle

官網:https://gradle.org/releases/

2、配置Gradle環境變數

3、更換映象源

gradle安裝目錄下找到init.d資料夾

再該資料夾中建立init.gradle檔案,編寫一下指令碼替換阿里雲映象

// 阿里雲倉庫和源倉庫地址對比:https://developer.aliyun.com/mvn/guide

gradle.projectsLoaded {
    rootProject.allprojects {
        buildscript {
            repositories {
                def PUBLIC_URL = 'https://maven.aliyun.com/repository/public'
                def CENTER_URL = 'https://maven.aliyun.com/repository/center'
                def GOOGLE_URL = 'https://maven.aliyun.com/repository/google'
                def GRADLE_PLUGIN_URL = 'https://maven.aliyun.com/repository/gradle-plugin/'
                all { ArtifactRepository repo ->
                    if (repo instanceof MavenArtifactRepository) {
                        def url = repo.url.toString()
                        if (url.startsWith('https://jcenter.bintray.com/')) {
                            project.logger.lifecycle "Repository ${repo.url} replaced by $PUBLIC_URL."
                            println("buildscript ${repo.url} replaced by $PUBLIC_URL.")
                            remove repo
                        }
                        else if (url.startsWith('https://dl.google.com/dl/android/maven2/')) {
                            project.logger.lifecycle "Repository ${repo.url} replaced by $GOOGLE_URL."
                            println("buildscript ${repo.url} replaced by $GOOGLE_URL.")
                            remove repo
                        }
                        else if (url.startsWith('https://repo1.maven.org/maven2')) {
                            project.logger.lifecycle "Repository ${repo.url} replaced by $CENTER_URL."
                            println("buildscript ${repo.url} replaced by $CENTER_URL.")
                            remove repo
                        } else if (url.startsWith('https://plugins.gradle.org/m2/')) {
                            project.logger.lifecycle "Repository ${repo.url} replaced by $GRADLE_PLUGIN_URL."
                            remove repo
                        }
                    }
                }
                jcenter {
                    url PUBLIC_URL
                }
                google {
                    url GOOGLE_URL
                }
                maven {
                    url CENTER_URL
                }
            }
        }
        repositories {
            def PUBLIC_URL = 'https://maven.aliyun.com/repository/public'
            def CENTER_URL = 'https://maven.aliyun.com/repository/center'
            def GOOGLE_URL = 'https://maven.aliyun.com/repository/google'
            def GRADLE_PLUGIN_URL = 'https://maven.aliyun.com/repository/gradle-plugin/'
            all { ArtifactRepository repo ->
                if (repo instanceof MavenArtifactRepository) {
                    def url = repo.url.toString()
                    if (url.startsWith('https://jcenter.bintray.com/')) {
                        project.logger.lifecycle "Repository ${repo.url} replaced by $PUBLIC_URL."
                        println("buildscript ${repo.url} replaced by $PUBLIC_URL.")
                        remove repo
                    }
                    else if (url.startsWith('https://dl.google.com/dl/android/maven2/')) {
                        project.logger.lifecycle "Repository ${repo.url} replaced by $GOOGLE_URL."
                        println("buildscript ${repo.url} replaced by $GOOGLE_URL.")
                        remove repo
                    }
                    else if (url.startsWith('https://repo1.maven.org/maven2')) {
                        project.logger.lifecycle "Repository ${repo.url} replaced by $CENTER_URL."
                        println("buildscript ${repo.url} replaced by $CENTER_URL.")
                        remove repo
                    } else if (url.startsWith('https://plugins.gradle.org/m2/')) {
                        project.logger.lifecycle "Repository ${repo.url} replaced by $GRADLE_PLUGIN_URL."
                        remove repo
                    }
                }
            }
            jcenter {
                url PUBLIC_URL
            }
            google {
                url GOOGLE_URL
            }
            maven {
                url CENTER_URL
            }
        }
    }
}

4、idea中更換gradle為本地安裝

相關文章