如何向 Gradle 添加新的源集? [英] How do I add a new sourceset to Gradle?

查看:31
本文介绍了如何向 Gradle 添加新的源集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将集成测试添加到我的 Gradle 构建(版本 1.0)中.它们应该与我的正常测试分开运行,因为它们需要将 webapp 部署到本地主机(它们测试该 webapp).测试应该能够使用我的主要源集中定义的类.我该如何实现?

I want to add integration tests to my Gradle build (Version 1.0). They should run separately from my normal tests because they require a webapp to be deployed to localhost (they test that webapp). The tests should be able to use classes defined in my main source set. How do I make this happen?

推荐答案

2021 年更新:

8 年来发生了很多变化.Gradle 仍然是一个很棒的工具.现在文档中有一个完整的部分专门用于配置集成测试.我建议您立即阅读文档.

A lot has changed in 8ish years. Gradle continues to be a great tool. Now there's a whole section in the docs dedicated to configuring Integration Tests. I recommend you read the docs now.

原答案:

这花了我一段时间才弄明白,而且在线资源不是很好.所以我想记录我的解决方案.

This took me a while to figure out and the online resources weren't great. So I wanted to document my solution.

这是一个简单的 gradle 构建脚本,除了主要和测试源集之外,还有一个 intTest 源集:

This is a simple gradle build script that has an intTest source set in addition to the main and test source sets:

apply plugin: "java"

sourceSets {
    // Note that just declaring this sourceset creates two configurations.
    intTest {
        java {
            compileClasspath += main.output
            runtimeClasspath += main.output
        }
    }
}

configurations {
    intTestCompile.extendsFrom testCompile
    intTestRuntime.extendsFrom testRuntime
}

task intTest(type:Test){
    description = "Run integration tests (located in src/intTest/...)."
    testClassesDir = project.sourceSets.intTest.output.classesDir
    classpath = project.sourceSets.intTest.runtimeClasspath
}

这篇关于如何向 Gradle 添加新的源集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆