我有一个 gradle 多项目,其中我的 war 文件构建在一个子项目中,而我的集成测试位于不同的子项目中。我读到 gretty 应该能够启动一个 jetty 实例,让我在构建期间运行集成测试,但我不知道如何将 gretty IntegrationTestTask 任务“连接”到我的其他子项目中的任务,从那里运行实际测试。
我的项目结构如下所示:
root/
int-test/
build.gradle
web/
build.gradle
build.gradle
settings.gradle
文件内容
根/settings.gradle:
include ':web'
include ':int-test'
根/build.gradle:
apply plugin: 'java'
根/web/build.gradle:
apply plugin: 'war'
apply from: 'https://raw.github.com/akhikhl/gretty/master/pluginScripts/gretty.plugin'
gretty {
contextPath = '/'
integrationTestTask = 'intTests'
}
task intTests << {
println 'This task will run in just the right time'
}
根/int-test/build.gradle:
apply plugin: 'java'
task intTests << {
println 'All integration testing is done in here'
}
当我运行“./gradlew -q intTests”时,这是输出:
All integration testing is done in here 2014-12-11 15:37:02.046 INFO - Logging initialized @1157ms 2014-12-11 15:37:02.554 INFO - jetty-9.2.3.v20140905 2014-12-11 15:37:02.682 WARN - ServletContainerInitializers: detected. Class hierarchy: empty 2014-12-11 15:37:03.114 INFO - Started o.a.g.JettyWebAppContext@7da22e4a{/,file:/Users/fredrik/callista/dev/grettyfitnesse/web/build/inplaceWebapp/,AVAILABLE} 2014-12-11 15:37:03.130 INFO - Started ServerConnector@2590ae17{HTTP/1.1}{0.0.0.0:8080} 2014-12-11 15:37:03.130 INFO - Started @2245ms 2014-12-11 15:37:03.137 WARN - Jetty 9.2.3.v20140905 started and listening on port 8080 2014-12-11 15:37:03.158 WARN - runs at: 2014-12-11 15:37:03.159 WARN - http://localhost:8080/ This task will run in just the right time 2014-12-11 15:37:03.221 INFO - Stopped ServerConnector@2590ae17{HTTP/1.1}{0.0.0.0:8080} 2014-12-11 15:37:03.229 INFO - Stopped o.a.g.JettyWebAppContext@7da22e4a{/,file:/Users/fredrik/callista/dev/grettyfitnesse/web/build/inplaceWebapp/,UNAVAILABLE} 2014-12-11 15:37:03.232 WARN - Jetty 9.2.3.v20140905 stopped. Server stopped.
因此,Web 项目中的 intTests 任务将在正确的时刻运行,但 int-test 项目中的 intTests 任务将运行得很早(在 Web 服务器启动之前)。 如何设置以便 gretty 插件“连接”到我的 int-test 项目中定义的 intTests 任务?
我尝试过的事情: * 设置“integrationTestTask = ':int-test:intTests'”,希望它足以指定 gretty 应该在哪个子项目中寻找正确的任务。结果 - jetty 甚至没有启动。 * 在根 build.gradle 中创建 intTests 任务,尝试在 int-test 中扩展该任务。结果——没有区别。 * 在Web项目的intTests任务中添加了dependsOn(":int-test:intTests")。结果 - 没有差异
最佳答案
首先,检查是否 gretty.integrationTestTask = ":int-test:intTests"
作品。如果没有,请将以下内容添加到 int-tests/build.gradle
:
intTests {
dependsOn(":web:taskThatStartsTheWebServer")
finalizedBy(":web:taskThatStopsTheWebServer")
}
(您可以在 Gretty 文档或 gradle tasks
的输出中找到任务名称。)
请注意,它是 task foo { dependsOn "bar" }
,不是task foo << { dependsOn "bar" }
。最近的 Gradle 版本将检测到此错误并导致构建失败。
关于java - 如何使用 gradle 在另一个子项目的 Web 项目上运行集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27425836/