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