Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mark Gradle source folder as test source in IntelliJ

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 }
    }
}

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.

I tried adding them as such (inside subprojects):

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

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.

How do I configure this correctly in Gradle?

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

like image 579
Jorn Avatar asked Feb 06 '17 09:02

Jorn


People also ask

How do I change the source folder in IntelliJ?

From the main menu, select File | Project Structure Ctrl+Alt+Shift+S and click Modules. Select the necessary module and open the Sources tab. next to Source Folders or Test Source Folders. Specify the package prefix and click OK.


1 Answers

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')
    }
}

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

be sure that you're not using IDE gradle integration when using idea plugin from gradle, custom changes to iml files will most likely clash with the IDE if the integration is on

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

EDIT: Gradle 4.7 Idea plugin correctly marks sources now.

like image 88
LazerBanana Avatar answered Sep 17 '22 06:09

LazerBanana