RGoyal
RGoyal

Reputation: 175

How to generate java stubs from protobuf for Java target 11 using maven?

I am trying to generate stub using protobuf. My pom.xml has below code

     <plugin>
          <groupId>com.github.os72</groupId>
          <artifactId>protoc-jar-maven-plugin</artifactId>
          <version>3.8.0</version>
          <executions>
            <execution>
              <phase>generate-sources</phase>
              <goals>
                <goal>run</goal>
              </goals>
              <configuration>
                <protocVersion>3.8.0</protocVersion>
                <includeStdTypes>true</includeStdTypes>
                <inputDirectories>
                  <include>src/main/proto</include>
                </inputDirectories>
                <outputTargets>
                  <outputTarget>
                    <type>java</type>
                  </outputTarget>
                    <outputTarget>
                    <type>grpc-java</type>
                    <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.24.0</pluginArtifact>
                    </outputTarget>
                </outputTargets>
              </configuration>
            </execution>
          </executions>
      </plugin>

However, it generates the source files with target as Java1.8.

I am migrating my apps to Java 11, and have included the below jars in pom.xml file:

<dependency>
    <groupId>com.sun.activation</groupId>
    <artifactId>javax.activation</artifactId>
    <version>1.2.0</version>
</dependency>
<dependency>
    <groupId>javax.annotation</groupId>
    <artifactId>javax.activation-api</artifactId>
    <version>1.3.2</version>
</dependency>

However maven protoc plugin generates the files Grpc.java with annotnation javax.annotation.Generated instead of javax.annotation.api.Generated

Is there any other way for generating the java stub with target version as JDK 11.

Upvotes: 2

Views: 1743

Answers (1)

Eric Anderson
Eric Anderson

Reputation: 26434

If you follow the grpc-java documentation, it instructs you to use:

<dependency> <!-- necessary for Java 9+ -->
  <groupId>org.apache.tomcat</groupId>
  <artifactId>annotations-api</artifactId>
  <version>6.0.53</version>
  <scope>provided</scope>
</dependency>

Previous versions of the examples used javax.annotation:javax.annotation-api:1.2, which does work, but it was replaced with Tomcat for licensing reasons. In your pom.xml, it seems you might have mixed up "activation" vs "annotation", which look pretty similar at a glance.

I'm not aware of a javax.annotation.api.Generated annotation. I've not seen any real evidence that javax.annotation.processing.Generated is an appropriate replacement for javax.annotation.Generated either.

Upvotes: 3

Related Questions