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

后端 未结 5 2471
天涯浪人
天涯浪人 2021-02-20 13:08

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

相关标签:
5条回答
  • 2021-02-20 13:26

    in 2020 you probably did not refresh the project in IDEA

    because it actually works oob.

    30 mins of reading outdated solutions :(

    0 讨论(0)
  • 2021-02-20 13:27

    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
      }
    }
    
    0 讨论(0)
  • 2021-02-20 13:32

    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)
        }
    }
    
    0 讨论(0)
  • 2021-02-20 13:32
    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

    0 讨论(0)
  • 2021-02-20 13:36

    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.

    0 讨论(0)
提交回复
热议问题