2

是否可以为 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块从我的扩展中获得了一个合理的值(我可以打印它),但是分配显然为时已晚,无法影响测试运行器。

我有什么办法可以做到这一点吗?如果没有,是否有另一种方法可以将动态配置传递给我的测试运行器?

4

4 回答 4

2

我找到了一种方法来完成这项工作。诀窍是稍后在该属性确定可用时在测试任务上设置系统属性。实现这一点的最简单方法似乎是通过虚拟依赖项:

project.task("integrationtestconfig") << {
   outputs.upToDateWhen { false }
   project.tasks.integrationtest.systemProperty("integration.test.server.wait", project.gwt.getServerWait())
}

project.task("integrationtest", type: org.gradle.api.tasks.testing.Test, 
             dependsOn: project.tasks.buildApplication, project.tasks.integrationtestconfig)
...

这不是我所希望的优雅解决方案,但它确实有效,而且并不难遵循。

于 2013-10-23T01:01:45.603 回答
1

我要做的是:

ext {
    // Sets a sensible default
    myProperty project.properties.myProperty
}
task preTest << {
    // Computes the property...
    project.ext.myProperty = ...
}
task myTest {
    ...
    doFirst {
        systemProperty 'myProperty', project.ext.myProperty
    }
}

我在文件中定义了一个合理的默认值gradle.properties

myProperty = A sensible default value

在多模块环境中,它可能会更棘手。然后我rootProject.ext.myPropertytest任务中使用。

最好的问候,达米安。

于 2016-04-05T17:07:27.373 回答
1

不知道你用什么版本来做这个,但这对我来说似乎是最方便的方法:

task doSomethingBeforeTest {
    doLast {
      // some stuff to do
        test {
            systemProperties['some.property'] = 'prop'
        }
    }
}

基本上,只需将该test块放入您的任务中并设置属性。(这适用于 gradle 4.0 - 不确定以前的版本,但我想它会)。

于 2017-07-21T02:35:05.827 回答
0

我不确定这是否有效,但是您是否尝试过以这种方式进行操作

doLast {
    systemProperty "integration.test.server.wait", project.gwt.getServerWait()
}

在您的插件脚本中?

可能这与在 gradle 中解决问题时的阶段(配置,...)有关。

于 2013-10-23T11:41:28.950 回答