README.txt 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. Protocol Buffers - Google's data interchange format
  2. Copyright 2008 Google Inc.
  3. This directory contains the Java Protocol Buffers runtime library.
  4. Installation - With Maven
  5. =========================
  6. The Protocol Buffers build is managed using Maven. If you would
  7. rather build without Maven, see below.
  8. 1) Install Apache Maven if you don't have it:
  9. http://maven.apache.org/
  10. 2) Build the C++ code, or obtain a binary distribution of protoc. If
  11. you install a binary distribution, make sure that it is the same
  12. version as this package. If in doubt, run:
  13. $ protoc --version
  14. You will need to place the protoc executable in ../src. (If you
  15. built it yourself, it should already be there.)
  16. 3) Run the tests:
  17. $ mvn test
  18. If some tests fail, this library may not work correctly on your
  19. system. Continue at your own risk.
  20. 4) Install the library into your Maven repository:
  21. $ mvn install
  22. 5) If you do not use Maven to manage your own build, you can build a
  23. .jar file to use:
  24. $ mvn package
  25. The .jar will be placed in the "target" directory.
  26. Installation - 'Lite' Version - With Maven
  27. ==========================================
  28. Building the 'lite' version of the Java Protocol Buffers library is
  29. the same as building the full version, except that all commands are
  30. run using the 'lite' profile. (see
  31. http://maven.apache.org/guides/introduction/introduction-to-profiles.html)
  32. E.g. to install the lite version of the jar, you would run:
  33. $ mvn install -P lite
  34. The resulting artifact has the 'lite' classifier. To reference it
  35. for dependency resolution, you would specify it as:
  36. <dependency>
  37. <groupId>com.google.protobuf</groupId>
  38. <artifactId>protobuf-java</artifactId>
  39. <version>${version}</version>
  40. <classifier>lite</classifier>
  41. </dependency>
  42. Installation - Without Maven
  43. ============================
  44. If you would rather not install Maven to build the library, you may
  45. follow these instructions instead. Note that these instructions skip
  46. running unit tests.
  47. 1) Build the C++ code, or obtain a binary distribution of protoc. If
  48. you install a binary distribution, make sure that it is the same
  49. version as this package. If in doubt, run:
  50. $ protoc --version
  51. If you built the C++ code without installing, the compiler binary
  52. should be located in ../src.
  53. 2) Invoke protoc to build DescriptorProtos.java:
  54. $ protoc --java_out=src/main/java -I../src \
  55. ../src/google/protobuf/descriptor.proto
  56. 3) Compile the code in src/main/java using whatever means you prefer.
  57. 4) Install the classes wherever you prefer.
  58. Micro version
  59. ============================
  60. The runtime and generated code for MICRO_RUNTIME is smaller
  61. because it does not include support for the descriptor,
  62. reflection or extensions. Also, not currently supported
  63. are packed repeated elements nor testing of java_multiple_files.
  64. To create a jar file for the runtime and run tests invoke
  65. "mvn package -P micro" from the <protobuf-root>/java
  66. directory. The generated jar file is
  67. <protobuf-root>java/target/protobuf-java-2.2.0-micro.jar.
  68. If you wish to compile the MICRO_RUTIME your self, place
  69. the 7 files below, in <root>/com/google/protobuf and
  70. create a jar file for use with your code and the generated
  71. code:
  72. ByteStringMicro.java
  73. CodedInputStreamMicro.java
  74. CodedOutputStreamMicro.java
  75. InvalidProtocolBufferException.java
  76. MessageMicro.java
  77. StringUtf8Micro.java
  78. WireFormatMicro.java
  79. If you wish to change on the code generator it is located
  80. in /src/google/protobuf/compiler/javamicro.
  81. To generate code for the MICRO_RUNTIME invoke protoc with
  82. --javamicro_out command line parameter. javamciro_out takes
  83. a series of optional sub-parameters separated by comma's
  84. and a final parameter, with a colon separator, which defines
  85. the source directory. Sub-paraemeters begin with a name
  86. followed by an equal and if that sub-parameter has multiple
  87. parameters they are seperated by "|". The command line options
  88. are:
  89. opt -> speed or space
  90. java_use_vector -> true or false
  91. java_package -> <file-name>|<package-name>
  92. java_outer_classname -> <file-name>|<package-name>
  93. opt:
  94. This change the code generation to optimize for speed,
  95. opt=speed, or space, opt=space. When opt=speed this
  96. changes the code generation for strings to use
  97. StringUtf8Micro which eliminates multiple conversions
  98. of the string to utf8. The default value is opt=space.
  99. java_use_vector:
  100. Is a boolean flag either java_use_vector=true or
  101. java_use_vector=false. When java_use_vector=true the
  102. code generated for repeated elements uses
  103. java.util.Vector and when java_use_vector=false the
  104. java.util.ArrayList<> is used. When java.util.Vector
  105. is used the code must be compiled with Java 1.3 and
  106. when ArrayList is used Java 1.5 or above must be used.
  107. The using javac the source parameter maybe used to
  108. control the version of the srouce: "javac -source 1.3".
  109. You can also change the <source> xml element for the
  110. maven-compiler-plugin. Below is for 1.5 sources:
  111. <plugin>
  112. <artifactId>maven-compiler-plugin</artifactId>
  113. <configuration>
  114. <source>1.5</source>
  115. <target>1.5</target>
  116. </configuration>
  117. </plugin>
  118. When compiling for 1.5 java_use_vector=false or not
  119. present where the default value is false.
  120. And below would be for 1.3 sources note when changing
  121. to 1.3 you must also set java_use_vector=true:
  122. <plugin>
  123. <artifactId>maven-compiler-plugin</artifactId>
  124. <configuration>
  125. <source>1.3</source>
  126. <target>1.5</target>
  127. </configuration>
  128. </plugin>
  129. java_package:
  130. The allows setting/overriding the java_package option
  131. and associates allows a package name for a file to
  132. be specified on the command line. Overriding any
  133. "option java_package xxxx" in the file. The default
  134. if not present is to use the value from the package
  135. statment or "option java_package xxxx" in the file.
  136. java_outer_classname:
  137. This allows the setting/overriding of the outer
  138. class name option and associates a class name
  139. to a file. An outer class name is required and
  140. must be specified if there are multiple messages
  141. in a single proto file either in the proto source
  142. file or on the command line. If not present the
  143. no outer class name will be used.
  144. Below are a series of examples for clarification of the
  145. various javamicro_out parameters using
  146. src/test/proto/simple-data.proto:
  147. package testprotobuf;
  148. message SimpleData {
  149. optional fixed64 id = 1;
  150. optional string description = 2;
  151. optional bool ok = 3 [default = false];
  152. };
  153. Assuming you've only compiled and not installed protoc and
  154. your current working directory java/, then a simple
  155. command line to compile simple-data would be:
  156. ../src/protoc --javamicro_out=. src/test/proto/simple-data.proto
  157. This will create testprotobuf/SimpleData.java
  158. The directory testprotobuf is created because on line 1
  159. of simple-data.proto is "package testprotobuf;". If you
  160. wanted a different package name you could use the
  161. java_package option command line sub-parameter:
  162. ../src/protoc '--javamicro_out=java_package=src/test/proto/simple-data.proto|my_package:.' src/test/proto/simple-data.proto
  163. Here you see the new java_package sub-parameter which
  164. itself needs two parameters the file name and the
  165. package name, these are separated by "|". Now you'll
  166. find my_package/SimpleData.java.
  167. If you wanted to also change the optimization for
  168. speed you'd add opt=speed with the comma seperator
  169. as follows:
  170. ../src/protoc '--javamicro_out=opt=speed,java_package=src/test/proto/simple-data.proto|my_package:.' src/test/proto/simple-data.proto
  171. Finally if you also wanted an outer class name you'd
  172. do the following:
  173. ../src/protoc '--javamicro_out=opt=speed,java_package=src/test/proto/simple-data.proto|my_package,java_outer_classname=src/test/proto/simple-data.proto|OuterName:.' src/test/proto/simple-data.proto
  174. Now you'll find my_packate/OuterName.java.
  175. As mentioned java_package and java_outer_classname
  176. may also be specified in the file. In the example
  177. below we must define java_outer_classname because
  178. there are multiple messages in
  179. src/test/proto/two-messages.proto
  180. package testmicroruntime;
  181. option java_package = "com.example";
  182. option java_outer_classname = "TestMessages";
  183. message TestMessage1 {
  184. required int32 id = 1;
  185. }
  186. message TestMessage2 {
  187. required int32 id = 1;
  188. }
  189. This could be compiled using:
  190. ../src/protoc --javamicro_out=. src/test/proto/two-message.proto
  191. With the result will be com/example/TestMessages.java
  192. Usage
  193. =====
  194. The complete documentation for Protocol Buffers is available via the
  195. web at:
  196. http://code.google.com/apis/protocolbuffers/