I'm having a lot of trouble figuring out how to compile a GRPC Java server. I looked all over the grpc.io website and closest thing I found was this: http://www.grpc.io/docs/#quick-start , where I run
../gradlew -PskipCodegen=true installDist to build, and
./build/install/grpc-examples/bin/hello-world-client to run the client. This all works, but only for the hello-world tutorial. I have no idea how to do this for my own client/server. I'm able to generate the client/server protobufs using the .proto file. I looked in their readme and Java tutorial and couldn't find out how to compile the actual server (and client) after I write them
https://github.com/grpc/grpc-java/blob/master/examples/README.md
(can't link java tutorial because I dont have enough reputation). Unless there's documentation im missing, does anyone know how to compile a server and client that implements the GRPC classes generated from the .proto file? I did spend a fair amount of time searching. Any advice is much appreciated, thanks.
Also have a similar problem, ensure that:
You configured correctly the protoc, that will be downloading the executable and configure it as an environment variable of your OS.
The build.gradle file, make sure to include protobuf-gradle-plugin:
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'com.google.protobuf'
ext.grpcVersion = '1.0.1'
ext.protobufVersion = '3.0.2'
buildscript {
repositories { mavenCentral() }
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.0' }
}
repositories {
mavenCentral()
}
dependencies {
compile "com.google.protobuf:protobuf-java:${protobufVersion}"
compile "io.grpc:grpc-all:${grpcVersion}"
}
protobuf {
protoc { artifact = "com.google.protobuf:protoc:${protobufVersion}" }
plugins { grpc { artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}" } }
generateProtoTasks { ofSourceSet('main')*.plugins { grpc { } } }
}
idea {
module {
sourceDirs += file("${protobuf.generatedFilesBaseDir}/main/java");
sourceDirs += file("${protobuf.generatedFilesBaseDir}/main/grpc");
}
}
Your proto file:
syntax = "proto3";
package com.company.project;
service CompanyService{
rpc call(RequestMessage) returns (ResponseMessage) {}
}
Run gradle clean build, and it should generate your service and client classes for CompanyService.
The idea plugin, is jut for telling IntelliJ to recognize the src/main/proto as a source set.
To actually execute the client and server, you will need to make the implementation, mentioned in the tutorial for gRpc, and then apply the application plugin, in order to generate correctly the executable jar
//build.grdle code...
apply plugin: 'application'
mainClassName = 'com.company.project.MainClass'
jar { manifest { attributes('Main-Class' : mainClassName) } }
I had a similar issue but solved it using it in Gradle by adding the 'application' plugin. Before I was using the 'java' plugin and I could only generated a jar file. After switching to the 'application' plugin there is a gradle task similar to the gRPC example.
./gradlew installDist
And now to start your server you can run something similar to this:
./build/install/your-project/bin/your-server
To actually generate the Java classes off my .proto files I needed to run './gradle build' and also include the source generated using the sourceDir element you can see in the build.gradle below.
This is the full build.gradle file.
apply plugin: 'application'
apply plugin: 'com.google.protobuf'
apply plugin: 'idea'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.7.6'
}
}
repositories {
jcenter()
}
dependencies {
compile 'io.grpc:grpc-all:0.14.0'
}
mainClassName = "com.domain.service.YourMainClass"
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.0.0-beta-2"
}
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:0.14.0'
}
}
generateProtoTasks {
all()*.plugins {
grpc {}
}
}
}
idea {
module {
sourceDirs += file("${projectDir}/build/generated/source/proto/main/grpc");
sourceDirs += file("${projectDir}/build/generated/source/proto/main/java");
}
}
I am new to gRPC so any improvments to my Gradle file would be appericated.
This question has been answered on groups.google.com by 'Eric Anderson':
The JAR only has your code in it. It sounds like you want to make a "fat" jar which includes all your dependencies. You can do something like this:
jar {
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
}
}
Note that that isn't gRPC-specific; it's just working with Gradle. There may be alternatives, such as a quick Googling returned gradle-fatjar-plugin.
Related
I am playing a little bit with my own gradle plugin (for gradle 6.5.1). Now I wrote a small test (implemented in java) which is not working:
Project project = ProjectBuilder.builder().build();
ScriptHandler buildscript = project.getBuildscript();
Action<? super MavenArtifactRepository> action = new Action<MavenArtifactRepository>() {
#Override
public void execute(MavenArtifactRepository mavenArtifactRepository) {
mavenArtifactRepository.setUrl("https://plugins.gradle.org/m2/");
}
};
buildscript.getRepositories().maven(action);
buildscript.getDependencies().add("classpath", "io.spring.gradle:dependency-management-plugin:1.0.9.RELEASE");
project.getPlugins().apply("java");
project.getPlugins().apply("io.spring.dependency-management");
It says Plugin with id 'io.spring.dependency-management' not found. I thought that I copied the original from https://plugins.gradle.org/plugin/io.spring.dependency-management
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "io.spring.gradle:dependency-management-plugin:1.0.9.RELEASE"
}
}
apply plugin: "io.spring.dependency-management"
But I guess thats not the case. :-) Any idea how to translate that snippet to java world?
If you want to apply an other plugin in your test you need the other plugin on your classpath. So you have to add to the build file of your gradle-plugin and than its there.
In this case it means write the following to your build.gradle
testImplementation "io.spring.gradle:dependency-management-plugin:1.0.9.RELEASE"
I am converting over to using IntelliJ (version 2019.1). The multi-project directory structure used has the standard src/main/java and src/test/java for each project, but additionally has some non-standard ones such as: src/testsupport/java.
Gradlew (using the internal/recommended gradlew packaged within IntelliJ) is used to import the projects. The Gradle build files include both:
apply plugin: 'idea'
apply plugin: 'java'
Edited to improve clarity
Every project imports fine. Interproject references work to the standard directories. However, when I am in Project B, but need access to src/generated/java or src/testsupport/java from Project A, those are not imported (import statements that compile fine from the gradle command line show up as unresolvable within IntelliJ). Is there a configuration change or something needed to make these take effect?
Currently, I have:
subprojects {
idea {
module {
testSourceDirs += project.sourceSets.generated.java.srcDirs
testSourceDirs += project.sourceSets.testsupport.java.srcDirs
}
}
}
You need help Gradle out by creating a source set for the custom sources your projects define. So from your question, something like:
(using Kotlin DSL)
allprojects {
apply {
plugin("idea")
plugin("java-library")
}
repositories {
mavenCentral()
}
configure<SourceSetContainer> {
create("generated") {
compileClasspath += project.the<SourceSetContainer>()["main"].output
runtimeClasspath += project.the<SourceSetContainer>()["main"].output
}
create("testsupport") {
compileClasspath += project.the<SourceSetContainer>()["main"].output
runtimeClasspath += project.the<SourceSetContainer>()["main"].output
}
}
val api by configurations
val testImplementation by configurations
val testRuntimeOnly by configurations
dependencies {
api(platform("org.junit:junit-bom:5.5.1"))
testImplementation("org.junit.jupiter:junit-jupiter-api")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
}
val test by tasks.getting(Test::class) {
useJUnitPlatform()
}
}
The above will give you:
So now you want to use projectA in projectB, so projectB's Gradle file would include a dependency on projectA:
dependencies {
implementation(":projectA")
}
This should hopefully get you started. Keep in mind, the examples given above use the Kotlin DSL which you should be able to convert back to Groovy.
References:
https://docs.gradle.org/current/userguide/java_plugin.html#source_sets
https://docs.gradle.org/current/userguide/java_testing.html#sec:configuring_java_integration_tests
We have a few .proto files from which java classes are generated.
Because we have to keep the generated files in the repository, we would like to generate them only when we want (and not during every single build ). We are using this plugin. Also we would like to stick with this plugin, since we have quite a few things configured and we found it pretty useful.
Is there a way to exclude code generation during build task in gradle?
You can enable/disable protobuf tasks based on the property passed via command line:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.3'
}
}
apply plugin: 'java'
apply plugin: 'com.google.protobuf'
protobuf {
generateProtoTasks {
all().each { task ->
task.enabled = project.hasProperty('runProtobuf')
}
}
}
afterEvaluate {
println "generateProto enabled ${tasks.generateProto.enabled}"
println "generateTestProto enabled ${tasks.generateTestProto.enabled}"
}
I am having trouble importing Protobuf generated classes using Gradle.
This is how my project tree looks like:
I've tried marking the packages as Source, I have tried all possible combinations of imports:
import generated.main.grpc.GreeterGrpc;
import main.java.HelloRequest;
import java.*;
import HelloRequest;
None of them works. Here is my build.gradle:
group 'andu'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'com.google.protobuf'
sourceCompatibility = 1.5
repositories {
mavenCentral()
maven { url "https://plugins.gradle.org/m2/" }
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.0'
}
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compile 'io.grpc:grpc-protobuf:1.0.0-pre2'
compile 'com.google.protobuf:protobuf-java:3.0.0'
compile 'io.grpc:grpc-stub:1.0.0-pre2'
compile 'io.grpc:grpc-netty:1.3.0'
compile 'io.grpc:grpc-protobuf:1.3.0'
compile 'io.grpc:grpc-stub:1.3.0'
}
sourceSets {
main {
proto {
srcDir 'src/main/proto'
}
java {
srcDirs = ['src/main/java', 'generated/main/java']
}
}
}
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.2.0"
}
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.3.0'
}
}
generateProtoTasks.generatedFilesBaseDir = 'generated'
generateProtoTasks {
all()*.plugins {
grpc {}
}
}
}
Before I added
generateProtoTasks.generatedFilesBaseDir = 'generated'
all of my generated classes would be added to build/generated/main/java
from : https://medium.com/#DivyaJaisawal/generate-java-code-from-proto-file-using-gradle-1fb9fe64e046
The Protobuf plugin assumes Protobuf files (*.proto) are organized in the same way as Java source files, in sourceSets. The Protobuf files of a sourceSet are compiled in a single protoc run, and the generated files are added to the input of the Java compilation run of that sourceSet ().
sourceSets {
main {
java {
srcDirs 'build/generated/source/proto/main/grpc'
srcDirs 'build/generated/source/proto/main/java'
}
}
}
It may be a little different since I did this in an Android project, but what worked for me was adding classpath "com.google.protobuf:protobuf-gradle-plugin:0.8.1" in my root build.gradle and the following in the apps build.gradle.
Also don't forget to run a gradle build as only running won't generate the classes automatically unless you have the build project automatically option selected in compiler options.
apply plugin: 'com.google.protobuf'
protobuf {
generatedFilesBaseDir = "$projectDir/generated"
protoc {
artifact = 'com.google.protobuf:protoc:3.0.0'
}
plugins {
javalite {
artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0"
}
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.0.3'
}
}
generateProtoTasks {
all().each { task ->
task.builtins {
java
}
task.plugins {
grpc {}
}
}
}
}
//Protobuf
compile 'com.google.protobuf:protobuf-lite:3.0.0'
compile 'com.google.protobuf:protobuf-java:3.0.0'
//GRPC
compile('io.grpc:grpc-protobuf:1.1.1') {
exclude module: 'jsr305'
}
compile('io.grpc:grpc-okhttp:1.1.1') {
exclude module: 'jsr305'
}
compile('io.grpc:grpc-stub:1.1.1') {
exclude module: 'jsr305'
}
I've also made two basic but working examples using gRPC that may be able to help. Example Java project using Maven (if you're open to switching to Maven). And a basic Example Android project using gradle although there are minor differences (I'm using java-lite and okhttp instead of netty).
I had same issues in intellij while in sbt everything worked just fine, its just problem of higlightning it in IDE. I had to go to 'Project structure'->'Modules'->click on 'root'-> 'Depencencies'->'+' sign at the bottom-> Jars or directories -> add your generated dir as dependency.
If you want to generate for java, you have to mention a package name in your proto file like this:
option java_package = "com.example";
Do that and generate again.
Now import like this:
`import com.example.HelloRequest;`
I am still trying to wrap my head around gradle so please bear with me.
I'm trying to set up flyway migrations to run automatically on every gradle refresh/build instead of having to run a command-line command. So when someone is pulling the latest changes including some new migrations their local db will be up to date.
I have this build.grade in the root dir:
buildscript {
...
repositories {
mavenLocal()
mavenCentral()
}
allprojects {
...
}
project(":core") {
apply plugin: "java"
dependencies {
compile project(":shared")
}
}
project(":server") {
apply plugin: "java"
dependencies {
compile project(":shared")
}
}
project(":shared") {
apply plugin: "java"
}
tasks.eclipse.doLast {
delete ".project"
}
Where I've tried merging the lines from the flyway get started pages.
I basically want to run the $ gradle flywayMigrate -i command from within the build.gradle file. I've tried with build.finalizedBy(flywayMigrate) but to no avail.
How would I do something like that?
Thanks in advance!
In order to make build finalized by flywayMigrate you need to run gradle build which probably You didn't.
To run flywayMigrate you can use defaultTasks which will run the tasks configured if no other are provided. So:
buildscript {
dependencies {
classpath 'com.h2database:h2:1.4.191'
}
}
plugins {
id "org.flywaydb.flyway" version "4.1.2"
}
flyway {
url = 'jdbc:h2:file:./target/foobar'
user = 'sa'
}
defaultTasks 'flywayMigrate'
When it comes to configuring -i (logging level) from within build.gradle it seems to be impossible with the newest gradle versions.