我有以下Gradle Exec任务,使用经典的个人访问令牌(启用所有repo
范围)从GitHub下载文件:
tasks.register<Exec>("downloadScript") {
val cmdLine = mutableListOf<String>("curl", "-H", "'Authorization: Bearer ${gh_token}'", "-H", "'Accept: application/vnd.github.raw'", "--remote-name", "-L", "https://api.github.com/repos/my_org/my_repo/contents/scripts/script.py")
commandLine(cmdLine)
}
当我运行这个任务时,我得到了这样的输出:
> Task :downloadScript FAILED
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 123 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
curl: (22) The requested URL returned error: 404
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':downloadScript'.
> Process 'command 'curl'' finished with non-zero exit value 22
这里最有趣的部分是,在终端(IntelliJ本身或MacOS终端)中运行相同的命令通过,文件被下载,同时将${gh_token}替换为真实的的令牌。curl -H 'Authorization: Bearer token_string' -H 'Accept: application/vnd.github.raw' --remote-name -L https://api.github.com/repos/my_org/my_repo/contents/scripts/script.py
为什么使用变量将字符串传递给commandline()会导致命令行插值失败,导致它失败?
1条答案
按热度按时间stszievb1#
对于任何人到达这里,我已经找到了解决方案,这是非常简单的。
Authorization
和Accept
头文件周围的'[header]'(撇号)会扰乱commandLine()
的插值。一旦我删除它们,一切都如预期的那样工作。
这是正确的任务: