I try to build a gRPC and protobuf application in java with Gradle
I followed the instruction from: https://github.com/grpc/grpc-java/blob/master/README.md
The issue is that one file is not generated: *ServiceGrpc.java
But the corresponding *ServiceGrpc.class file is in the build directory generated by the gradle build.
I tried with running the compiler manually with the command protoc but I have the exact same issue (I'm on Ubuntu 18.04)
Here is my proto file
syntax = "proto3";
option java_multiple_files=true;
option java_generic_services= true;
//...//
message Track {
int64 id = 1; //... }
service TrackService {
rpc Create(Track) returns (Response); }
//...
The file Track.java, TrackOrBuilder.java, TrackOuterClass.java are all there. As well as their corresponding .class files in the build directory.
With the flag "option java_generic_services= true", TrackService.java is generated, and again .class file.
But not matter what, the file TrackServiceGrpc.java is not created, contrary its correspond .class file, which is quite confusing.
here is my build.gradle :
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.8'
}
}
plugins {
id 'java'
}
repositories {
mavenCentral()
}
apply plugin: 'com.google.protobuf'
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.9.0"
generateProtoTasks.generatedFilesBaseDir = 'src'
}
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.23.0'
}
}
generateProtoTasks {
all()*.plugins {
grpc {}
}
}
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
//https://github.com/grpc/grpc-java/blob/master/README.md
implementation 'io.grpc:grpc-netty-shaded:1.23.0'
implementation 'io.grpc:grpc-protobuf:1.23.0'
implementation 'io.grpc:grpc-stub:1.23.0'
compile group: 'javax.annotation', name: 'javax.annotation-api', version: '1.3.2'
}
What am I doing wrong ?
If the .class file for the gRPC service exists, the corresponding .java file must have been at some place. By default, it should appear in $generatedFilesBaseDir/{main, test}/grpc. By default, $generatedFilesBaseDir is $buildDir/generated/source/proto. But seems you have changed (or intended to change) generatedFilesBaseDir, that configuration should be done inside the protobuf closure instead of the protoc closure.
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.9.0"
}
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.23.0'
}
}
generatedFilesBaseDir = 'src'
generateProtoTasks {
all()*.plugins {
grpc {}
}
}
}
Also, need to mention that configuring generatedFileBaseDir is discouraged as it might have potential problems. See discussion at https://github.com/google/protobuf-gradle-plugin/issues/332
Related
I was trying to set up a JDBC connection to a redshift database in my java code and created a few UTs that would make calls to that database using the driver.
The problem lies (probably) in my Gradle as a classpath problem, as the UTs runs fine, but when I'm deploying the jar on my server, suddenly I get:
java.lang.ClassNotFoundException: com.amazon.redshift.jdbc42.Driver
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Here is my function :
private void establishJDBCConnection() throws Exception {
Class.forName("com.amazon.redshift.jdbc42.Driver");
Properties props = new Properties();
props.setProperty("user", USERNAME);
props.setProperty("password", PWD);
conn = DriverManager.getConnection(DB_URL, props);
}
And here is my gradle files (I've tried reducing them, but I don't know much about which parts are important so it may still be a bit big)
We are running with gradle 5.2.1
build.gradle:
apply plugin: 'java'
apply plugin: 'osgi'
apply plugin: 'partnerextension'
group = 'com.stuff'
// Setup of version, dependencies, imports, names...
version = '3.9.0.14'
// Repositories for the script itself (STIBO Added)
buildscript {
repositories{
repositories {
mavenCentral()
maven {
url "https://artifactory-2018.schneider-electric.com/artifactory/oneoffer-oim-libs-step9.3"
}
maven {
url "https://s3.amazonaws.com/redshift-maven-repository/release"
}
}
dependencies {
classpath 'com.stibo:com.stibo.gradle.plugin.partnerextension:1.0.0'
}
}
}
// Repositories for the project
repositories {
mavenCentral()
maven {
url "https://artifactory-2018.schneider-electric.com/artifactory/oneoffer-oim-libs-step9.3"
}
maven {
url "https://s3.amazonaws.com/redshift-maven-repository/release"
}
}
jar {
baseName = "com.stuff-${version}"
manifest {
// Do not export anything from the bundle
instruction 'Export-Package', "!*"
}
}
apply from :'partner.gradle'
partner.gradle:
// Setup of version, dependencies, imports, names...
def artifactName
// added so to declare external component POI
configurations {
includeInJar
}
dependencies {
testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:2.18.3"
//compile group: 'com.amazon.redshift', name: 'redshift-jdbc42', version: '1.2.43.1067'
compile group: 'com.amazon.redshift', name: 'redshift-jdbc42-no-awssdk', version: '1.2.8.1005'
compile group: 'com.amazonaws', name: 'aws-java-sdk-redshift', version: '1.11.920'
// added to declare POI
includeInJar 'some jars', \/* , \
'com.amazon.redshift:redshift-jdbc42:1.2.43.1067'*/
configurations.compile.extendsFrom(configurations.includeInJar)
}
jar {
into('lib') {
println "includeInJar: " + configurations.includeInJar.collect { File file -> file }
from configurations.includeInJar
}
// generate checksum
def folder = new File( "${buildDir}")
if( !folder.exists() ) {
// Create all folders up-to and including B
folder.mkdirs()
}
manifest {
def jarName = "com.stuff"
instruction 'Bundle-ClassPath', \
'somejars'/*, \
'lib/redshift-jdbc42-1.2.43.1067.jar' */
instruction 'Import-Package',
'''someimports'''
archiveName = "${jarName}-${version}.jar"
artifactName = archiveName
}
}
I've tried adding the redshift driver jar to my generated jar, but that does not make the class available. I've tried adding a main that would run this function and use the -cp to add the driver-jar to my classpath, but it didn't work either.
I've tried using the PostgreSQL driver instead of redshift, didn't work.
Also, can anyone explain to me how can the code work fine during UT? it makes no sense to me!
I need to create Gradle task which can replace .java files before Gradle build.
Gradle build package app in .war file. And I need to have replaced bytecode there after build.
I tried sourceSets Gradle task but it can only exclude files.
sourceSets {
main {
java {
exclude 'com/myapp/example/resource/impl/ResourceBundleImpl.java'
}
}
}
But I need to also include file in the same place. How I can do it with Gradle?
The directory to file that I need to exclude: com/myapp/example/resource/impl/ResourceBundleImpl.java
The directory to file that I need to include: src/main/webapp/WEB-INF/my/ResourceBundleImpl.java
To copy file content it is also posible solution.But How can I do it before compile time?
The below task didn't helped, becouse in build file have .java files instead of .classe file.
task prepareSources(type: Copy) {
from('src/main/webapp/WEB-INF/my')
into('build/classes/java/main/com/myapp/example/resource/impl')com/myapp/example
}
// Prepare sources, before compile
compileJava {
dependsOn prepareSources
}
The below task throws :
Task :cdx-war:compileJava FAILED
error: package com.myapp.example.util does not exist
sourceSets {
main {
java {
srcDir "$projectDir"
exclude 'com/medtronic/diabetes/carelink/rbps/resource/impl/ResourceBundleImpl.java'
include 'src/main/webapp/WEB-INF/my/ResourceBundleImpl.java'
}
}
I put classes that I need to replace together and add suffix to one of them and replace this suffix during compile time :
sourceSets {
main {
java {
srcDirs = ["$buildDir\\generated-src"]
}
}
}
task copySourceSet(type: Copy) {
println 'Change java class in patient-svc-war'
from "$rootDir\\patient-svc-war\\src\\main\\java"
into "$buildDir\\generated-src"
from file("$rootDir\\patient-svc-war\\**_suffix.java")
into "$buildDir\\generated-src"
rename { String fileName ->
fileName.replace("_suffix", "")
}
filter { line -> line.replaceAll('_suffix', '')}
}
compileJava.source "$buildDir\\generated-src"
compileJava {
dependsOn copySourceSet
}
compileJava.doLast {
delete "$buildDir\\generated-src"
}
This is a continuation of this question My initial issue has been solved, but a new one came after.
Following the tutorial mentioned in it, having solved a few errors, I now get an error when I try to run .\gradlew tasks:
FAILURE: Build failed with an exception.
* Where:
Build file 'C:\Users\benji\MinecraftWorkspace\forge-1.7.10-10.13.4.1614-1.7.10-src\build.gradle' line: 18
* What went wrong:
A problem occurred evaluating root project 'forge-1.7.10-10.13.4.1614-1.7.10-src'.
> Failed to apply plugin [id 'forge']
> You must set the Minecraft Version!
> java.lang.NullPointerException (no error message)
How do I set the Minecraft version? (1.7.10 in this instance)
Edit to include build.gradle:
buildscript {
repositories {
mavenCentral()
maven {
name = "forge"
url = "http://files.minecraftforge.net/maven"
}
maven {
name = "sonatype"
url = "https://oss.sonatype.org/content/repositories/snapshots/"
}
}
dependencies {
classpath 'net.minecraftforge.gradle:ForgeGradle:1.2-SNAPSHOT'
}
}
apply plugin: 'forge'
version = "1.0"
group= "com.yourname.modid" // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = "modid"
minecraft {
version = "1.7.10-10.13.4.1614-1.7.10"
runDir = "eclipse"
}
dependencies {
// you may put jars on which you depend on in ./libs
// or you may define them like so..
//compile "some.group:artifact:version:classifier"
//compile "some.group:artifact:version"
// real examples
//compile 'com.mod-buildcraft:buildcraft:6.0.8:dev' // adds buildcraft to the dev env
//compile 'com.googlecode.efficient-java-matrix-library:ejml:0.24' // adds ejml to the dev env
// for more info...
// http://www.gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html
// http://www.gradle.org/docs/current/userguide/dependency_management.html
}
processResources
{
// this will ensure that this task is redone when the versions change.
inputs.property "version", project.version
inputs.property "mcversion", project.minecraft.version
// replace stuff in mcmod.info, nothing else
from(sourceSets.main.resources.srcDirs) {
include 'mcmod.info'
// replace version and mcversion
expand 'version':project.version, 'mcversion':project.minecraft.version
}
// copy everything else, thats not the mcmod.info
from(sourceSets.main.resources.srcDirs) {
exclude 'mcmod.info'
}
}
Good day.
For minecraft version 1.7.10.
I was with the same problem, searching the forum in Japanese.
this link Forum
You have to modify the repositories and dependencies of the build.gradle and change the gradle-wrapper.properties to version 5.6.4.
As indicated in this link ForgeGradle-1.2
Contains an example to modify your build.gradle example
build.gradle
buildscript {
repositories {
mavenCentral()
maven { url = "https://jcenter.bintray.com/" }
maven {
name = "forge"
url = "https://files.minecraftforge.net/maven"
}
maven {
name = "sonatype"
url = "https://oss.sonatype.org/content/repositories/snapshots/"
}
}
dependencies {
classpath ('com.anatawa12.forge:ForgeGradle:1.2-1.0.+') {
changing = true
}
}
gradle-wrapper.properties
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-bin.zip
I made it work
Sorry, my English is very basic. I'm using a translator.
pls update the plugin to latest one . For reference check this link and link.
also try deleting the .gradle folder in your User Home and run it again.
I solved this by installing the 'Recommended' version of Forge 1.7.10 instead of the 'Latest'. Simple fix really, although I've already hit another wall.
I have few Java classes that I want to use on different projects.
I don't want to move these classes in a dedicated project for now.
So I want to build a JAR with these classes, and be able to use it in my other projects, all with Gradle.
So here my JAR task (sources) and I publish it as an artifact :
task utilitiesJar(type: Jar) {
baseName = 'utilities'
version = '0.0.1'
includeEmptyDirs = false
from sourceSets.main.allJava
include "**\\common\\exceptions\\**"
include "**\\common\\json\\**"
include "**\\common\\logging\\**"
}
publishing {
publications {
utilities(MavenPublication) {
artifact utilitiesJar
groupId group
artifactId utilitiesJar.baseName
version utilitiesJar.version
}
}
repositories {
maven {
url 'my_URL'
}
}
}
I get it back with an other project :
repositories {
mavenCentral()
maven {
url 'my_URL'
}
}
...
compile (...)
...
Seems like the JAR is correctly imported (I can see it in "External Libraries" of IntelliJ, with all its classes), but I can't use it.
Maybe because the .class files are missing ?
I'm beginner in Java, maybe I missed something.
How can I create a JAR with only some classes and then use it ?
Ok so as said in comments, I have to include the builded .class files, I can't use external .java classes like this.
So my solution :
def utilitiesName = '...'
def utilitiesVersion = '0.0.1'
task utilitiesJar(type: Jar, dependsOn: classes) {
baseName = utilitiesName
version = utilitiesVersion
includeEmptyDirs = false
from sourceSets.main.output
include ("**\\common\\exceptions\\**\\*", "**\\common\\json\\**\\*", "**\\common\\logging\\**\\*")
}
task utilitiesSourcesJar(type: Jar, dependsOn: classes) {
baseName = utilitiesName
version = utilitiesVersion
classifier = 'sources'
includeEmptyDirs = false
from sourceSets.main.allJava
include ("**\\common\\exceptions\\**\\*", "**\\common\\json\\**\\*", "**\\common\\logging\\**\\*")
}
publishing {
publications {
utilities(MavenPublication) {
artifact utilitiesJar
artifact utilitiesSourcesJar
groupId group
artifactId utilitiesName
version utilitiesVersion
}
}
repositories {
maven {
url 'myURL'
}
}
}
Now I can use it and see the classes in my IDE.
PS : doing in this way is pretty dirty. Create a sub-project / a module, it's just the way how to do it, that's finaly what I did.
I want to use gradle's CreateStartScripts Task to generate the script to start the application.
I use it in the following way:
apply plugin: 'java'
mainClass = 'UIMain';
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
task copyResources(type: Copy) {
from 'config.ini'
into 'build/dist'
}
task copyLibs(type: Copy) {
from configurations.default
from configurations.default.allArtifacts.files
into 'build/dist/libs'
}
task generateScript(type: CreateStartScripts) {
applicationName = "Dagang"
mainClassName = mainClass
outputDir = "build/dist/"
classpath = ""
}
task distribute(dependsOn: [
build,
copyLibs,
copyResources,
generateScript
]) <<{
description = 'Copies all the project libs to the distribution place.'
}
However when I run the build, it runs into error like:
A problem occurred evaluating root project 'dagang'.
[org.gradle.BuildExceptionReporter] Cause: Could not find property 'CreateStartScripts' on root project 'dagang'.
Can anyone shed me some light? Thanks.
Either import the class (org.gradle.api.tasks.application.CreateStartScripts), or use the application plugin. The latter is generally preferable.
CreateStartScripts appears to be a package-private class, hence it's invisible.
Try using the application plugin instead. You can then additionally override some other properties of the built-in startScripts task.