我有一個使用TestNg編寫的Java測試框架,並使用gradle構建。我想在命令行傳遞一個'environment'參數,這個參數將在Environment類中用於定義特定環境的域和服務器值。在這個問題上使用Gradle文檔和幾個論壇討論,我仍然無法將作爲系統屬性傳遞給框架的參數。我試圖從「測試」任務和「調試」任務中完成此操作,但無濟於事。任務中的'println'語句顯示Gradle正在拾取-Denv參數,但我嘗試將其添加到測試框架的System屬性失敗。GRADLE:TestNg - 無法將-D參數傳遞給java代碼
執行結果爲null引用,其中environmentName設置爲null。
有人可以識別我的錯誤嗎?
命令行
.\gradlew debug -Denv=production
的build.gradle
test {
def environment = System.properties["env"]
println environment
systemProperties = System.getProperties()
systemProperties['env'] = environment
useTestNG()
}
task debug(type: Test) {
// def environment = System.properties["env"]
// println environment
// systemProperties = System.getProperties()
// systemProperties['env'] = environment
def groupsToInclude = []
def groupsToExclude = []
groupsToInclude.add('under_development')
useTestNG() {
groupsToInclude.each { String group -> includeGroups group }
groupsToExclude.each { String group -> excludeGroups group }
}
}
Java類
import com.sun.javafx.runtime.SystemProperties;
public class Environment {
private static EnvironmentDefinition environment;
private Environment() {
}
public static EnvironmentDefinition getInstance() {
if (environment == null) {
String environmentName = SystemProperties.getProperty("env");
// environmentName = environmentName == null ? "production" : environmentName;
switch (environmentName) {
default:
Environment.environment = ProductionEnvironment.getInstance();
}
}
return environment;
}
}
這可能有助於...添加這是在gradel shell-sc中'exec'之前的那行ript - 'echo「$ JAVACMD」「$ {JVM_OPTS [@]}」-classpath「$ CLASSPATH」org.gradle.launcher.GradleMain「$ @」 '。下一行應該是exec。報告輸出。 – Prashant