JoeG
JoeG

Reputation: 7652

Can IntelliJ import non-standard source directories from Gradle?

I am converting over to using IntelliJ (version 2019.1). The multi-project directory structure used has the standard src/main/java and src/test/java for each project, but additionally has some non-standard ones such as: src/testsupport/java.

Gradlew (using the internal/recommended gradlew packaged within IntelliJ) is used to import the projects. The Gradle build files include both:

apply plugin: 'idea'
apply plugin: 'java'

Edited to improve clarity

Every project imports fine. Interproject references work to the standard directories. However, when I am in Project B, but need access to src/generated/java or src/testsupport/java from Project A, those are not imported (import statements that compile fine from the gradle command line show up as unresolvable within IntelliJ). Is there a configuration change or something needed to make these take effect?

Currently, I have:

subprojects {
    idea {
        module {
            testSourceDirs += project.sourceSets.generated.java.srcDirs
            testSourceDirs += project.sourceSets.testsupport.java.srcDirs
        }
    }
}

Upvotes: 1

Views: 205

Answers (1)

Cisco
Cisco

Reputation: 23042

You need help Gradle out by creating a source set for the custom sources your projects define. So from your question, something like:

(using Kotlin DSL)

allprojects {
    apply {
        plugin("idea")
        plugin("java-library")
    }

    repositories {
        mavenCentral()
    }

    configure<SourceSetContainer> {
        create("generated") {
            compileClasspath += project.the<SourceSetContainer>()["main"].output
            runtimeClasspath += project.the<SourceSetContainer>()["main"].output
        }
        create("testsupport") {
            compileClasspath += project.the<SourceSetContainer>()["main"].output
            runtimeClasspath += project.the<SourceSetContainer>()["main"].output
        }
    }

    val api by configurations
    val testImplementation by configurations
    val testRuntimeOnly by configurations

    dependencies {
        api(platform("org.junit:junit-bom:5.5.1"))
        testImplementation("org.junit.jupiter:junit-jupiter-api")
        testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
    }

    val test by tasks.getting(Test::class) {
        useJUnitPlatform()
    }
}

The above will give you:

enter image description here

So now you want to use projectA in projectB, so projectB's Gradle file would include a dependency on projectA:

dependencies {
    implementation(":projectA")
}

This should hopefully get you started. Keep in mind, the examples given above use the Kotlin DSL which you should be able to convert back to Groovy.

References:

Upvotes: 0

Related Questions