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

如何从gradle.properties读取VERSION_CODE到Azure DevOps Pipeline YAML

在Azure DevOps Pipeline中读取gradle.properties里的VERSION_CODE变量

当然可以实现,直接在你的PowerShell任务脚本中读取并解析同层级的gradle.properties文件,提取VERSION_CODE的值即可。

修改后的Pipeline任务代码如下:

- task: PowerShell@2
  displayName: Run gradle sonar
  inputs:
    targetType: 'inline'
    pwsh: true
    workingDirectory: ${{ parameters.workingDirectory }}
    script: |
        # 读取gradle.properties文件并提取VERSION_CODE的值
        $gradlePropsContent = Get-Content ./gradle.properties -Raw
        if ($gradlePropsContent -match 'VERSION_CODE\s*=\s*(\d+)') {
            $VERSION_CODE = $matches[1]
        } else {
            throw "未在gradle.properties中找到VERSION_CODE变量"
        }

        # 拼接gradle命令
        $GRADLE_COMMAND = "./gradlew sonar"
        $GRADLE_COMMAND += " -Dsonar.projectVersion=$VERSION_CODE"
        
        # 执行命令
        Invoke-Expression $GRADLE_COMMAND

说明

  • 用Get-Content读取gradle.properties文件内容,通过正则表达式VERSION_CODE\s*=\s*(\d+)匹配并提取版本号(如果版本号包含非数字字符,可调整正则为VERSION_CODE\s*=\s*(.+))
  • 增加了异常判断,如果没找到VERSION_CODE会抛出错误,避免后续命令执行异常
  • 最后用Invoke-Expression执行拼接好的gradle命令

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 03:38:11