xolstice / protobuf-maven-plugin

Maven Plugin that executes the Protocol Buffers (protoc) compiler
https://www.xolstice.org/protobuf-maven-plugin/
Other
232 stars 76 forks source link

How to set the compile order #14

Closed Mr-Giraffe closed 7 years ago

Mr-Giraffe commented 7 years ago

My project depends on the "google/api/annotations.proto". I tried to add this dependency with the param "additionalProtoPathElements". But it seem that this proto file didn't be compiled before compile my own proto files. So, it print the following errors: "XXXXService.java:[10504,25] package com.google.api does not exist". Is there a way to solve this?

sergei-ivanov commented 7 years ago

Most likely it is a configuration problem. Will you be able to publish your POM file as a gist and post a link here? It will also be helpful if you add your proto file to the gist, or if you could reduce it to a small reproducible test project.

Mr-Giraffe commented 7 years ago

Finally I decided to use a work around. I copied the "google/api/annotations.proto" to my project under the protoSourceRoot and it works now. But it will still be great if I know the root cause.

The pom file related to this part is as follows:

<plugin>
                <groupId>org.xolstice.maven.plugins</groupId>
                <artifactId>protobuf-maven-plugin</artifactId>
                <version>0.5.0</version>
                <configuration>
                    <!--
                      The version of protoc must match protobuf-java. If you don't depend on
                      protobuf-java directly, you will be transitively depending on the
                      protobuf-java version that grpc depends on.
                    -->
                    <checkStaleness>true</checkStaleness>
                    <protoSourceRoot>${project.basedir}/src/main/proto</protoSourceRoot>
                    <protocArtifact>com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}</protocArtifact>
                    <pluginId>grpc-java</pluginId>
                    <pluginArtifact>io.grpc:protoc-gen-grpc-java:${gRPC.version}:exe:${os.detected.classifier}</pluginArtifact>
                    <additionalProtoPathElements>
                        <additionalProtoPathElement>${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis</additionalProtoPathElement>
                    </additionalProtoPathElements>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>compile-custom</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
sergei-ivanov commented 7 years ago

The additionalProtoPathElements only assists protoc in resolving the imports. No proto files in those directories will be compiled. So either the project that provides annotations.proto needs to provide an artifact with generated java classes, or you have to do what you've done and compile it yourself.

sergei-ivanov commented 7 years ago

Shall I close this issue as a non-issue then?

clehene commented 5 years ago

@sergei-ivanov @Mr-Giraffe I took me a few hours to figure this one out you don't need to copy the protos, but you have to include

  1. https://mvnrepository.com/artifact/com.google.api.grpc/proto-google-common-protos/ in order for protoc to work properly. This will put the right target/protoc-dependencies/ (there may be two there, but one has the incorrect google/protobuf path instead of google/api. I didn't check where that's coming from
  2. the language specific dependency of https://github.com/googleapis/api-common-protos in order to have the compile time deps
sergei-ivanov commented 5 years ago

google.protobuf package is not a "rogue" one, it is actually part of protobuf core API. Those proto files are bundled into the protobuf-java artifact, and the generated java classes are placed into com.google.protobuf java package in the same jar:

<dependency>
    <groupId>com.google.protobuf</groupId>
    <artifactId>protobuf-java</artifactId>
    <version>3.6.1</version>
</dependency>

And when a dependency on common protos is added:

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

...it also pulls in protobuf-java as a transitive dependency, which is correct. And in addition to that it provides proto files in google.api protobuf package and the corresponding generated java classes in com.google.api java package.

So in the case of @Mr-Giraffe it looks like all they needed was the latter dependency (proto-google-common-protos), which would have made everything else to work properly. That is exactly what I meant by saying that

the project that provides annotations.proto needs to provide an artifact with generated java classes

clehene commented 5 years ago

@sergei-ivanov perhaps it would make sense to make parts of this thread part of the site docs?

sergei-ivanov commented 5 years ago

@clehene I've just pushed additional documentation that should hopefully provide a good reference in the future.

twjitm commented 4 years ago

so?write context in lable and it?

sergei-ivanov commented 4 years ago

@twjitm Sorry, I do not understand the question.

vithakur-gb commented 4 years ago

I am facing the same issue . also checked with debugger on protobuf-maven-plugin and found the there is no ordering followed in generation of the protofiles from the source root. Many times the files which has other proto files as import gets compiled first and throws compilation error as that file is which it requires is still not compiled yet. is there any way to provide compilation order ?? I don't think without this this particular plugin is going to work. any help would be appreciated . Thanks.

sergei-ivanov commented 4 years ago

@vithakur-gb there is no ordering enforced by protobuf compiler. As long as all imports can be resolved locally, all sources will be compiled. Your sources have to be internally consistent, and any external protobuf dependencies need to be declared.

vithakur-gb commented 4 years ago

@sergei-ivanov please refer to this stackoverflow question which I have asked for more details. https://stackoverflow.com/questions/59926285/unable-to-resolve-import-statements-in-google-protobuf-file-using-maven-protobuf