[英]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
依赖于moduleA
( implementation 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.