File tree Expand file tree Collapse file tree
java/com/example/tutorial Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+ <project xmlns =" http://maven.apache.org/POM/4.0.0"
3+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
4+ xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
5+ <modelVersion >4.0.0</modelVersion >
6+
7+ <groupId >com.fd</groupId >
8+ <artifactId >protobuf</artifactId >
9+ <version >1.0-SNAPSHOT</version >
10+
11+ <properties >
12+ <java-version >1.8</java-version >
13+ <maven .compiler.source>1.8</maven .compiler.source>
14+ <maven .compiler.target>1.8</maven .compiler.target>
15+ </properties >
16+
17+ <dependencies >
18+ <dependency >
19+ <groupId >com.google.protobuf</groupId >
20+ <artifactId >protobuf-java</artifactId >
21+ <version >3.7.1</version >
22+ </dependency >
23+ </dependencies >
24+
25+ <build >
26+ <plugins >
27+ <plugin >
28+ <groupId >com.github.os72</groupId >
29+ <artifactId >protoc-jar-maven-plugin</artifactId >
30+ <version >3.7.0.2</version >
31+ <executions >
32+ <execution >
33+ <phase >generate-sources</phase >
34+ <goals >
35+ <goal >run</goal >
36+ </goals >
37+ <configuration >
38+ <inputDirectories >
39+ <include >src/main/resources</include >
40+ </inputDirectories >
41+ <outputTargets >
42+ <outputTarget >
43+ <type >java</type >
44+ <addSources >none</addSources >
45+ <outputDirectory >src/main/java</outputDirectory >
46+ </outputTarget >
47+ <outputTarget >
48+ <type >descriptor</type >
49+ <addSources >none</addSources >
50+ <outputDirectory >src/main/resources</outputDirectory >
51+ </outputTarget >
52+ </outputTargets >
53+ </configuration >
54+ </execution >
55+ </executions >
56+ </plugin >
57+ </plugins >
58+ </build >
59+
60+ </project >
Original file line number Diff line number Diff line change 1+ ## Compiling .proto files manually
2+ If you want to use the protocol buffers compiler ` protoc ` manually, navigate to the folder of the source code and
3+ execute the command below:
4+
5+ ```
6+ protoc -I=./main/resources --java_out=./main/java ./main/resources/adressbook.proto
7+ ```
8+
9+ ## Protobuf Maven Plugin
10+ With the ` protoc-jar-maven-plugin ` in the ` pom.xml ` the .proto files are searched under the ` src/main/resources ` folder
11+ and compiled automatically during the ` generate-sources ` phase of maven build lifecycle
12+
13+
Original file line number Diff line number Diff line change 1+ package com .fd .protobuf ;
2+
3+ import com .fd .protobuf .proto .AddressBookProtos ;
4+
5+ import java .io .*;
6+
7+ public class App {
8+
9+ public static void main (String [] args ) throws IOException {
10+ AddressBookProtos .Person furkan = AddressBookProtos .Person .newBuilder ()
11+ .setId (123 )
12+ .setName ("furkan danismaz" )
13+ .setEmail ("..." )
14+ .addPhones (
15+ AddressBookProtos .Person .PhoneNumber .newBuilder ()
16+ .setNumber ("555-1234" )
17+ .setType (AddressBookProtos .Person .PhoneType .MOBILE )
18+ .build ())
19+ .build ();
20+
21+ OutputStream os = new FileOutputStream (new File ("./output" ));
22+ furkan .writeTo (os );
23+
24+ InputStream is = new FileInputStream (new File ("./output" ));
25+ AddressBookProtos .Person parsedPerson = AddressBookProtos .Person .parseFrom (is );
26+
27+ System .out .println (parsedPerson );
28+ }
29+ }
You can’t perform that action at this time.
0 commit comments