简体   繁体   English

IntelliJ 不能识别 Scala 项目中的 protobuf 吗?

[英]Does IntelliJ not recognize protobuf in a Scala project?

I have a protobuf file src/main/protobuf/datamodel.proto on my scala project.我的 scala 项目上有一个 protobuf 文件src/main/protobuf/datamodel.proto datamodel.proto。 I was generating the java file using the protoc compiler installed on my ubuntu machine.我正在使用安装在我的 ubuntu 机器上的protoc编译器生成 java 文件。

cd src/
protoc --java_out=main/java main/protobuf/datamodel.proto

datamodel.proto:数据模型.proto:

syntax = "proto3";
package org.github.felipegutierrez.explore.akka.classic.remote.serialization;
option java_package = "org.github.felipegutierrez.explore.akka.classic.remote.serialization";
option java_outer_classname = "Datamodel";
message OnlineStoreUser {
  int32 userId = 1;
  string userName = 2;
  string userEmail = 4;
  string userPhone = 5;
}
message ProtobufVote {
  string ssn = 1;
  string candidate = 2;
}

So when I went to the src package org.github.felipegutierrez.explore.akka.classic.remote.serialization the corresponding file was there.所以当我去src package org.github.felipegutierrez.explore.akka.classic.remote.serialization的时候对应的文件是wasial.remote Then I decided to use the sbt-protobuf plugin.然后我决定使用sbt-protobuf插件。 Basically I added on the plugins.sbt file:基本上我在plugins.sbt文件中添加了:

addSbtPlugin("com.github.gseitz" % "sbt-protobuf" % "0.6.5")

and on the build.sbt file:build.sbt文件中:

enablePlugins(JavaAppPackaging, ProtobufPlugin)
sourceDirectories in ProtobufConfig += (protobufExternalIncludePath in ProtobufConfig).value
unmanagedResourceDirectories in Compile += (sourceDirectory in ProtobufConfig).value
libraryDependencies ++= Seq(
  ...
  "com.google.protobuf" % "protobuf-java"  % "3.14.0",
  "com.google.protobuf" % "protoc" % "3.14.0" pomOnly(),
)

When I run sbt protobuf:protobufGenerate on the root of the project, the corresponding java files are created.当我在项目的根目录上运行sbt protobuf:protobufGenerate时,会创建相应的 java 文件。 That is fine, I can compile, use the sbt docker:stage , sbt docker:publishLocal without problems.没关系,我可以编译,使用sbt docker:stage , sbt docker:publishLocal没有本地问题:

ERROR : But when I click on my IntelliJ IDEA Build > Rebuild project I am getting the error:错误:但是当我单击我的 IntelliJ IDEA Build > Rebuild project时,我收到了错误:

Datamodel is already defined as class Datamodel
public final class Datamodel {

I suppose that it has something to do with IntelliJ + sbt + protobuf configuration.我想这与 IntelliJ + sbt + protobuf 配置有关。 When I search for classes Datamodel inside IntelliJ I find only one under the target directory src_managed , which is where the sbt protobuf:protobufGenerate generated it.当我在 IntelliJ 中搜索Datamodel类时,我在目标目录src_managed下只找到一个,这是sbt protobuf:protobufGenerate生成它的地方。

Does anyone have an idea where I can configure it correctly and make IntelliJ recognize the class as only one in my project?有谁知道我可以在哪里正确配置它并使 IntelliJ 将 class 识别为我项目中的唯一一个?

I was able to solve this problem by adding this line on the build.sbt :我能够通过在build.sbt上添加这一行来解决这个问题:

javaSource in ProtobufConfig := ((sourceDirectory in Compile).value / "generated")

and removing the option java_package line from the datamodel.proto file:并从datamodel.proto文件中删除option java_package行:

syntax = "proto3";
package org.github.felipegutierrez.explore.akka.classic.remote.serialization;
option java_outer_classname = "Datamodel";
// commented this line because we are using "sbt-protobuf" plugin to generate java file on the specific location
// option java_package = "org.github.felipegutierrez.explore.akka.classic.remote.serialization";
message OnlineStoreUser {
  int32 userId = 1;
  string userName = 2;
  string userEmail = 4;
  string userPhone = 5;
}

now the command sbt protobuf:protobufGenerate generates one source file and IntelliJ see only one source file as well.现在命令sbt protobuf:protobufGenerate生成一个源文件,而 IntelliJ 也只看到一个源文件。

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

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