gradle - Gradle 无法正确执行外部命令
问题描述
我有以下 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 build 它将成功。commandLine "npm", "install"
Gradle 的执行方式和结果似乎
与我从终端手动执行的方式不同。
但为什么?这两个命令 - gradle build 和 manualnpm install
都由终端上的同一用户执行......
解决方案
在我将exec
块分成两部分后,如下所示:
exec {
workingDir "src/ui"
commandLine "npm", "install"
}
exec {
workingDir "src/ui"
commandLine "npm", "run", "build"
}
该应用程序已构建没有错误。
推荐阅读
- python - Pymongo 查找值在两个 mongo 索引值之间的所有 Mongo 记录
- reactjs - next js - 为什么我刷新页面时显示找不到消息?
- python - 如何在 python selenium webdriver 中使一个类适用于 infinte
- node.js - 如何使用 NodeJS express 框架从 MongoDB 中搜索关键字(错误:MongoDB schema.index 不是函数)
- c# - 为什么通过存储桶中的实体框架从数据库中获取大量数据时进程内存会增加
- php - Docker 上的 Nginx + PHP 提供静态文件
- flutter-layout - 如何在android studio中打开flutter现有项目?我在那里向您展示如何成功
- ios - TestFlight React Native Expo 在启动时崩溃
- docker - BrowserSync 在使用 Docker 时不断重新加载
- php - Woocommerce - 隐藏特定产品类别的产品图片