You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gradle Wrapper distributionUrl多环境配置及gradle-release插件提交问题

解决Gradle Wrapper本地与Jenkins不同distributionUrl的方案

方案1:Jenkins流水线临时修改配置(推荐)

  • 代码仓库中保留指向官方仓库的gradle-wrapper.properties,不要提交公司内部地址的版本
  • 在Jenkins流水线的构建前置步骤中,添加Shell命令替换配置:
    # 替换为你的Gradle版本和公司仓库地址
    sed -i 's|https://services.gradle.org/distributions/gradleXX-all.zip|https://my-company.com/gradle/gradleXX.zip|' gradle/wrapper/gradle-wrapper.properties
    
  • 该修改仅作用于当前Jenkins构建环境,不会提交到代码仓库,本地开发依然使用官方地址,完全隔离

方案2:利用本地属性文件覆盖

  • 在项目根目录创建gradle-local.properties,将其加入.gitignore,避免提交到仓库,内容如下:
    org.gradle.wrapper.distributionUrl=https://services.gradle.org/distributions/gradleXX-all.zip
    
  • 在项目根目录的settings.gradle中添加以下逻辑,构建时自动读取本地属性并覆盖Wrapper配置:
    def localPropsFile = new File(rootDir, "gradle-local.properties")
    if (localPropsFile.exists()) {
        def props = new Properties()
        localPropsFile.withInputStream { props.load(it) }
        def wrapperPropsFile = new File(rootDir, "gradle/wrapper/gradle-wrapper.properties")
        def wrapperProps = new Properties()
        wrapperPropsFile.withInputStream { wrapperProps.load(it) }
        
        def localDistUrl = props.getProperty("org.gradle.wrapper.distributionUrl")
        if (localDistUrl && !localDistUrl.equals(wrapperProps.getProperty("distributionUrl"))) {
            wrapperProps.setProperty("distributionUrl", localDistUrl)
            wrapperPropsFile.withOutputStream {
                wrapperProps.store(it, "Updated by local properties")
            }
        }
    }
    
  • 代码仓库中gradle-wrapper.properties配置为公司内部地址,本地开发时会自动替换为官方地址,注意不要误提交修改后的gradle-wrapper.properties

方案3:使用Gradle Init脚本全局覆盖

  • 本地环境:在用户目录下创建~/.gradle/init.d/wrapper-override.gradle,内容:
    wrapper {
        distributionUrl = "https://services.gradle.org/distributions/gradleXX-all.zip"
    }
    
  • Jenkins服务器:在Jenkins使用的Gradle配置目录下创建相同名称的init脚本,内容替换为公司内部地址:
    wrapper {
        distributionUrl = "https://my-company.com/gradle/gradleXX.zip"
    }
    
  • Gradle启动时会优先执行init脚本中的配置,覆盖仓库里的gradle-wrapper.properties设置,无需修改项目文件

额外注意事项

  • 若使用gradle-release插件,确保在本地执行release任务时,提交的是官方地址的gradle-wrapper.properties,避免Jenkins构建时的冲突
  • 所有方案中,替换的Gradle版本号需保持一致,避免本地与Jenkins构建环境版本不一致引发问题

内容的提问来源于stack exchange,提问作者brtk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 19:52:43