简体   繁体   中英

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. 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. 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?

The generated code is saved in src/gen/java/ and the location of the generated code is made known via:

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

Since IntelliJ is building the project I thought the easiest way would have been to do:

compileJava.dependsOn <generateSourcesTask>

But adding that to the gradle file does not have an effect (probably because the JavaCC plugin is doing this already).

Did you try to add generated sources dir to main? Like this:

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

It works for me with:

compileJava.dependsOn('generateSourcesTask')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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