正在运行带有环境变量的Gradle任务

问题描述 投票:0回答:1

我有以下Gradle任务:

task runCli(type: JavaExec) {
    group = "Execution"
    description = "Run the CLI tool"
    classpath = sourceSets.main.runtimeClasspath
    main = "commandline.CommandLineToolKt"
}

然后,在项目根文件夹中,我运行:

export MONGODB_HOST=localhost && ./gradlew runCli

在我的代码中,我有:

System.getenv("MONGODB_HOST")

当我运行该应用程序时,我得到:

Exception in thread "main" java.lang.IllegalStateException: System.getenv("MONGODB_HOST") must not be null
java kotlin gradle
1个回答
0
投票
根据Gradle docs

使用-D命令行选项,您可以将系统属性传递给运行Gradle的JVM。 gradle命令的-D选项具有与java命令的-D选项相同。

更改您的命令行以使用:

./gradlew -DMONGODB_HOST=localhost runCli

© www.soinside.com 2019 - 2024. All rights reserved.