简体   繁体   English

IntelliJ IDEA 看不到从子项目中的 protobuf 文件生成的类

[英]IntelliJ IDEA doesn't see classes generated from protobuf files in a subproject

I have a Gradle project with modules.我有一个带有模块的 Gradle 项目。 moduleA contains only protobuf files and produces a jar file with classes generated from the .proto files. moduleA仅包含 protobuf 文件,并生成一个.proto文件,其中包含从 .proto 文件生成的类。 moduleB depends on the moduleA ( implementation project(':moduleA') ). moduleB依赖于moduleAimplementation project(':moduleA') )。

moduleA
│   build.gradle
│   src
│   └───main
│       └───proto  <-- proto file defining gRPC services
moduleB
│   build.gradle
│   src            <-- code dependent on classes generated from moduleA
build.gradle

The project works well if I build/run it from Gradle.如果我从 Gradle 构建/运行它,该项目运行良好。

Problem: IntelliJ IDEA doesn't see the classes generated from moduleA in sources of moduleB (imports are red).问题: IntelliJ IDEA 在 moduleB 的源代码moduleB不到从moduleA生成的类(导入为红色)。

Question: How to make IntelliJ IDEA correctly recognize classes built from .proto files?问题:如何让 IntelliJ IDEA 正确识别从.proto文件构建的类?

I am using IntelliJ IDEA 2020.2.4 (Ultimate Edition).我正在使用 IntelliJ IDEA 2020.2.4(终极版)。

This is same as using proto generated code in the same module, it needs to be there first before it can be picked up by IDE.这与在同一模块中使用 proto 生成的代码相同,它需要先存在,然后才能被 IDE 拾取。

Such dependencies cannot be resolved statically.这种依赖关系不能静态解决。 Proto generated classes are available only after moduleA is built .原型生成的类仅在moduleA构建后可用。 You would need to build moduleA at least once (and refresh the IDE imports if necessary) to make its generated code reachable to moduleB .您需要构建moduleA至少一次(并在必要时刷新 IDE 导入)以使其生成的代码可以访问moduleB

For the IDE to resolve classes and imports from dependant module these classes should exist and they must be located in dependant module's source directory .对于 IDE 从依赖模块解析类和导入,这些类应该存在并且它们必须位于依赖模块的目录中。 Looks like the classes are generated into a directory which is not recognized by IDE as a source directory.看起来这些类生成到 IDE 无法识别为目录的目录中。 Try adding this generated directory as a Gradle source set.尝试将此生成的目录添加为 Gradle 源集。 In moduleA's Gradel build file add:在 moduleA 的 Gradel 构建文件中添加:

sourceSets {
    main {
        java {
            srcDirs = ['build/generated/source/proto/main/java']
        }
    }
}

where 'build/generated/source/proto/main/java' - the directory where sources are generated.其中'build/generated/source/proto/main/java' - 生成源的目录。

There is a related issue for IntelliJ IDEA: IDEA-209418 . IntelliJ IDEA 有一个相关问题: IDEA-209418

The location of the classes generated from the .proto files is not known by Intellij out of the box. Intellij 不知道从.proto文件生成的类的位置。 The 'com.google.protobuf' gradle plugin registers the source directories with the generated code for Intellij to see when the idea ' gradle plugin is applied in the build.gradle file of moduleA. 'com.google.protobuf' gradle 插件使用为 Intellij 生成的代码注册源目录,以查看何时在模块文件的 build.gradle 文件中应用idea ' gradle 插件

See IntelliJ IDEA tips for com.google.protobuf gradle plugin for protobuf gradle plugin documentation.有关 protobuf gradle 插件文档 的 com.google.protobuf gradle 插件,请参阅 IntelliJ IDEA 提示

This setup works for me with Intellij IDEA 2021.1.此设置适用于 Intellij IDEA 2021.1。

Short answer: Apply the idea gradle plugin to moduleA.简短回答:将idea gradle 插件应用于模块A。

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

相关问题 Intellij IDEA 看不到类,但通过 Gradle 构建有效 - Intellij IDEA doesn't see classes but building via Gradle works 为什么 IntelliJ IDEA 看不到 HttpClients? - Why IntelliJ IDEA doesn't see HttpClients? Maven:从编译中排除文件在 IntelliJ IDEA 中不起作用 - Maven: excluding files from compilation doesn't work in IntelliJ IDEA Intellij没有看到MapStruct生成的类 - Intellij doesn't see the generated class by MapStruct Eclipse 看不到来自目标/生成源的类 - Eclipse doesn't see classes from target/generated-sources 无法通过 IntelliJ Idea 和 Gradle 使用构建文件夹中生成的类 - Can't use generated classes from build folder with IntelliJ Idea and Gradle 使用IntelliJ中的Gradle导入Protobuf生成的类 - Import Protobuf generated classes using Gradle in IntelliJ 如果从 IntelliJ IDEA 运行应用程序,则找不到 Mapstruct 生成的类 - Mapstruct generated classes are not found if the application is run from IntelliJ IDEA IntelliJ Idea没有看到依赖性,它是由Gradle添加的 - IntelliJ Idea doesn't see dependency, which was added by Gradle IntelliJ IDEA 不显示新创建的类或文件。 要查看文件,我需要完全重新启动程序 - IntelliJ IDEA does not show newly created classes or files. For the files to see I need to restart the program completely
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM