Skip to content

Commit

Permalink
Merge pull request quarkusio#10886 from michalszynkiewicz/code-gen-ev…
Browse files Browse the repository at this point in the history
…erywhere

gRPC code gen to generate health and reflection stubs
  • Loading branch information
michalszynkiewicz authored Jul 23, 2020
2 parents 7fc6af5 + bbb79f1 commit 05a0ec5
Show file tree
Hide file tree
Showing 23 changed files with 419 additions and 203 deletions.
101 changes: 101 additions & 0 deletions bom/application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@
<!-- Make sure to check compatibility between these 2 gRPC components before upgrade -->
<grpc.version>1.29.0</grpc.version>
<grpc-jprotoc.version>1.0.1</grpc-jprotoc.version>
<protobuf-java.version>3.11.0</protobuf-java.version>
<protoc.version>3.11.4</protoc.version>

<picocli.version>4.4.0</picocli.version>
<org.eclipse.sisu.inject.version>0.3.4</org.eclipse.sisu.inject.version>
Expand Down Expand Up @@ -1223,6 +1225,16 @@
<artifactId>quarkus-grpc</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-grpc-codegen</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-grpc-stubs</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-grpc-deployment</artifactId>
Expand Down Expand Up @@ -4267,6 +4279,95 @@
<artifactId>jprotoc</artifactId>
<version>${grpc-jprotoc.version}</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>${protobuf-java.version}</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protoc</artifactId>
<classifier>linux-aarch_64</classifier>
<type>exe</type>
<version>${protoc.version}</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protoc</artifactId>
<classifier>linux-x86_32</classifier>
<type>exe</type>
<version>${protoc.version}</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protoc</artifactId>
<classifier>linux-x86_64</classifier>
<type>exe</type>
<version>${protoc.version}</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protoc</artifactId>
<classifier>osx-x86_64</classifier>
<type>exe</type>
<version>${protoc.version}</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protoc</artifactId>
<classifier>windows-x86_32</classifier>
<type>exe</type>
<version>${protoc.version}</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protoc</artifactId>
<classifier>windows-x86_64</classifier>
<type>exe</type>
<version>${protoc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>protoc-gen-grpc-java</artifactId>
<type>exe</type>
<classifier>linux-aarch_64</classifier>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>protoc-gen-grpc-java</artifactId>
<type>exe</type>
<classifier>linux-x86_32</classifier>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>protoc-gen-grpc-java</artifactId>
<type>exe</type>
<classifier>linux-x86_64</classifier>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>protoc-gen-grpc-java</artifactId>
<type>exe</type>
<classifier>osx-x86_64</classifier>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>protoc-gen-grpc-java</artifactId>
<type>exe</type>
<classifier>windows-x86_32</classifier>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>protoc-gen-grpc-java</artifactId>
<type>exe</type>
<classifier>windows-x86_64</classifier>
<version>${grpc.version}</version>
</dependency>

<!-- Picocli -->
<dependency>
Expand Down
15 changes: 0 additions & 15 deletions build-parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,6 @@
<jacoco.agent.argLine></jacoco.agent.argLine>
<jacoco.version>0.8.5</jacoco.version>

<!-- grpc -->
<os-maven-plugin.version>1.6.2</os-maven-plugin.version>
<protobuf-maven-plugin.version>0.6.1</protobuf-maven-plugin.version>
<protoc.version>3.11.4</protoc.version>
<grpc.version>1.29.0</grpc.version>
<maven-dependency-plugin.version>3.1.2</maven-dependency-plugin.version>
</properties>

Expand Down Expand Up @@ -481,16 +476,6 @@
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>${os-maven-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>${protobuf-maven-plugin.version}</version>
</plugin>
</plugins>
</pluginManagement>
</build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public interface CodeGenProvider {
String providerId();

/**
* File extensions that CodeGenProvider will generate code from
* File extension that CodeGenProvider will generate code from
*
* @return collection of file extensions
* @return file extension
*/
@NotNull
String inputExtension();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import io.quarkus.bootstrap.prebuild.CodeGenException;
import io.quarkus.deployment.codegen.CodeGenData;

/**
* A set of methods to initialize and execute {@link CodeGenProvider}s.
*/
public class CodeGenerator {

// used by Gradle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class QuarkusPrepare extends QuarkusTask {
private boolean test = false;

public QuarkusPrepare() {
super("Quarkus performs pre-build preparations, such as sources generation");
super("Performs Quarkus pre-build preparations, such as sources generation");
}

@TaskAction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import io.quarkus.deployment.CodeGenerator;
import io.quarkus.deployment.codegen.CodeGenData;

@Mojo(name = "prepare", defaultPhase = LifecyclePhase.GENERATE_SOURCES, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME)
@Mojo(name = "prepare", defaultPhase = LifecyclePhase.GENERATE_SOURCES, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME, threadSafe = true)
public class CodeGenMojo extends AbstractMojo {

@Parameter(defaultValue = "${project.build.directory}")
Expand Down
2 changes: 0 additions & 2 deletions docs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@

<grpc-version>${grpc.version}</grpc-version>
<protoc-version>${protoc.version}</protoc-version>
<os-maven-plugin-version>${os-maven-plugin.version}</os-maven-plugin-version>
<protobuf-maven-plugin-version>${protobuf-maven-plugin.version}</protobuf-maven-plugin-version>
<elasticsearch-version>${elasticsearch-server.version}</elasticsearch-version>

<!-- Project website home page -->
Expand Down
148 changes: 90 additions & 58 deletions docs/src/main/asciidoc/grpc-getting-started.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -15,87 +15,42 @@ The default configuration is enough, but you can also select some extensions if

== Configuring your project

Edit the `pom.xml` file to add the quarkus gRPC extension dependency and the JSR 305 (just under `<dependencies>`):
Edit the `pom.xml` file to add the Quarkus gRPC extension dependency (just under `<dependencies>`):

[source, xml]
[source,xml]
----
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-grpc</artifactId>
</dependency>
----

In the `<properties>` section, define the 2 following properties:
Make sure you have `prepare` goal of `quarkus-maven-plugin` enabled in your `pom.xml`:

[source, xml, subs="verbatim,attributes"]
----
<grpc.version>{grpc-version}</grpc.version>
<protoc.version>{protoc-version}</protoc.version>
----

They configure the gRPC version and the `protoc` version.

Finally, add to the `build` section the `os-maven-plugin` extension and the `protobuf-maven-plugin` configuration.

[source, xml, subs="verbatim,attributes"]
[source,xml]
----
<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> // <1>
<version>{protobuf-maven-plugin-version}</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}</protocArtifact> // <2>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
<protocPlugins>
<protocPlugin>
<id>quarkus-grpc-protoc-plugin</id>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-grpc-protoc-plugin</artifactId>
<version>{quarkus-version}</version>
<mainClass>io.quarkus.grpc.protoc.plugin.MutinyGrpcGenerator</mainClass>
</protocPlugin>
</protocPlugins>
</configuration>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<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>
<goal>prepare</goal>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- ... -->
</plugins>
</build>
----
1. The `protobuf-maven-plugin` that generates stub classes from your gRPC service definition (`proto` files).
2. The class generation uses a tool named `protoc`, which is OS-specific.
That's why we use the `os-maven-plugin` to target the executable compatible with the operating system.

NOTE: This configuration instructs the `protobuf-maven-plugin` to generate the default gRPC classes and classes using Mutiny to fit with the Quarkus development experience.

With this configuration, you can put your service and message definitions in the `src/main/proto` directory.
`quarkus-maven-plugin` will generate Java files from your `proto` files.
Alternatively to using the `prepare` goal of the `quarkus-maven-plugin`, you can use `protobuf-maven-plugin` to generate these files, more in <<Generating Java files from proto with protobuf-maven-plugin>>

Let's start with a simple _Hello_ service.
Create the `src/main/proto/helloworld.proto` file with the following content:

Expand Down Expand Up @@ -287,14 +242,15 @@ public class ExampleResource {
}
}
----
1. Inject the service and configure its name. This name is used in the application configuration

1. Inject the service and configure its name.This name is used in the application configuration
2. Use the _blocking_ stub (also a generated class)
3. Invoke the service

We need to configure the application to indicate where is the `hello` service.
In the `src/main/resources/application.properties` file, add the following property:

[source, text]
[source,text]
----
quarkus.grpc.clients.hello.host=localhost
----
Expand All @@ -308,3 +264,79 @@ Then, open http://localhost:8080/hello/quarkus in a browser, and you should get

Like any other Quarkus applications, you can package it with: `mvn package`.
You can also package the application into a native executable with: `mvn package -Pnative`.

== Generating Java files from proto with protobuf-maven-plugin

Alternatively to using Quarkus code generation to generate stubs for `proto` files, you can also use
`protobuf-maven-plugin`.

To do it, first define the 2 following properties in the `<properties>` section:

[source,xml,subs="verbatim,attributes"]
----
<grpc.version>{grpc-version}</grpc.version>
<protoc.version>{protoc-version}</protoc.version>
----

They configure the gRPC version and the `protoc` version.

Then, add to the `build` section the `os-maven-plugin` extension and the `protobuf-maven-plugin` configuration.

[source,xml,subs="verbatim,attributes"]
----
<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> // <1>
<version>${protobuf-maven-plugin-version}</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}</protocArtifact> // <2>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
<protocPlugins>
<protocPlugin>
<id>quarkus-grpc-protoc-plugin</id>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-grpc-protoc-plugin</artifactId>
<version>{quarkus-version}</version>
<mainClass>io.quarkus.grpc.protoc.plugin.MutinyGrpcGenerator</mainClass>
</protocPlugin>
</protocPlugins>
</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>
----

1. The `protobuf-maven-plugin` that generates stub classes from your gRPC service definition (`proto` files).
2. The class generation uses a tool named `protoc`, which is OS-specific.
That's why we use the `os-maven-plugin` to target the executable compatible with the operating system.

NOTE: This configuration instructs the `protobuf-maven-plugin` to generate the default gRPC classes and classes using Mutiny to fit with the Quarkus development experience.
Loading

0 comments on commit 05a0ec5

Please sign in to comment.