我有以下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
...
但是,当我cd
到src/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/