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

时间:2016-04-20 15:49:04

标签: gradle generated

所以我有一个像顶部一样旋转的XJC javaExec,但IntelliJ无法识别生成的输出,尽管标记为generated-src/java。我是否需要调整插件或其他内容?

注意:插件本身是从根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 ]
    }
}

5 个答案:

答案 0 :(得分:10)

我会指出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')
    }
}

适合我。

答案 1 :(得分:5)

使用this重写的Kotlin DSL回答代码如下所示:

plugins {
    idea
}

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

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

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

答案 2 :(得分:0)

在我的情况下,除非我将生成源目录添加到 sourceDirsgeneratedSourceDirs中,否则它不起作用:

def generatedSourcesDir = file('src/generated/main/java')
idea {
  module {
    sourceDirs += generatedSourcesDir
    generatedSourceDirs += generatedSourcesDir
  }
}

答案 3 :(得分: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

答案 4 :(得分:0)

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

因为它实际上可以工作。

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