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

如何让IntelliJ识别gradle生成的源目录?,gradle,generated,Gradle,Generated,因此,我有一个XJC javaExec,它像陀螺一样旋转,但IntelliJ无法识别生成的输出,尽管它标记了generated src/java。我需要调整idea插件还是什么 注意:插件本身从根build.gradle加载到子项目中 XJC项目: description=“生成源代码并将其编译到$project的Jar中” 配置{xjc} 依赖关系{ xjc'org.glassfish.jaxb:jaxbxjc:2.2.11' xjc'org.glassfish.jaxb:jaxb运行时:2.

因此,我有一个XJC javaExec,它像陀螺一样旋转,但IntelliJ无法识别生成的输出,尽管它标记了
generated src/java
。我需要调整idea插件还是什么

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

XJC项目:

description=“生成源代码并将其编译到$project的Jar中”
配置{xjc}
依赖关系{
xjc'org.glassfish.jaxb:jaxbxjc:2.2.11'
xjc'org.glassfish.jaxb:jaxb运行时:2.2.11'
}
任务xjc(类型:JavaExec){
首先{
File generatedSrcDir=File($buildDir/generated src/java)
如果(!generatedSrcDir.exists()){
generatedSrcDir.mkdirs()
}
}
main=“com.sun.tools.xjc.xjcmeacade”
classpath configurations.xjc
def argsList=[
“-已生成标记”,
“-无标题”,
“-verbose”、//或-quiet或默认为nothing。
“-目标”,“2.1”,
“-编码”,“UTF-8”,
“-d”、“$buildDir/generated src/java”,
“-catalog”、“$projectDir/src/main/resources/commons gradle.cat”,
文件(“$projectDir/src/main/resources/v1/”)
args argsList
inputs.files文件(文件(“$projectDir/src/main/resources/v1/”)
outputs.files文件(文件(“$buildDir/generated src/java”)、文件(“$buildDir/classes”))
}
内贾瓦{
德彭森xjc
源“${buildDir}/generated src”
}
在依赖于此的项目中,我有:

编译项目(“:路径:到:模式:测试模式”)
我试过:

创意{
模块{
def buildDir=文件(“$buildDir”)
def generatedDir=文件(“$buildDir/generated src”)
def listOfDirs=[]
buildDir.eachDir{file->
if(file.name!=buildDir.name&&file.name!=generatedDir.name)
添加(文件)
}
excludeDirs=listOfDirs.toArray()
generatedSourceDirs+=文件($buildDir/generated src/java)
scopes.COMPILE.plus+=[configurations.xjc]
}
}

我将指出丹尼尔·德卡尼的解决方案。引述:

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')
    }
}
对我来说效果很好。

使用重写的答案代码如下所示:

plugins {
    idea
}

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

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

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

在我的例子中,除非我将generate sources目录添加到
sourceDirs
generated sourceDirs
,否则它不会工作:

def generatedSourcesDir = file('src/generated/main/java')
idea {
  module {
    sourceDirs += generatedSourcesDir
    generatedSourceDirs += generatedSourcesDir
  }
}
霉烯示例:

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

在2020年,您可能没有更新IDEA中的项目 因为它实际上在oob中起作用


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

您可以尝试添加idea gradle插件,并可能操纵idea使用的项目类路径?我的不好。这是在子项目中添加的。我闻到一个bug…可能不止一个。原因如下。在生成的文档中,我可以找到其他项的引用,但不能找到生成的sourceDirs。如果我使用sourceDirs+=…它工作正常。请怀疑是否存在这种情况generatedSourceDirs没有加入到构建中。目前的解决方法是:将目录添加到idea插件中的主sourceSet或sourceDirs中。Kotlin是什么?开玩笑吧,有点。很多人不知道Kotlin。@user447607这是一种编程语言:)()和Gealle提供了一个基于Kotlin的DSL,它与Groovy相比,版本3:@ USSR77607,很多人使用KOTLIN代替Gradle的Groovy,所以这是有用的。对于我来说,将它添加到SoSeCeSts是不够的,我需要添加<代码> SooSeCurrS++= DIIR < /代码>到模块CaseOngult:<代码>文件GsRcR=文件。('build/generated/javacc');sourceset.main.java.srcDir genSrc;idea.module.generatedSourceDirs+=genSrc文档提示您需要将其添加到
sourceDirs
generatedSourceDirs
->
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",
    ]
}