Class defined in generated sources not found by Intellij editor (but found by compiler (gradle build))

已回答

When generating java source files in a gradle build.
How do I make Intellij aware of generated classes so they may become known to the editor?

In the following screenshot you can see that the editor is not aware of `com.github.cstroe.svndumpgui.generated.CharStream`. 
`CharStream` is present in the build directory in both source and class file forms. 

0

If you are using Intellij and would like your gradle project to compile nicely in intellij and have the generated code in the build path; 
you can add the `idea` plugin and add the generated path to the main sourceSet, and the `idea.module`.
(The following uses the Kotlin DSL for gradle.)
```gradle
plugins {
    ...
    `idea`
}
tasks.compileJavacc {
        inputDirectory = layout.projectDirectory.dir("src/main/javacc").asFile
        outputDirectory = layout.buildDirectory.dir("generated/javacc").get().asFile
    }
    
sourceSets {
    named("main") {
        java {
            srcDirs.add(tasks.compileJavacc.get().outputDirectory)
        }
    }
}
idea {
    module {
        val genSrc = setOf(
            tasks.compileJavacc.get().outputDirectory,
            tasks.jjdoc.get().outputDirectory,
        )
        sourceDirs.addAll(genSrc)
        generatedSourceDirs.addAll(genSrc)
    }
}
```

0

Hi fred,

>The project is https://gitlab.com/babeloff/svndumpapi 

I'm getting 404 error when trying to access it. Could you please verify? Thanks.
0

>I'm getting 404 error when trying to access it. 

It was because of the trailing space in the link. Now I've opened it. And setup looks correct:

Have you made some changes to the project since it was not working? Can you give link to a project when it builds correctly by Gradle but the IntelliJ IDEA does not detect some classes? Thank you.

0

Hi Andrey,

I am facing the same issue on my project. I have already spend few days trying different configurations. I am able to build the project however IntelliJ complains that the package is in unnamed module. I assume because it's not in subfolder of the module file hierarchy. 

My situation is almost exactly the same as Fred's. I have also tried Idea plugin, but that didn't change anything.
Adding the path into the source path of the source set makes the source recognized by the IntelliJ and everything within the same folder hierarchy works, but I use Java9 modules and that creates a problem.

When I add the generated folder path into the source set path, compilation works. The code also works, the problem for me here is that the IntelliJ don't like the sources to be outside of the module folder structure. I am not sure if there is a way how to tell java to access classes from unnamed module or how to tell IntelliJ that the sources are also in different folder and belong to the same module.

If I generate the sources into the source folder it seems like something else get broken in the IntelliJ which seems to have something to do with output directory of the source generation Gradle task. It's almost like the files in the folder that is a subject to build output is considered invalid as a input/sources folder. So either way I end up with IDE complaining and basically I have to work around having errors in the project.

I am not sure how I suppose to deal with a generated sources in the multi-modular project in a way that IntelliJ likes.
I could just simply copy the files using a Gradle task, but it would be ugly, dangerous and possibly still not working.
So I might simply manually copy files and generate sources separately but that's terrible.

Just for the completeness, the import line is read displaying the following error message:
"Package '<package>' is declared in the unnamed module, but module '<module_name>' does not read it"
This is in the source that is in the named module trying to import package from the generated sources.

0

>IntelliJ complains that the package is in unnamed module. 

It does not look like the origially reported issue. You are getting a differnet error.

>the IntelliJ don't like the sources to be outside of the module folder structure.

You should not use `idea` Gradle plugin for manipulating source directories. Instead, normally, you should use standard Gradle means: condfigure corresponding source sets. But it does not look like it should be required. Does Gradle build from command line work? Does build from IDE work? Also pleas clarify the IDE version. Please make sure to try with the latest version.

0

Hi Andrey,

Thanks for the reply! Basically generated sources aren't located inside of the module file hierarchy. I am not sure if this is by Java Modules specification, but I assume that's the case. So the error makes sense however I am unsure how this suppose to work then.

I did a walkaround for now and basically copy the files into the module folder. Generated sources are in "generated" package so that way I keep it separated but I am deleting and copying files into the source file/folder hierarchy which is not ideal.

I don't use the Idea plugin, but I tired it to see if it makes any difference. (made no difference). I used source sets to define generated sources for the Gradle however they are still not considered a part of the same java module. 

Does Gradle build from command line work? 
I didn't tried this but the Gradle build works from IDE, it compiled just fine with no errors, but the IDE complains when accessing the generated sources from a codebase as they are not included as a requirement for the module.

IDE version is 2022.2.3 (Build #IC-222.4345.14, built on October 5, 2022)

0
Please provide a sample project to look into the possible issue. For uploading you can use https://uploads.jetbrains.com or any file sharing service. Thank you.
0

Hi Andrey,

So I have created a simple project to demonstrate the issue, but I couldn't reproduce it. So it seems it's something with my project specifically. I cannot share my project or have time to look at this deeper. For now I can work with my walkaround solution. 

However just to give a hint, I think it might have something to do with the Gradle plugin that I use for generating the scripts. It seems like when I comment out or change the output directory in the plugin the errors disappears (at least as far as I remember). In my demo project that I created today I don't use the plugin because to make that work you need to build jextract yourself or get the binary from somewhere.

0

请先登录再写评论。