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

6

16

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 ]
    }
}
Clavicle answered 20/4, 2016 at 15:49 Comment(4)
you can try adding the idea gradle plugin and maybe manipulate the project classpath that idea uses?Yuhas
My bad. That is added in subProjects.Clavicle
I smell a bug. ...maybe more than one. Here is why. In the generated documentation I can find a ref to other items but not generatedSourceDirs. If I use sourceDirs += ... it works fine. Suspect that somehow generatedSourceDirs didn't make it into the build.Clavicle
Work-around for now is: Add the directory to the main sourceSet or sourceDirs in the idea plugin.Clavicle
R
22

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.

Rachealrachel answered 24/6, 2017 at 19:21 Comment(4)
for me, adding it to sourceSets was somehow not sufficient, i needed to add sourceDirs += theDir to the module configVinny
More concise: File genSrc = file('build/generated/javacc'); sourceSets.main.java.srcDir genSrc; idea.module.generatedSourceDirs += genSrcJinajingle
The documentation hints that you need to add it to both sourceDirs AND generatedSourceDirs -> docs.gradle.org/current/dsl/…Scully
If you do not want to hard code the path, could do sourceSets.main.output.getGeneratedSourcesDirs() github.com/gradle/gradle/blob/…Unreality
B
15

The code of this answer, rewritten using Kotlin DSL, will look like this:

plugins {
    idea
}

val generatedSourcesPath = file("out/production/classes/generated")

java.sourceSets["main"].java.srcDir(generatedSourcesPath)

idea {
    module {
        generatedSourceDirs.add(generatedSourcesPath)
    }
}
Backstage answered 9/2, 2018 at 11:33 Comment(3)
Whats a Kotlin? Kidding -- somewhat. Lots of people don't know Kotlin.Clavicle
@Clavicle well it's a programming language :) (kotlinlang.org). And Gradle offers a DSL based on Kotlin on par with Groovy since version 3.0: github.com/gradle/kotlin-dslBackstage
@Clavicle and lots of people use Kotlin instead of Groovy for Gradle so this is useful.Materi
O
5

In my case, it didn't work unless I added the generate sources directory to both sourceDirs and generatedSourceDirs:

def generatedSourcesDir = file('src/generated/main/java')
idea {
  module {
    sourceDirs += generatedSourcesDir
    generatedSourceDirs += generatedSourcesDir
  }
}
Observable answered 12/4, 2019 at 15:48 Comment(0)
D
3

in 2020 you probably did not refresh the project in IDEA

because it actually works oob.

30 mins of reading outdated solutions :(

Damnatory answered 20/11, 2020 at 15:20 Comment(0)
B
1

It's happening in some versions. There are some issues that we can look at and read carefully. But for myself, from the IntelliJ IDEA 2019 the solutions below aren't working anymore:

Discussion in the Gradle forum about this: https://discuss.gradle.org/t/how-do-i-get-intellij-to-recognize-gradle-generated-sources-dir/16847

According to @Daniel Dekany, this worked in IDEA 2017.1.2, and worked for me until 2019:

plugins {
    id 'idea'
}

idea {
    module {
        generatedSourceDirs += file('build/generated/sources/annotationProcessor')
    }
}

But from 2019 to 2022, the solution that worked for me was:

def generatedDir = "${buildDir}/generated/sources"

sourceSets {
    main {
        java {
            srcDir generatedDir
        }
    }
}

idea {
    module {
        generatedSourceDirs.addAll(file(generatedDir))
    }
}
Buckle answered 6/10, 2022 at 17:41 Comment(0)
A
0
ext {
    // path to IDEA generated sources directory
    ideaGeneratedSourcesDir = "$projectDir/src/main/generated"
}
compileJava {
    //……
    options.annotationProcessorGeneratedSourcesDirectory = file(ideaGeneratedSourcesDir)
    //options.headerOutputDirectory.set(file(ideaGeneratedSourcesDir)) (tested no effect)
    //……
}
// above work for me, and i try all method this question mentioned it's not work! env: idea2019.3, wrapped gradle6.3-all, zh-CN, JDK8, [x] annotation processing is disabled(no effect, in global settings ), no idea plugin([x]plugins {id idea}), [x]sourceSets no need to set(genereated srcDir)

myCodeGenExample:

task vertxCodeGen(type: JavaCompile) {
    group 'build'
    source = sourceSets.main.java
    destinationDir = file(ideaGeneratedSourcesDir)
    classpath = configurations.compileClasspath
    options.annotationProcessorPath = configurations.annotationProcessor
    options.debugOptions.debugLevel = "source,lines,vars"
    options.compilerArgs = [
            "-proc:only",
            "-processor", "io.vertx.codegen.CodeGenProcessor",
            // where the non Java classes / non resources are stored (mandatory) - the processors requires this option to know where to place them
            "-Acodegen.output=$destinationDir.absolutePath",
    ]
}

refresh the gradle, continously exist

Attwood answered 8/4, 2020 at 10:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.