简体   繁体   English

使用 Gradle Kotlin DSL 和 Intellij 的自定义测试源集

[英]Custom test source set with Gradle Kotlin DSL and Intellij

My project is using Gradle with Kotlin DSL for building.我的项目使用 Gradle 和 Kotlin DSL 进行构建。 I now want to add a custom sourceset used for testing.我现在想添加一个用于测试的自定义源集。 The relevant code looks as follows:相关代码如下所示:

java {
    sourceSets.create("systemTest") {
        java.srcDir("src/system-test/java")
        resources.srcDir("src/system-test/resources")
    }
}

By using that, I get another module in Intellij, which is treated as source module.通过使用它,我在 Intellij 中获得了另一个模块,它被视为源模块。 I also tried the following to mark it as test module:我还尝试了以下将其标记为测试模块:

idea.module {
 testSourceDirs.addAll(project.sourceSets.getByName("systemTest").java.srcDirs)
testSourceDirs.addAll(project.sourceSets.getByName("systemTest").resources.srcDirs)
}

However, if I then do a ./gradlew clean idea and open the project using the generated files (normally I import my projects from Gradle), the system-test folder isn't treated as source module at all.但是,如果我然后执行./gradlew clean idea并使用生成的文件打开项目(通常我从 Gradle 导入我的项目),则 system-test 文件夹根本不会被视为源模块。

Anyone having experience with declaring a custom sourceset with Kotlin DSL AND marking it as test module?任何有使用 Kotlin DSL 声明自定义源集并将其标记为测试模块的经验?

EDIT : I now created a sample Gradle project using Groovy and there it worked without problems with using the following code (which I guess is just the groovy version of my statements above):编辑:我现在使用 Groovy 创建了一个示例 Gradle 项目,并且在使用以下代码时它没有问题(我猜这只是我上面语句的 groovy 版本):

sourceSets {
  systemTest {
    java {
      compileClasspath += main.output + test.output
      runtimeClasspath += main.output + test.output
      srcDir file('src/system-test/java')
    }
    resources.srcDir file('src/system-test/resources')
  }
}

idea {
    module {
        testSourceDirs += project.sourceSets.systemTest.java.srcDirs
        testSourceDirs += project.sourceSets.systemTest.resources.srcDirs
    }
}

So either I am getting the transformation to Kotlin DSL wrong or it is a bug on the Intellij side.因此,要么是我错误地转换为 Kotlin DSL,要么是 Intellij 方面的错误。

Seems like I got the Kotlin transformation wrong, it should be 似乎我弄错了Kotlin转换,应该是

idea.module {
    val testSources = testSourceDirs

    testSources.addAll(project.sourceSets.getByName("systemTest").java.srcDirs)
    testSources.addAll(project.sourceSets.getByName("systemTest").resources.srcDirs)

    testSourceDirs = testSources
}

Using this snippet, the testSourceDirs set is changed and the relevant sourceset marked as test module. 使用此代码段,更改testSourceDirs集,并将相关的testSourceDirs集标记为测试模块。

Another way to do the same:另一种方法来做同样的事情:

testSourceDirs = testSourceDirs.plus(sourceSets["integrationTest"].java.srcDirs)
testResourceDirs = testResourceDirs.plus(sourceSets["integrationTest"].resources.srcDirs)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM