scala - IntelliJ 不能识别 Scala 项目中的 protobuf 吗?
问题描述
我的 scala 项目中有一个 protobuf 文件src/main/protobuf/datamodel.proto
。protoc
我正在使用安装在我的 ubuntu 机器上的编译器生成 java 文件。
cd src/
protoc --java_out=main/java main/protobuf/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;
}
所以当我去src
包时org.github.felipegutierrez.explore.akka.classic.remote.serialization
,相应的文件就在那里。然后我决定使用sbt-protobuf插件。基本上我在plugins.sbt
文件中添加了:
addSbtPlugin("com.github.gseitz" % "sbt-protobuf" % "0.6.5")
并在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(),
)
当我sbt protobuf:protobufGenerate
在项目的根目录上运行时,会创建相应的 java 文件。很好,我可以编译,使用sbt docker:stage
,sbt docker:publishLocal
没有问题。
错误:但是当我点击我的 IntelliJ IDEA Build
>Rebuild project
我收到错误:
Datamodel is already defined as class Datamodel
public final class Datamodel {
我想这与 IntelliJ + sbt + protobuf 配置有关。Datamodel
当我在 IntelliJ中搜索类时,我在目标目录下只找到一个src_managed
,这是sbt protobuf:protobufGenerate
生成它的地方。
有谁知道我可以在哪里正确配置它并让 IntelliJ 将该类识别为我项目中的唯一一个?
解决方案
我能够通过在以下位置添加这一行来解决这个问题build.sbt
:
javaSource in ProtobufConfig := ((sourceDirectory in Compile).value / "generated")
并从文件中删除该option java_package
行datamodel.proto
:
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;
}
现在该命令sbt protobuf:protobufGenerate
生成一个源文件,而 IntelliJ 也只看到一个源文件。
推荐阅读
- json - jq 为以下奇怪的 json 重塑输入
- javascript - 如果将 matTableDataSource 放入 angular 中的类似 hasmap 结构中,如何进行排序和分页
- azure-cosmosdb - 如何在 CosmosDB 中获取 _ts?
- git - 在 GitHub 操作中获取当前分支和提交哈希
- sql - 将具有相同 ID 但具有条件的不同事件日志的行汇总在一起
- sql-server-data-tools - 如何在不安装任何工具的情况下从命令行生成 ISPAC 文件
- ios - 从 API 检索数据以在数组中使用它们时,如何摆脱“必须从主线程调用 API 方法”问题?迅速
- angular - Angular:无法通过路由器传递对象?
- python - requests.get 在某些 url 上崩溃
- asp.net-mvc - 由于请求正文过大,大文件上传到 ASP.NET Core 3.0 Web API 失败