我有一个使用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