# Gradle 如何配置全局 mavenCentral()

**URL:** <https://www.isharkfly.com/t/gradle-mavencentral/14561>\
**Category:** DevOps\
**Tags:** java, gradle\
**Created:** [July 21, 2026, 4:00am UTC](https://www.isharkfly.com/t/gradle-mavencentral/14561 "2026-07-21T04:00:00Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![hex](https://www.isharkfly.com/user_avatar/www.isharkfly.com/hex/32/21953_2.png) [@hex](https://www.isharkfly.com/u/hex)\
**Post date:** [July 21, 2026, 4:00am UTC](https://www.isharkfly.com/t/gradle-mavencentral/14561/1 "2026-07-21T04:00:00Z")

</div>

我们都知道 Gradle 会使用 Maven 的中央仓库。

在 Gradle 的配置文件中，通常有一个 mavenCentral()

 ![517c2fb634ffb86bb5fe5a057f0a1f53cbecefaa_2_690x248](https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/3X/b/8/b8dfc5cf43afe7a37c570dadcbf101a8d7877086.png)  
如果我们想把 mavenCentral() 的仓库地址全局替换掉别的仓库地址的话。

我们可以在 `C:\Users\yhu\.gradle` 目录下创建一个 init.gradle 文件。

文件中的代码为：

```auto
allprojects{
    repositories {
        all { ArtifactRepository repo ->
            println repo.url.toString()
            if ((repo instanceof MavenArtifactRepository) && repo.url.toString().startsWith("https://repo1.maven.org/maven2")) {
                project.logger.warn "Repository ${repo.url} removed. Only $coporateRepoUrl is allowed"
                remove repo
            }   
        }
        maven { 
            url "https://repo.sharkdat.com/repository/maven-public/" 
        }
    }
}

```

 ![706db1d07fe97cab50bb755d8bbec6e3d61ac01f](https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/3X/2/6/261f496259c12d51afc32a0caa137e16e8a5aea2.png)  
上面代码的意思是，在 Gradle 进程启动之前进行初始化，在这个初始化的时候将会用自己的 Maven 仓库地址替换掉远程的 Maven 中央仓库地址。
