简体   繁体   English

使用Gradle时如何在IntelliJ IDEA中集成生成的源?

[英]How to integrate generated sources in IntelliJ IDEA when using Gradle?

I'm currently using JavaCC (with the JavaCC gradle plugin from here ) to generate some of my source code. 我目前正在使用JavaCC(使用此处的JavaCC gradle插件)来生成我的一些源代码。 The rest of the project does depend on that code. 项目的其余部分确实依赖于该代码。 If I import the project into IDEA or clean the project, then I will get errors because classes are not found. 如果我将项目导入IDEA或清理项目,那么我将收到错误,因为找不到类。 However, building the project does work. 但是,构建项目确实有效。

Is it possible to modify the gradle file, so that IntelliJ (and possibly other editors too) know to generate these sources before analysing the code? 是否可以修改gradle文件,以便IntelliJ(以及可能还有其他编辑者)知道在分析代码之前生成这些源?

The generated code is saved in src/gen/java/ and the location of the generated code is made known via: 生成的代码保存在src / gen / java /中,生成的代码的位置通过以下方式得知:

sourceSets {
    gen {
        java {
            srcDir 'src/gen/java'
        }
    }
}

Since IntelliJ is building the project I thought the easiest way would have been to do: 由于IntelliJ正在构建项目,我认为最简单的方法是:

compileJava.dependsOn <generateSourcesTask>

But adding that to the gradle file does not have an effect (probably because the JavaCC plugin is doing this already). 但是将它添加到gradle文件没有效果(可能是因为JavaCC插件已经这样做了)。

Did you try to add generated sources dir to main? 您是否尝试将生成的源dir添加到main? Like this: 像这样:

sourceSets {
    main {
        java {
            srcDirs = ["src/main/java", "src/gen/java"]
        }
    } 
}

It works for me with: 它对我有用:

compileJava.dependsOn('generateSourcesTask')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM