簡體   English   中英

如何將系統屬性從 gradle 傳遞到 JVM?

[英]How to pass system properties from gradle to JVM?

我嘗試在測試項目中使用 SystemProperties,但無法在代碼中獲取任何內容。

CLI 命令:

gradle uitests -Dbrowser=chrome

項目結構:

my-app/
├─ src/
│  ├─ test/
│  │  ├─ java/
│  │  │  ├─ LoginTest.java
│  │  │  ├─ BaseTest.java
│  │  ├─ resources/
│  │  │  ├─ login.feature
├─build.gradle
  • build.gradle:
tasks.withType(Test){
    systemProperty 'browser', System.getProperty('browser')
    println systemProperties.get('browser') <------------------------(1)
}

task uitests() {
    dependsOn assemble, testClasses
    doLast {
        javaexec {
            main = "io.cucumber.core.cli.Main"
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
            args = [
                    '--plugin', 'pretty',
                    '--plugin', 'html:target/cucumber-report.html',
                    '--glue', myproject.steps',
                    'src/test/resources/scenarios']
        }
    }
}

因此,第 (1) 行打印了我需要的正確參數,但是當我嘗試使用System.getProperty("browser"); 我得到null

因為 -D JVM 參數僅限於 gradle JVM,它不會自動傳遞給使用特定參數手動創建的javaexec

你已經添加jvmArgs = ['-Dbrowser='+System.getProperty('browser')]
see https://docs.gradle.org/current/dsl/org.gradle.api.tasks.JavaExec.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM