gradle - Gradle无法正确执行外部命令

标签 gradle npm

我有以下gradle任务,可使用npm构建前端代码:

task buildFrontend() {
    doLast {
        exec {
            workingDir "src/ui"
            commandLine "npm", "install"
            commandLine "npm", "run", "build"
        }
...
}
但是,当我运行构建时,出现以下错误:
...
> Task :solutions:dash:buildFrontend FAILED

...
> ng build --base-href /p/ --prod

sh: 1: ng: not found
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT
npm ERR! dash@0.0.0 build: `ng build --base-href /p/ --prod`
npm ERR! spawn ENOENT
npm ERR! 
npm ERR! Failed at the dash@0.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?
...

FAILURE: Build failed with an exception.
...

* What went wrong:
Execution failed for task ':solutions:dash:buildFrontend'.
> Process 'command 'npm'' finished with non-zero exit value 1
...
但是,当我cdsrc/ui目录并从终端运行npm install,然后重新运行gradle构建时,它将成功。
似乎Gradle无法执行commandLine "npm", "install"的方式和结果与我从终端手动执行的方式相同。
但为什么?这两个命令-gradle build和manual npm install由同一用户从终端执行...

最佳答案

exec块分成两部分后,如下所示:

exec {
    workingDir "src/ui"
    commandLine "npm", "install"
}
exec {
    workingDir "src/ui"
    commandLine "npm", "run", "build"
}
该应用程序的构建没有错误。

关于gradle - Gradle无法正确执行外部命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63806504/

相关文章:

gradle - 如何在 gradle 测试中禁用断言

javascript - 将 cron 转换为 "simpler"人类可读格式的包?

node.js - 无法安装@angular/cli

windows - npm package.json bin 在 Windows 上不起作用

javascript - package.json 中的 Node 引擎 8.x 或 10.x

android - 自动化gradle从代码构建应用程序包(.apk)

android - 无法合并dex-Android Studio 3.0.1

java - Android gradle processReleaseResources 失败 android-sdk-linux/build-tools/23.0.0_rc3/aapt java.io.IOException

javascript - npm标准版补丁版本问题

android - 将 gradle 版本更新到 5.1.1,但 kapt 不起作用