Protobuf java 代码有构建错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11697572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:06:15  来源:igfitidea点击:

Protobuf java code has build errors

javaprotocol-buffers

提问by Timmmm

If you get build errors like these when using protobufs with java, look below.

如果在将 protobufs 与 java 一起使用时遇到此类构建错误,请查看以下内容。

The method getOptions() from the type Descriptors.Descriptor refers to the missing type MessageOptions

The import com.google.protobuf.DescriptorProtos cannot be resolved

FileDescriptorProto cannot be resolved to a type

回答by Timmmm

Ok, the so-called Java tutorialfor protobufs doesn't actually mention how to get the protobuf library into your project. It implies that allthe code is in your single generated .java file, which would actually be pretty nice, but that isn't case.

好吧,所谓的 protobufs 的Java 教程实际上并没有提到如何将 protobuf 库引入到您的项目中。这意味着所有代码都在您生成的单个 .java 文件中,这实际上非常好,但事实并非如此。

Look at the source and you will see references to com.google.protobuf, which you can find in the java/src/main/javadirectory in the protobuf source. Copy that into your project however, and it will have build errors.

查看源代码,您将看到对 的引用com.google.protobuf,您可以java/src/main/java在 protobuf 源代码的目录中找到它。但是,将其复制到您的项目中,它将出现构建错误。

The solution is in the README.txtfile. Yeah, maybe I should have read it, but shouldn't all the information you need to get started be in the getting started tutorial? Anyway, do this:

解决方案在README.txt文件中。是的,也许我应该阅读它,但是入门教程中不应该包含所有入门所需的信息吗?无论如何,这样做:

# From the protobuf directory.
cd java
protoc --java_out=src/main/java -I../src ../src/google/protobuf/descriptor.proto

And thencopy the java files into your project.

随后的Java文件复制到您的项目。

回答by doc

Another option is to edit the pom.xml included in the source. You can change it to compile the proto files at the validate life-cycle and write them into the source directory.

另一种选择是编辑源中包含的 pom.xml。您可以更改它以在验证生命周期编译 proto 文件并将它们写入源目录。

Apply this diff or similar (or create a new build profile):

应用此差异或类似(或创建新的构建配置文件):

$ diff -u ~/Downloads/protobuf-2.6.0/java/pom.xml pom.xml
--- /c/Users/MYNAME/Downloads/protobuf-2.6.0/java/pom.xml     Mon Aug 25 20:52:36 2014
+++ pom.xml     Tue Dec  2 13:51:56 2014
@@ -74,12 +74,12 @@
         <executions>
           <execution>
             <id>generate-sources</id>
-            <phase>generate-sources</phase>
+            <phase>validate</phase>
             <configuration>
               <tasks>
                 <mkdir dir="target/generated-sources" />
-                <exec executable="../src/protoc">
-                  <arg value="--java_out=target/generated-sources" />
+                <exec executable="protoc">
+                  <arg value="--java_out=src/main/java" />
                   <arg value="--proto_path=../src" />
                   <arg value="../src/google/protobuf/descriptor.proto" />
                 </exec>
@@ -92,12 +92,12 @@
           </execution>
           <execution>
             <id>generate-test-sources</id>
-            <phase>generate-test-sources</phase>
+            <phase>validate</phase>
             <configuration>
               <tasks>
                 <mkdir dir="target/generated-test-sources" />
-                <exec executable="../src/protoc">
-                  <arg value="--java_out=target/generated-test-sources" />
+                <exec executable="protoc">
+                  <arg value="--java_out=src/test/java" />
                   <arg value="--proto_path=../src" />
                   <arg value="--proto_path=src/test/java" />
                   <arg value="../src/google/protobuf/unittest.proto" />

Now, you can just run mvn validateand all the proto files will be compiled into the source of your project :)

现在,您可以运行mvn validate,所有 proto 文件都将编译到您的项目源中:)

回答by anql

https://github.com/google/protobuf/tree/master/java

https://github.com/google/protobuf/tree/master/java

Installation - Without Maven

安装 - 没有 Maven

If you would rather not install Maven to build the library, you may follow these instructions instead. Note that these instructions skip running unit tests and only describes how to install the core protobuf library (without the util package).

如果您不想安装 Maven 来构建库,您可以按照这些说明进行操作。请注意,这些说明跳过了运行单元测试,仅描述了如何安装核心 protobuf 库(没有 util 包)。

1) Build the C++ code, or obtain a binary distribution of protoc. If you install a binary distribution, make sure that it is the same version as this package. If in doubt, run:

1) 构建 C++ 代码,或获取 protoc 的二进制分发版。如果您安装二进制分发版,请确保它与此软件包的版本相同。如果有疑问,请运行:

$ protoc --version If you built the C++ code without installing, the compiler binary should be located in ../src.

$ protoc --version 如果您在未安装的情况下构建了 C++ 代码,则编译器二进制文件应位于 ../src 中。

2) Invoke protoc to build DescriptorProtos.java:

2)调用protoc来构建DescriptorProtos.java:

$ protoc --java_out=core/src/main/java -I../src \ ../src/google/protobuf/descriptor.proto 3) Compile the code in core/src/main/java using whatever means you prefer.

$ protoc --java_out=core/src/main/java -I../src \ ../src/google/protobuf/descriptor.proto 3) 使用您喜欢的任何方式编译 core/src/main/java 中的代码.

4) Install the classes wherever you prefer.

4) 在您喜欢的任何地方安装这些类。