Search code examples
intellij-ideagradleintellij-14

Custom source set in Gradle imported to IntelliJ 14


I'm trying to get Gradle (2.1) and IntelliJ (14.0.2) to play nicely. Specifically, I have imported a sample Gradle project containing a separate source set for integration tests into IntelliJ.

The project builds fine using Gradle on the command line, and I'm able to run the integration tests successfully. When running inside IntelliJ on the other hand, I have two problems:

1) Compiling inside IntelliJ fails, due to a dependency in the integration test to a third-party library (commons-collections) which fails to resolve.

2) If I remove the dependency above and compile, I'm not able to run the integration test inside IntelliJ. I get the following error message:

No tests found for given includes: [org.gradle.PersonIntegrationTest.canConstructAPersonWithAName]

The file structure looks like this:

src
  integration-test
    java
    resources
  main
    java
    resources
  test
    java
    resources
build.gradle

And build.gradle:

apply plugin: 'java'

repositories {
    mavenCentral()
}

sourceSets {
    integrationTest {
        java.srcDir file('src/integration-test/java')
        resources.srcDir file('src/integration-test/resources')
    }
}

dependencies {
    testCompile 'junit:junit:4.11'
    integrationTestCompile 'commons-collections:commons-collections:3.2'
    integrationTestCompile sourceSets.main.output
    integrationTestCompile configurations.testCompile
    integrationTestCompile sourceSets.test.output
    integrationTestRuntime configurations.testRuntime
}

task integrationTest(type: Test, dependsOn: jar) {
    testClassesDir = sourceSets.integrationTest.output.classesDir
    classpath = sourceSets.integrationTest.runtimeClasspath
    systemProperties['jar.path'] = jar.archivePath
}

check.dependsOn integrationTest

Any ideas on how to make this work would be much appreciated.

The full Gradle sample project is available in the Gradle distribution, under samples/java/withIntegrationTests


Solution

  • You need to tell IDEA to map entries from your integrationTest configuration into your project as TEST dependencies. I am not sure whether you need to add source root directories too. The important part is:

    idea {
      module {
        //and some extra test source dirs
        testSourceDirs += file('some-extra-test-dir')
        generatedSourceDirs += file('some-extra-source-folder')
        scopes.TEST.plus += [ configurations.integrationTest ]
      }
    }
    

    More is described in http://www.gradle.org/docs/current/dsl/org.gradle.plugins.ide.idea.model.IdeaModule.html


    Edits to reflect Daniel's comments: generatedSourceDirs is is Gradle 2.2+. To set up the test task you will use task like

    task integTest(type: Test) {
        description = 'Runs the integration tests.'
        group = 'verification'
        testClassesDir = sourceSets.integTest.output.classesDir
        classpath = sourceSets.integTest.runtimeClasspath
        reports.junitXml.destination = file("${project.testResultsDir}/$name")
        reports.html.destination = file("${project.reporting.baseDir}/$name")
        shouldRunAfter test
    }
    check.dependsOn integTest