gradle全局替换国内源方案

一、init.gradle

创建init.gradle放在GRADLE_USER_HOME目录下

allprojects {
    repositories {
        all { ArtifactRepository repo ->
            if (repo instanceof MavenArtifactRepository) {
                def url1 = repo.url.toString()
                def ALIYUN_REPOSITORY_URL = 'https://maven.aliyun.com/repository/public'
                def ALIYUN_GOOGLE_URL = 'https://maven.aliyun.com/repository/google'
                def ALIYUN_GRADLE_PLUGIN_URL = 'https://maven.aliyun.com/repository/gradle-plugin'

                def repositoriesToReplace = [
                        'https://repo1.maven.org/maven2/': ALIYUN_REPOSITORY_URL,
                        'https://jcenter.bintray.com/': 'https://maven.aliyun.com/repository/jcenter', // 注意:jcenter已停止服务
                        'https://dl.google.com/dl/android/maven2/': ALIYUN_GOOGLE_URL,
                        'https://plugins.gradle.org/m2/': ALIYUN_GRADLE_PLUGIN_URL
                ]

                repositoriesToReplace.each { defaultUrl, aliyunUrl ->
                    if (url1.startsWith(defaultUrl)) {
                        project.logger.lifecycle "Repository ${repo.url} replaced by $aliyunUrl."
                        remove(repo)
                        project.logger.lifecycle "maven ${url} $aliyunUrl."
                        maven { url aliyunUrl}
                    }
                }
            }
        }
    }
}

另一种实现

fun RepositoryHandler.enableMirror() {
    all {
        if (this is MavenArtifactRepository) {
            val originalUrl = this.url.toString().removeSuffix("/")
            urlMappings[originalUrl]?.let {
                logger.lifecycle("Repository[$url] is mirrored to $it")
                this.setUrl(it)
            }
        }
    }
}

val urlMappings = mapOf(
    "https://repo.maven.apache.org/maven2" to "https://mirrors.tencent.com/nexus/repository/maven-public/",
    "https://dl.google.com/dl/android/maven2" to "https://mirrors.tencent.com/nexus/repository/maven-public/",
    "https://plugins.gradle.org/m2" to "https://mirrors.tencent.com/nexus/repository/gradle-plugins/"
)

gradle.allprojects {
    buildscript {
        repositories.enableMirror()
    }
    repositories.enableMirror()
}

gradle.beforeSettings { 
    pluginManagement.repositories.enableMirror()
    dependencyResolutionManagement.repositories.enableMirror()
}

脚本地址

二、gradle.properties

创建gradle.properties放在GRADLE_USER_HOME目录下

# gradle.properties 示例配置

# 使用阿里云 Maven 镜像
repositories.grails.default = https://maven.aliyun.com/repository/public
repositories.grails.default.1 = https://mirrors.tuna.tsinghua.edu.cn/maven/repos/public
repositories.grails.default.2 = https://repo.maven.apache.org/maven2
# 设置阿里云的 Maven 仓库作为镜像
systemProp.maven.repo.local=https://maven.aliyun.com/repository/public
# 设置阿里云的 JCenter 仓库作为镜像
systemProp.jcenter.url=https://maven.aliyun.com/repository/jcenter
# 设置阿里云的 Google Maven 仓库作为镜像
systemProp.google.maven.repo.url=https://maven.aliyun.com/repository/google
# 设置阿里云的 Gradle 插件仓库作为镜像
systemProp.gradle.plugin.repo.url=https://maven.aliyun.com/repository/gradle-plugin
# 其他可能的配置
#systemProp.http.proxyHost=127.0.0.1
#systemProp.http.proxyPort=1080
#systemProp.https.proxyHost=127.0.0.1
#systemProp.https.proxyPort=1080
# 使用清华大学的 Maven 镜像
# repositories.grails.default = https://mirrors.tuna.tsinghua.edu.cn/maven/repos/public
# 使用中科大的 Maven 镜像
#repositories.grails.default = https://mirrors.ustc.edu.cn/maven/

三、项目使用方式

可能出错时使用吧

maven {
           allowInsecureProtocol = true
           url = "https://maven.aliyun.com/repository/central"
       }
       maven {
           allowInsecureProtocol = true
           url =  "https://mirrors.huaweicloud.com/repository/maven/"
       }
       maven {
           allowInsecureProtocol = true
           url =  "https://maven.aliyun.com/repository/google"
       }
       maven {
           allowInsecureProtocol = true
           url = "https://maven.aliyun.com/repository/central"
       }
       maven {
           allowInsecureProtocol = true
           url = "https://maven.aliyun.com/repository/gradle-plugin"
       }

其他参考
国内源
其他汇总
方式1

总结

我也不知道哪样管用, 先用着

### IntelliJ IDEA 全局替换完成后的常见问题及解决方案 #### 1. 替换后编译错误 全局替换可能会引入新的语法错误或不兼容的代码片段。建议在执行替换操作后立即运行项目的构建工具,如 Maven 或 Gradle 来验证是否有任何编译错误。 ```bash ./gradlew build ``` 如果发现编译失败,可以利用版本控制系统(VCS),比如 Git 的 `git diff` 命令查看具体修改位置并修正这些问题[^1]。 #### 2. 测试覆盖率下降 当进行了大规模的字符串替换时,某些测试可能不再覆盖到被更改的方法或类。这可以通过重新运行单元测试套件来检测: ```java public class TestRunner { @Test public void runAllTests() throws Exception { Result result = JUnitCore.runClasses(TestSuite.class); for (Failure failure : result.getFailures()) { System.out.println(failure.toString()); } assertTrue(result.wasSuccessful()); } } ``` 确保所有的测试都通过,并且保持良好的测试覆盖率水平[^2]。 #### 3. 配置文件中的路径变化未同步更新 对于涉及资加载路径或其他配置项的情况,在码中做了相应调整之后还需要检查相关的 XML、YAML 等格式的配置文档是否也需要做出相应的改动[^3]。 #### 4. 数据库迁移脚本遗漏 如果有数据库表结构定义或者 SQL 脚本存在于项目当中,则需要注意这些地方也可能存在需要一并处理的内容。特别是 ORM 映射关系的变化要特别小心对待,以免造成数据丢失的风险。 #### 5. 文档和注释的一致性维护 除了实际业务逻辑外,API 接口说明以及内部使用的函数级注解都应该随着代码变更而得到及时更新,以保证团队成员之间的沟通顺畅无误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值