Gradle当中执行Pnpm执行失败

使用gradle使用npm去构建Vue前端项目时,出现了如下的问题,说启动npm进程失败(感觉像是找不到npm命令)。 Execution failed for task ':pnpmSetup'. > A problem occurred starting process 'command 'np

使用gradle使用npm去构建Vue前端项目时,出现了如下的问题,说启动npm进程失败(感觉像是找不到npm命令)。

Execution failed for task ':pnpmSetup'.
> A problem occurred starting process 'command 'npm''

依次使用npm和node命令,在本地查看,发现均正常。

 ~/Desktop/Code/java/my-project/halo/ [main] node -v
v20.17.0
 ~/Desktop/Code/java/my-project/halo/ [main] npm -v
10.8.2
 ~/Desktop/Code/java/my-project/halo/ [main] pnpm -v
9.9.0
 ~/Desktop/Code/java/my-project/halo/ [main] which npm
/usr/local/bin/npm
 ~/Desktop/Code/java/my-project/halo/ [main] echo $PATH
/Users/jianchaojia/Library/pnpm:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/usr/local/share/dotnet:~/.dotnet/tools:/Users/jianchaojia/Library/Application Support/JetBrains/Toolbox/scripts:/Users/jianchaojia/Desktop/Code/jdk/jdk1.8.0_411.jdk/Contents/Home:/Users/jianchaojia/Desktop/Code/jdk/jdk1.8.0_411.jdk/Contents/Home/bin:/Users/jianchaojia/Desktop/Code/jdk/jdk-21.0.3.jdk/Contents/Home:/Users/jianchaojia/Desktop/Code/jdk/jdk-21.0.3.jdk/Contents/Home/bin:/Users/jianchaojia/Desktop/Code/jdk/graalvm-jdk-22.0.2+9.1/Contents/Home:/Users/jianchaojia/Desktop/Code/jdk/graalvm-jdk-22.0.2+9.1/Contents/Home/bin:/Users/jianchaojia/Desktop/Code/tools/gradle-8.8:/Users/jianchaojia/Desktop/Code/tools/gradle-8.8/bin:/Users/jianchaojia/Desktop/Code/tools/apache-maven-3.9.7:/Users/jianchaojia/Desktop/Code/tools/apache-maven-3.9.7/bin

此时想到了之前的一个问题,怀疑是gradle进程启动时没有读取到环境变量配置信息,怀疑这个问题也是因为没有读取到环境变量的问题。

f9766ac3b44abcab6f0a5c27cd48a693.png

尝试在Groovy脚本当中新增一行(在dependencies当中新增一行就行),打印Gradle获取到的环境变量信息,Groovy脚本当中可以写Java代码,很容易。

    println(System.getenv("PATH"))

打印的结果如下,果然和我们预期的不一样。

 ~/Desktop/Code/java/my-project/halo-backup-plugin/ [main*] ./gradlew build

> Configure project :
/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/usr/local/share/dotnet:~/.dotnet/tools:/Users/jianchaojia/Library/Application Support/JetBrains/Toolbox/scripts:/Users/jianchaojia/Desktop/Code/jdk/jdk1.8.0_411.jdk/Contents/Home:/Users/jianchaojia/Desktop/Code/jdk/jdk1.8.0_411.jdk/Contents/Home/bin:/Users/jianchaojia/Desktop/Code/jdk/jdk-21.0.3.jdk/Contents/Home:/Users/jianchaojia/Desktop/Code/jdk/jdk-21.0.3.jdk/Contents/Home/bin:/Users/jianchaojia/Desktop/Code/jdk/graalvm-jdk-22.0.2+9.1/Contents/Home:/Users/jianchaojia/Desktop/Code/jdk/graalvm-jdk-22.0.2+9.1/Contents/Home/bin:/Users/jianchaojia/Desktop/Code/tools/gradle-8.8:/Users/jianchaojia/Desktop/Code/tools/gradle-8.8/bin:/Users/jianchaojia/Desktop/Code/tools/apache-maven-3.9.7:/Users/jianchaojia/Desktop/Code/tools/apache-maven-3.9.7/bin

> Task :pnpmSetup FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':pnpmSetup'.
> A problem occurred starting process 'command 'npm''

可以使用stop命令停止后台的Gradle进程

./gradlew --stop

接着再去执行build命令进行工程的构建

./gradlew build

发现已经成功构建

BUILD SUCCESSFUL in 3m 46s
40 actionable tasks: 19 executed, 21 up-to-date
Comment