Search code examples
javamavenprotocol-buffersprotoc

How can I use google.protobuf.Timestamp with protoc-jar-maven-plugin?


I've been trying to use the protobuf type google.protobuf.Timestamp with the protoc-jar-maven-plugin but only get these compile time errors:

google/protobuf/timestamp.proto: File not found.
test.proto: Import "google/protobuf/timestamp.proto" was not found or had errors.
test.proto:9:5: "google.protobuf.Timestamp" is not defined.

The proto file looks like this:

syntax = "proto3";

import "google/protobuf/timestamp.proto";

package test;
option java_package = "test";

message TestTimestamp {
    google.protobuf.Timestamp liveStartDate = 1;
}

And pom file has the following configuration:

<build>
    <plugins>
        <plugin>
            <groupId>com.github.os72</groupId>
            <artifactId>protoc-jar-maven-plugin</artifactId>
            <version>3.6.0.1</version>
            <executions>
                <execution>
                    <id>protoc.main</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <protocVersion>3.6.0</protocVersion>
                        <addSources>main</addSources>
                        <includeDirectories>
                            <include>src/main/protobuf</include>
                        </includeDirectories>
                        <inputDirectories>
                            <include>src/main/protobuf</include>
                        </inputDirectories>
                    </configuration>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>

As far as I understand, this type is a part of proto3, so why do I get these errors?


Solution

  • Turns out that since this type is not one of the standard types, the Maven plugin needs additional configuration for this to work, namely the <includeMavenTypes>direct</includeMavenTypes> parameter, like so:

    <build>
        <plugins>
            <plugin>
                <groupId>com.github.os72</groupId>
                <artifactId>protoc-jar-maven-plugin</artifactId>
                <version>3.6.0.1</version>
                <executions>
                    <execution>
                        <id>protoc.main</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <protocVersion>3.6.0</protocVersion>
                            <addSources>main</addSources>
                            <includeMavenTypes>direct</includeMavenTypes>
                            <includeDirectories>
                                <include>src/main/protobuf</include>
                            </includeDirectories>
                            <inputDirectories>
                                <include>src/main/protobuf</include>
                            </inputDirectories>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    
        </plugins>
    </build>
    

    With this configuration, the proto file compiles.