简体   繁体   English

Protobuf java代码有构建错误

[英]Protobuf java code has build errors

If you get build errors like these when using protobufs with java, look below. 如果在使用带有java的protobufs时遇到类似这样的构建错误,请查看下面的内容。

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

Ok, the so-called Java tutorial for protobufs doesn't actually mention how to get the protobuf library into your project. 好吧,所谓的protobufs Java教程实际上没有提到如何将protobuf库放入你的项目中。 It implies that all the code is in your single generated .java file, which would actually be pretty nice, but that isn't case. 这意味着所有代码都在你单个生成的.java文件中,这实际上非常好,但事实并非如此。

Look at the source and you will see references to com.google.protobuf , which you can find in the java/src/main/java directory in the protobuf source. 查看源代码,您将看到对com.google.protobuf引用,您可以在protobuf源代码的java/src/main/java目录中找到它。 Copy that into your project however, and it will have build errors. 然而,将其复制到您的项目中,它将产生构建错误。

The solution is in the README.txt file. 解决方案位于README.txt文件中。 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: 无论如何,这样做:

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

And then copy the java files into your project. 然后将java文件复制到您的项目中。

Another option is to edit the pom.xml included in the source. 另一种选择是编辑源中包含的pom.xml。 You can change it to compile the proto files at the validate life-cycle and write them into the source directory. 您可以更改它以在验证生命周期中编译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 validate and all the proto files will be compiled into the source of your project :) 现在,您可以运行mvn validate并将所有proto文件编译到项目的源代码中:)

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. 如果您不想安装Maven来构建库,则可以按照这些说明进行操作。 Note that these instructions skip running unit tests and only describes how to install the core protobuf library (without the util package). 请注意,这些指令跳过了运行单元测试,仅描述了如何安装核心protobuf库(没有util包)。

1) Build the C++ code, or obtain a binary distribution of protoc. 1)构建C ++代码,或获取protoc的二进制分发。 If you install a binary distribution, make sure that it is the same version as this package. 如果安装二进制分发版,请确保它与此程序包的版本相同。 If in doubt, run: 如有疑问,请运行:

$ 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)在您喜欢的任何地方安装课程。

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

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