如何通过Intellij Idea将环境变量传递到Gradle测试任务?

发布于 2025-02-13 14:28:13 字数 1270 浏览 1 评论 0原文

春季2.4.5。
Gradle 6.5.1。

application.properties在测试文件夹下的PostgreSQL连接字符串中有占位符。

spring.datasource.initialization-mode=always
spring.datasource.platform=postgres
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://${PG_HOST}:${PG_PORT}/
spring.datasource.username=postgres
spring.datasource.password=password
spring.datasource.max-size=5

为了使测试正常工作,我将以下行添加到build.gradle文件:

test {
    ...
    systemProperty "PG_HOST", System.getProperty("PG_HOST")
    systemProperty "PG_PORT", System.getProperty("PG_PORT")
    ...
}

因此,我可以通过命令行进行测试:

./gradlew test -DPG_HOST=localhost -DPG_PORT=5432

然后,我决定运行Gradle Test通过Intellij Idea Gradle Pane进行任务。

我添加了pg_host& pg_port作为环境变量。

,但是ENV变量未传递给Gradle Test Config,而测试失败。

对此有什么想法吗?

Spring 2.4.5.
Gradle 6.5.1.

There are placeholders in PostgreSQL connection string in application.properties file under test folder.

spring.datasource.initialization-mode=always
spring.datasource.platform=postgres
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://${PG_HOST}:${PG_PORT}/
spring.datasource.username=postgres
spring.datasource.password=password
spring.datasource.max-size=5

In order to make tests working, I've added the following lines to build.gradle file:

test {
    ...
    systemProperty "PG_HOST", System.getProperty("PG_HOST")
    systemProperty "PG_PORT", System.getProperty("PG_PORT")
    ...
}

So, I'm able to tun test via command line as follows:

./gradlew test -DPG_HOST=localhost -DPG_PORT=5432

Then, I decided to run Gradle test task via IntelliJ Idea Gradle pane.

enter image description here
I've added PG_HOST & PG_PORT as environment variables.

enter image description here

But the env variables are not being passed to Gradle test config, and test fail.

Any ideas on this matter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文