在IntelliJ中将Gradle源文件夹标记为测试源 [英] Mark Gradle source folder as test source in IntelliJ

查看:132
本文介绍了在IntelliJ中将Gradle源文件夹标记为测试源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在gradle中设置了一个集成测试源文件夹,如下所示:

I have an integration test source folder set up in gradle like so:

subprojects {
    apply plugin: 'java'
    apply plugin: 'idea'

    sourceCompatibility = 1.8

    configurations {
        integrationTestCompile.extendsFrom testCompile
        integrationTestCompileOnly.extendsFrom integrationTestCompile
        integrationTestCompileOnly.extendsFrom testCompileOnly
        integrationTestRuntime.extendsFrom testRuntime
    }

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

    task integrationTest(type:Test) {
        testClassesDir = sourceSets.integrationTest.output.classesDir
        classpath = sourceSets.integrationTest.runtimeClasspath
        outputs.upToDateWhen { false }
    }
}

对于执行测试,这可以很好地工作,但是会导致IntelliJ的检查出现问题,这可能会更改测试代码的行为. IntelliJ无法将源文件夹识别为测试源.

For executing the tests, this works perfectly well, but it causes problems with IntelliJ's inspections, which may change behavior for test code. IntelliJ does not recognize the source folder as test source.

我尝试这样添加它们(在subprojects内部):

I tried adding them as such (inside subprojects):

idea {
    module {
        testSourceDirs += file('src/integrationTest/java')
    }
}

但是那根本没有帮助.我还尝试过将它们手动标记为测试源(上下文菜单->将目录标记为->测试源根),但是IntelliJ很快将其覆盖为正常的源根.

but that did not help at all. I also tried manually marking them as test source (context menu -> mark directory as -> test sources root), but IntelliJ quickly overrides that back to normal source root.

如何在Gradle中正确配置它?

How do I configure this correctly in Gradle?

我正在Ubuntu 16.04上使用IntelliJ 2016.1.3和Gradle 2.14.1

I'm using IntelliJ 2016.1.3 and Gradle 2.14.1 on Ubuntu 16.04

推荐答案

您需要确保测试源是该软件包的唯一源,然后

You would need to make sure test source is the only source for this package then

idea {
    module {
        sourceDirs -= file('src/integrationTest/java')
        testSourceDirs += file('src/integrationTest/java')
    }
}

,然后您需要gradle cleanIdea idea重新创建IntelliJ文件.

and then you would need to gradle cleanIdea idea to recreate the IntelliJ files.

请确保从gradle中使用Idea插件时未使用IDE gradle集成,如果集成启用,对iml文件的自定义更改很可能会与IDE发生冲突

sourceSets {
    integrationTest {
        java {
            compileClasspath += main.output + test.output
            runtimeClasspath += main.output + test.output
            srcDir "$projectDir/src/integrationTest/java"
        }
        resources.srcDir "$projectDir/src/integrationTest/resources"
    }
}

Gradle 4.7 Idea插件现在可以正确标记源.

这篇关于在IntelliJ中将Gradle源文件夹标记为测试源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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