Using gRPC generated Java classes with IntelliJ

The Maven gRPC plugin generates the needed Java classes files from the .proto file. Taking this project for example:

It contains the plugin to generate the class files:

<plugin>
    <groupId>org.xolstice.maven.plugins</groupId>
    <artifactId>protobuf-maven-plugin</artifactId>
    <version>0.6.1</version>
    <configuration>
        <protocArtifact>
            com.google.protobuf:protoc:3.21.1:exe:${os.detected.classifier}
        </protocArtifact>
        <pluginId>grpc-java</pluginId>
        <pluginArtifact>
            io.grpc:protoc-gen-grpc-java:1.49.0:exe:${os.detected.classifier}
        </pluginArtifact>
        <protoSourceRoot>
            ${basedir}/src/main/proto/
        </protoSourceRoot>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
                <goal>compile-custom</goal>
            </goals>
        </execution>
    </executions>
</plugin>

If we open the project in IntelliJ, we can see there are classes missing:

Because these classes are generated during the compiling phase, so we can run the Maven command to generate the classes:

$ mvn install

We can see the Java classes are generated in the target directory:

Then we can set the above directories as source directory like this:

After these two directories are marked as source directory, we can see the color of these two directories are changed to blue, and the classes can be referred correctly:


Update: Or we can directly set the generated source director like this:

My Github Page: https://github.com/liweinan

Powered by Jekyll and Theme by solid

If you have any question want to ask or find bugs regarding with my blog posts, please report it here:
https://github.com/liweinan/liweinan.github.io/issues