ValarMorghulis
ValarMorghulis

Reputation: 31

How to import a proto from one module into a proto of a different module using maven

Hello could you please help me with the below issue I'm facing,

I' trying to import one proto from a module into a proto of a different module using maven.

I'm using Intellij to create protos and I've all the protos in a project, let's call it as protos. Below is the structure.

protos
----> foo
--------> proto
------------> foo.proto
----> bar
--------> proto
------------> bar.proto

I'm trying to import foo.proto into bar.proto

In bar.proto I'm trying to do this:

syntax = "proto2";

package bar;
import "protos/foo/proto/foo.proto";

...

In pom.xml I've the below build with plugins:

<build>
        <extensions>
            <extension>
                <groupId>kr.motd.maven</groupId>
                <artifactId>os-maven-plugin</artifactId>
                <version>${os-maven-plugin.version}</version>
            </extension>
        </extensions>
        <plugins>
            <plugin>
                <groupId>org.xolstice.maven.plugins</groupId>
                <artifactId>protobuf-maven-plugin</artifactId>
                <version>${protobuf-maven-plugin.version}</version>
                <configuration>
                    <protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}
                    </protocArtifact>
                    <pluginId>grpc-java</pluginId>
                    <pluginArtifact>io.grpc:protoc-gen-grpc-java:${io.grpc.version}:exe:${os.detected.classifier}
                    </pluginArtifact>
                    <outputDirectory>${basedir}</outputDirectory>
                    <clearOutputDirectory>false</clearOutputDirectory>
                    <protoSourceRoot>proto</protoSourceRoot>
                </configuration>
                <executions>
                    <execution>
                        <id>compile</id>
                        <goals>
                            <goal>compile</goal>
                            <goal>compile-custom</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>test-compile</id>
                        <goals>
                            <goal>test-compile</goal>
                            <goal>test-compile-custom</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

I've below dependencies and I've the module foo as a dependency in pom.xml of bar module too:

<dependency>
    <groupId>com.google.api.grpc</groupId>
    <artifactId>grpc-google-common-protos</artifactId>
    <version>2.16.0</version>
</dependency> 


<dependency>
    <groupId>com.test.foo</groupId>
    <artifactId>foo-proto</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

I keep getting the below error when I do mvn clean install as protos/foo/proto/foo.proto: File not found.

Am I missing something, why can't it find the proto file even after adding the dependency.

I tried to import with different styles by providing ../foo/proto/foo.proto but it didn't work.

I am not sure if using maven is the right build tool for protobufs. Should I consider using Bazel for Protobuf, will it solve problems, please let me know.

Upvotes: 2

Views: 1198

Answers (1)

Paul Durrant
Paul Durrant

Reputation: 1

From: https://protobuf.dev/programming-guides/proto3/

The protocol compiler searches for imported files in a set of directories specified on the protocol compiler command line using the -I/--proto_path flag. If no flag was given, it looks in the directory in which the compiler was invoked. In general you should set the --proto_path flag to the root of your project and use fully qualified names for all imports.

So if you use -I to set an import path to be your top level protos folder, the following should work:

import "foo/proto/foo.proto";

Upvotes: 0

Related Questions