是否可以为 Gradle 测试任务动态配置系统属性?我还没有找到让它工作的方法。
我正在使用自己的 Gradle 插件,GwtPlugin
. 该apply()
方法如下所示:
/** Apply the plugin. */
void apply(Project project) {
project.plugins.apply(JavaPlugin)
project.plugins.apply(WarPlugin)
project.extensions.create("gwt", GwtPluginExtension, project)
project.extensions.create("testSuite", TestSuitePluginExtension, project)
project.convention.plugins.gwt = new GwtPluginConvention(project)
applyGwt(project)
applyTestSuite(project)
}
在该applyTestSuite()
方法中,我为我的测试套件创建了一个任务。任务的定义integrationtest
如下所示:
// Run integration tests, assumed to be found in a class suites/IntegrationTestSuite.
project.task("integrationtest", type: org.gradle.api.tasks.testing.Test, dependsOn: project.tasks.buildApplication) {
workingDir = { project.testSuite.getWorkingDir() == null ? project.projectDir : project.testSuite.getWorkingDir() }
scanForTestClasses = false
scanForTestClasses = false
enableAssertions = false
outputs.upToDateWhen { false }
include "suites/IntegrationTestSuite.class"
systemProperty "integration.test.server.wait", project.gwt.getServerWait()
beforeSuite { descriptor ->
if (descriptor.className == "suites.IntegrationTestSuite") {
project.convention.plugins.gwt.rebootDevmode()
}
}
afterSuite { descriptor ->
if (descriptor.className == "suites.IntegrationTestSuite") {
project.convention.plugins.gwt.killDevmode()
}
}
}
我想从中获取integration.test.server.wait
系统属性的配置project.gwt.getServerWait()
。我不知道如何进行这项工作,而且我开始认为这是不可能的。
如果我对系统属性进行硬编码,一切都会按预期工作:
systemProperty "integration.test.server.wait", 10
问题似乎是在定义任务时设置了系统属性,但我的扩展此时没有任何值。我不知道如何解决这个问题。
例如,我尝试将project.gwt.getServerWait()
调用放在闭包中,但在这种情况下,系统属性被设置为如下字符串:
com.me.gradle.GwtPlugin$_applyTestSuite_closure10_closure42@320de756
我还尝试将systemProperty
线路移动到一个doFirst
街区。在这种情况下,该doFirst
块从我的扩展中获得了一个合理的值(我可以打印它),但是分配显然为时已晚,无法影响测试运行器。
我有什么办法可以做到这一点吗?如果没有,是否有另一种方法可以将动态配置传递给我的测试运行器?