如何让 IntelliJ 识别 gradle 生成的源目录?

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

所以我有一个像陀螺一样旋转的 XJC javaExec,但 IntelliJ 无法识别生成的输出,尽管已将其标记为 generated-src/java。我需要调整 idea 插件什么的吗?

注意:插件本身从根 build.gradle 加载到子项目中。

XJC项目:

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"
}

在依赖这个的项目中我有:

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

我试过:

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 ]
    }
}

我将指出 Daniel Dekany from a Gradle discussion thread actually linking to this question 的解决方案。引用:

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')
    }
}

很适合我。

answer, rewritten using Kotlin DSL 的代码如下所示:

plugins {
    idea
}

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

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

idea {
    module {
        generatedSourceDirs.add(generatedSourcesPath)
    }
}

在我的例子中,除非我将生成源目录添加到 both sourceDirsgeneratedSourceDirs:

,否则它不起作用
def generatedSourcesDir = file('src/generated/main/java')
idea {
  module {
    sourceDirs += generatedSourcesDir
    generatedSourceDirs += generatedSourcesDir
  }
}
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)

我的代码生成示例:

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

2020年你可能没有在IDEA中刷新项目

因为它确实有效。

30 分钟阅读过时的解决方案:(