我的项目根目录是:
D:/Project/Node_Project
我正在使用 gradle 插件在项目根目录中临时安装 nodejs,以便在构建 thoject 时可以在项目中运行一些 nodejs 命令。插件如下:
plugins {
id "com.github.node-gradle.node" version "2.2.4"
}
node {
download = true
version = "10.10.0"
distBaseUrl = 'https://nodejs.org/dist'
workDir = file("${project.buildDir}/nodejs")
}
因此,nodejs 将安装在项目内的位置:
D:/Project/Node_Project/build/nodejs/node-v10.10.0-win-x64
现在,我正在使用 .execute(String[] "path to set atenvironmentvariable", String path of file to beexecute which is in the project root directory)
方法来运行具有 Node 依赖性的 Windows 命令。代码如下:
cmd = "node connect.js"
def process = cmd.execute(["PATH=${project.projectDir}/build/nodejs/node-v10.10.0-win-x64"],null)
在上面的 .execute 方法中,有没有办法自动填充字符串的 "build/nodejs/node-v10.10.0-win-x64"
部分,而不是将其硬编码到方法?
像这样的东西:
def process = cmd.execute(["PATH=${project.projectDir}/.*"],null)
.execute方法的语法: https://docs.groovy-lang.org/latest/html/groovy-jdk/java/lang/String.html#execute(java.lang.String[],%20java.io.File)
所有代码都在“build.gradle”文件中。请帮忙!
最佳答案
我问你为什么不直接写一个 NodeTask 类型的任务,但我知道您喜欢在后台运行它,但您不能这样做。
您可以列出目录的内容并将其用作命令的一部分。但您也可以从插件提供的扩展中获取它。
这没有记录在案,并且可能会在插件的 future 版本中中断,但您可以执行以下操作(Groovy DSL):
task connectJS {
dependsOn nodeSetup
doFirst {
def connectProcess = "$node.variant.nodeExec $projectDir/src/js/connect.js".execute()
// Blocking readers (if async, pipe to a log file instead)
connectProcess.in.eachLine { logger.info(it) }
connectProcess.err.eachLine { logger.err(it) }
}
}
关于java - 有没有办法在groovy中自动设置字符串中的Windows路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62382667/