Reputation: 131
On my Mac I have libprotoc 27.3
(Protocol Buffers (Protobuf)) installed locally and I am trying to get it to "play nice" with my Java 17 app inside of IntelliJ. My Java app has the following high-level directory structure:
myapp/
src/
main/
java/
proto/
resources/
Inside the src/main/proto/
directory is a single file called dostuff.proto
and it looks like this:
syntax = "proto3";
package stuffdoer;
option java_multiple_files = true;
option java_package = "com.me.myapp.dostuff.stuffdoer";
message DoStuffRequest{
bytes image = 1;
}
message DoStuffResponse{
string stuff_image = 1;
}
service DoStuffDetection{
rpc GetStuffToDo(DoStuffRequest) returns (DoStuffResponse) {}
}
I believe the idea is that I need to run this tool to generate Java source code that can then be used in other areas of this project. Sure enough, I see that there are Java source files that seem to expect Java code generated by this file. One example is in a Java source file called DoStuffClient
that declares the following import statements:
package com.me.myapp.dostuff.stuffdoer.client;
import com.google.protobuf.ByteString;
import com.me.myapp.dostuff.stuffdoer.DoStuffGrpc;
import com.me.myapp.dostuff.stuffdoer.DoStuffRequest;
import io.grpc.StatusRuntimeException;
...
In IntelliJ, I am getting compiler errors because I have not explicitly ran Google Protocol Buffers to read this dostuff.proto
and generate the DoStuffGrpc
and DoStuffRequest
Java sources.
When I run the Maven build locally and run the app on my local machine, it starts up and I am able to manually test the code in the DoStuffClient
and verify it works just fine. So I believe Maven is correctly generating the Java sources (defined in dostuff.proto
) and making them available to the Java runtime.
However it is annoying to be getting the compiler errors in IntelliJ. What can I do to invoke Google Protocol Buffers from inside IntelliJ so that it generates the Java sources (defined in dostuff.proto
) and makes them available to the compile-time path, so that I stop seeing compiler errors when I'm writing code in IntelliJ?
What are the exact steps to take to get IntelliJ generating Java sources from my .proto
files and then getting it to link them to the compile-time path so that other source files in the opened project can "see" them?
Upvotes: 2
Views: 690
Reputation: 23371
So as discussed on the comments you will need to set up the protobuf generator plugin on your pom.xml
file. Since there are no details whatsoever of your pom file I will add the basic steps that will likely help you set yours up.
Make sure you have the dependencies for the protobuf java in your <dependencies>
section: from the documentation
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
<version>1.66.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.66.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>1.66.0</version>
</dependency>
<dependency> <!-- necessary for Java 9+ -->
<groupId>org.apache.tomcat</groupId>
<artifactId>annotations-api</artifactId>
<version>6.0.53</version>
<scope>provided</scope>
</dependency>
Then in the build section add the plugin for the protobuf, this code is directly from the plugin documentation I just added the path of your proto files to the configuration
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.7.1</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<configuration>
<protoSourceRoot>${project.basedir}/src/main/proto</protoSourceRoot>
<protocArtifact>com.google.protobuf:protoc:3.25.3:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.66.0:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
I would recommend looking through the documentation for any specifics.
Upvotes: 2
Reputation: 355
So if you'd like it to play nice, you have to generate the files while you're working.
From your resources folder in terminal (which you can do from a terminal inside intellij), you will need to run the command to generate the java code from the proto:
protoc --java_out="../java" --proto_path=./ *.proto
Note: You may want to set up a script that removes any proto files, and then recreates the protos if you're going to be making additions/changes to the proto as time goes on. It's easier than doing it manually everytime.
Since it's inside the same project, you should be able to run a mvn clean install
to get it to try to make the connections for you.
If it does not automatically add the source files, you can add them by going into your settings -> modules -> sources (I believe), and adding the source folder.
Upvotes: -1