How do I get IntelliJ to recognize gradle generated sources dir?

user447607 picture user447607 · Apr 20, 2016 · Viewed 10.2k times · Source

So I have an XJC javaExec that spins like a top but IntelliJ doesn't recognize the generated output despite having marked generated-src/java as such. Do I need to tweak the idea plug-in or something?

Note: The plug-in itself is loaded in subProjects from the root build.gradle.

XJC Project:

description = "Generates sources and compiles them into a Jar for $project"

configurations { xjc }
dependencies {
    xjc 'org.glassfish.jaxb:jaxb-xjc:2.2.11'
    xjc 'org.glassfish.jaxb:jaxb-runtime:2.2.11'
}

task xjc (type:JavaExec) {

    doFirst{
        File generatedSrcDir = file("$buildDir/generated-src/java")
        if (!generatedSrcDir.exists()) {
            generatedSrcDir.mkdirs()
        }
    }

    main = "com.sun.tools.xjc.XJCFacade"
    classpath configurations.xjc

    def argsList = [
            "-mark-generated",
            "-no-header",
            "-verbose", // or -quiet or nothing for default.
            "-target", "2.1",
            "-encoding", "UTF-8",
            "-d", "$buildDir/generated-src/java",
            "-catalog","$projectDir/src/main/resources/commons-gradle.cat",
            file("$projectDir/src/main/resources/v1/") ]

    args argsList
    inputs.files files(file("$projectDir/src/main/resources/v1/"))
    outputs.files files(file("$buildDir/generated-src/java"),file("$buildDir/classes"))

}

compileJava {
    dependsOn xjc
    source "${buildDir}/generated-src"
}

In the project that depends on this one I simply have:

compile project(":path:to:schemas:the-test-schema")

I've tried:

idea {
    module {

        def buildDir = file("$buildDir")
        def generatedDir = file("$buildDir/generated-src")
        def listOfDirs = []

        buildDir.eachDir { file ->
            if (file.name != buildDir.name && file.name != generatedDir.name)
            listOfDirs.add(file)
        }

        excludeDirs = listOfDirs.toArray()

        generatedSourceDirs += file("$buildDir/generated-src/java")
        scopes.COMPILE.plus += [ configurations.xjc ]
    }
}

Answer

Ophir Radnitz picture Ophir Radnitz · Jun 24, 2017

I'll point out a solution by Daniel Dekany, from a Gradle discussion thread actually linking to this question. To quote:

apply plugin: "idea"
...
sourceSets.main.java.srcDir new File(buildDir, 'generated/javacc')
idea {
    module {
        // Marks the already(!) added srcDir as "generated"
        generatedSourceDirs += file('build/generated/javacc')
    }
}

Works well for me.