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

懵懂的女人 提交于 2019-12-09 17:05:00

问题


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

回答1:


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.




回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/36749015/how-do-i-get-intellij-to-recognize-gradle-generated-sources-dir

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!