【问题标题】:Run go command as a gradle task将 go 命令作为 gradle 任务运行
【发布时间】:2021-01-11 22:26:52
【问题描述】:

我使用 gradle 作为 terraform 项目的构建工具。我确实在 ..test/... 文件夹下为项目编写了单元测试。我在本地运行测试的方式只是在命令行go test ..test/..' 上,它将在测试文件夹下运行所有​​tests。我想把它集成到构建中,这样每次构建都会运行这个命令'go test ..test/..',我如何在gradle中实现这个。可以使用自定义任务来运行go 命令吗?

我正在尝试做类似以下的事情

task testExec(type: Exec) {
    workingDir "${buildDir}/test"
    commandLine 'go','test'
} doLast {
    println "Test Executed!"
}

但我得到了错误

> A problem occurred starting process 'command 'go''

为了它的价值,我尝试了其他命令并为 ex 得到相同的错误

task testExec(type: Exec) {
     workingDir "${buildDir}/test"
     commandLine 'echo','${}buildDir'
} doLast {
     println "Test Executed!"
}

给出类似的错误

> A problem occurred starting process 'command 'echo''

【问题讨论】:

    标签: go gradle terraform terratest


    【解决方案1】:

    您可以使用gradle plugin。首先可以关注starting guide添加插件:

    plugins {
        id 'com.github.blindpirate.gogradle' version '0.11.4'
    }
    
    golang {
        packagePath = 'github.com/your/package' // go import path of project to be built, NOT local file system path!
    }
    

    然后你可以运行following command来执行所有遵循文件名约定<name>_test.go的go文件:

    gradlew goTest 
    

    否则您也可以创建complete custom taskcustom task with the plugin

    编辑

    我找到了您的错误的原因。变量buildDir 引用您项目中的build 文件夹:<project_folder>/build。现在的问题是文件夹test不存在,抛出异常。相反,您可以使用变量projectDir

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-25
      • 2015-03-06
      • 1970-01-01
      • 2020-10-04
      • 1970-01-01
      • 2022-10-25
      • 2014-04-22
      • 2021-12-20
      相关资源
      最近更新 更多