如何向Gradle中bootRun依赖的npmInstall任务传递参数
解决Gradle bootRun时npmInstall携带参数的问题
方法1:动态传递参数(推荐,支持灵活切换参数)
修改你的build.gradle(或build.gradle.kts)文件,为npmInstall任务添加外部参数支持:
Groovy版本(build.gradle)
plugins { id 'org.springframework.boot' id 'com.github.node-gradle.node' } // 配置npmInstall任务接收外部传递的参数 npmInstall { def npmArgs = project.findProperty('npm.install.args') ?: '' if (npmArgs) { args(npmArgs.split(' ')) } }
Kotlin版本(build.gradle.kts)
plugins { id("org.springframework.boot") id("com.github.node-gradle.node") } tasks.npmInstall { val npmArgs = project.findProperty("npm.install.args")?.toString() ?: "" if (npmArgs.isNotEmpty()) { args(npmArgs.split(" ")) } }
之后在Windows命令行(CMD/PowerShell)执行:
./gradlew bootRun -Pnpm.install.args="--legacy-peer-deps" # 如需使用--force参数,替换即可 ./gradlew bootRun -Pnpm.install.args="--force"
方法2:固定参数(无需每次命令行传递)
如果不需要频繁切换参数,直接在构建脚本中给npmInstall任务固定添加参数:
Groovy版本
npmInstall { args '--legacy-peer-deps' // 替换成--force即可切换为强制安装模式 }
Kotlin版本
tasks.npmInstall { args("--legacy-peer-deps") }
完成后直接运行./gradlew bootRun,npmInstall任务就会自动携带指定参数执行npm install。
原命令无效的原因
./gradlew bootRun npm_install_--force的写法不符合Gradle的参数传递规则,Gradle不支持通过给任务名追加后缀的方式传递参数,必须通过**项目属性(-P参数)**或直接修改任务配置来实现。
内容的提问来源于stack exchange,提问作者Argentum
相关产品推荐
相关产品推荐

