Protobuf generated classes cannot be found when using gradle - java

If I follow the instruction in the grpc-java readme and I use maven, the protobuf generated files appear in the target directory and are subsequently in the classpath for me to extend etc. However, when I use gradle, the generated classes appear in the build directory and are absent from the classpath. I'm fairly new to gradle so I'm not really sure why it's behaving so differently.
My build.gradle file
apply plugin: 'java'
apply plugin: 'com.google.protobuf'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.5'
}
}
group 'co.example'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile 'io.grpc:grpc-netty-shaded:1.15.1'
compile 'io.grpc:grpc-protobuf:1.15.1'
compile 'io.grpc:grpc-stub:1.15.1'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.5.1-1"
}
//noinspection GroovyAssignabilityCheck
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.15.1'
}
}
generateProtoTasks {
all()*.plugins {
grpc {}
}
}
}

It looks like for gradle (if you want to use the generated stubs/server interfaces) in the project where your proto files live in (i.e. the project is not just for generating jars and publishing them) then you'll need to add generatedFilesBaseDir to your build.gradle file:
protobuf {
generatedFilesBaseDir = "$projectDir/src/main/java/generated"
...
}
Once you've done this, the stubs should be in your classpath.
public class SomeServer extends MyProtoClassGrpc.PDFExtractImplBase {}

Related

IntelliJ: Unable to use class from jar dependency

I am trying to test setting up a minimal project, building and exporting it as a jar, and using that project as a dependency in another. I am using Spring for this. I have two projects, makeajar and useajar. In makeajar, I set up a simple project with a Java class called Dinner. I want to import the jar for this project as part of the library in useajar, and from useajar instantiate a Dinner object.
However, the issue I'm getting is that I can't import the Dinner class. When I do, the import shows red:
Is there something I'm doing wrong in this process?
Things I've already tried:
Invalidating caches and restarting
Creating a fresh project
Adding makeajar as a module dependency (note: This was already there, and also appeared in the Project Structure -> Libraries)
How I produce the jar file from makeajar:
Go to project root directory and run ./gradlew build
Things I'm noticing:
Although makeajar appears in my module dependencies and libraries, it does not appear in the "External Libraries" folder in the useajar project.
I can gradle refresh fine, and if I comment out the attempts to import the Dinner object, I build.
I can easily get various popular dependencies (i.e. Jackson, guava) from MavenCentral and use these right out of the box. The issue is only occurring with my makeajar dependency.
makeajar build.gradle:
plugins {
id 'org.springframework.boot' version '2.2.12.BUILD-SNAPSHOT'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id 'java'
}
group = 'com.makingajar'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
maven { url 'https://repo.spring.io/snapshot' }
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
useajar build.gradle:
plugins {
id 'org.springframework.boot' version '2.2.12.BUILD-SNAPSHOT'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id 'java'
id 'idea'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
apply plugin: 'idea'
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
maven { url 'https://repo.spring.io/snapshot' }
mavenLocal {
flatDir {
dirs projectDir.toString() + '/libs'
}
}
}
allprojects {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
implementation 'com.makingajar:makeajar2:0.0.1-SNAPSHOT'
implementation 'com.fasterxml.jackson.core:jackson-core:2.11.3'
}
}
test {
useJUnitPlatform()
}
IntelliJ version 2020.1.4
Gradle version 6.6.1

How to make Gradle + Google Sheets API + Java into a single downloadable program?

As of now I have made a chatbot application by combining Java and Google Sheets using the Google API which requires me to use Gradle. So far I have only been able to run the program through Gradle using the terminal.
In my end result I want to be able to send this program to someone and they should be able to install it or run it as easy as possible without having to run it through the terminal.
I suspect that the person might need to download Gradle to run the file since it is needed for the Google API. So I think the best way to do this is to send a zip file (or something else) with the Gradle Installer and the java file and somehow make it install everything automatically. Is this possible?
This is how my build.gradle file looks like
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'SheetsQuickstart'
sourceCompatibility = 1.7
targetCompatibility = 1.7
version = '1.0'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.1'
}
}
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.api-client:google-api-client:1.23.0'
compile 'com.google.oauth-client:google-oauth-client-jetty:1.23.0'
compile 'com.google.apis:google-api-services-sheets:v4-rev516-1.23.0'
compile(group: 'org.springframework', name: 'spring-core', version:'4.3.11.RELEASE')
}
jar {
doFirst {
manifest {
if (!configurations.compile.isEmpty()) {
attributes(
'Class-Path':configurations.compile.collect{it.toURI().toString()}.join(' '),
'Main-Class': 'SheetsQuickstart')
}
}
}
}
Assuming google sheets api is a dependency within gradle, cant you just build a jar file through gradle build then run the jar as an executable file?
build.gradle file example:
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'com.github.johnrengelman.shadow'
group = 'your.package.path.here'
version = '0.0.1-SNAPSHOT'
mainClassName = 'your.package.path.here.MainClassName'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.1'
}
}
dependencies {
compile(group: 'org.springframework', name: 'spring-core', version:'4.3.11.RELEASE')
}
repositories {
mavenCentral()
}
jar {
doFirst {
manifest {
if (!configurations.compile.isEmpty()) {
attributes(
'Class-Path':configurations.compile.collect{it.toURI().toString()}.join(' '),
'Main-Class': 'your.package.path.here.MainClassName')
}
}
}
Should be enough for a reg min build file, then add your dependencies.

How to move generated query type classes before compiling in Gradle?

I've managed to generate query type classes (.java) using Gradle, however they're being moved to build/classes/main along with compiled classes by default. How would I move them to src/main/java so I can reference them at compile time?
Here's my Gradle build script:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.6.RELEASE")
}
}
// Apply the java plugin to add support for Java
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
jar {
baseName = 'gs-serving-web-content'
version = '0.1.0'
}
// In this section you declare where to find the dependencies of your project
repositories {
// Use 'jcenter' for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
// In this section you declare the dependencies for your production and test code
dependencies {
// The production code uses the SLF4J logging API at compile time
compile 'org.slf4j:slf4j-api:1.7.21'
compile 'org.springframework.boot:spring-boot-starter-web:1.3.6.RELEASE'
compile 'org.springframework.boot:spring-boot-starter-thymeleaf:1.3.6.RELEASE'
compile 'org.springframework.boot:spring-boot-starter-data-jpa:1.3.6.RELEASE'
compile 'mysql:mysql-connector-java:6.0.3'
compile 'com.querydsl:querydsl-jpa:4.1.3'
compile 'com.querydsl:querydsl-apt:4.1.3:jpa'
// Declare the dependency for your favourite test framework you want to use in your tests.
// TestNG is also supported by the Gradle Test task. Just change the
// testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
// 'test.useTestNG()' to your build script.
testCompile 'junit:junit:4.12'
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
Edit
As per my comment - I'm trying to move generated classes to directory src/generated/java and then add that location to the source directories so they can get compiled. I've tried the following, but it doesn't create directory nor any files:
sourceSets {
main {
java {
srcDirs = [ 'src/main/java' ]
}
}
generated {
java {
srcDirs = [ 'src/generated/java' ]
}
}
}
This is the part you are missing:
compileJava {
options.compilerArgs << "-s"
options.compilerArgs << "$projectDir/generated/java"
doFirst {
// make sure that directory exists
file(new File(projectDir, "/generated/java")).mkdirs()
}
}
clean.doLast {
// clean-up directory when necessary
file(new File(projectDir, "/generated")).deleteDir()
}

Gradle + Java, multiproject build.gradle

This is my file hierarchy, 'Domain Module' has no code right now, basically a wrapper for DBController and Domain.
Domain Module
.gradle
.idea
build
DBController
build
src
main
java
interfaces
IDBController.java
DBController.java
res
some SQL files
test
java
some test files
build.gradle
Domain
.gradle
build
gradle
src
main
java
Server.java
build.gradle
gradlew
gradlew.bat
settings.gradle
gradle
build.gradle
gradlew
gradlew.bat
settings.gradle
This is my build.gradle in Domain Module/build.gradle
group 'Group'
version '1.0-SNAPSHOT'
apply plugin: 'java'
targetCompatibility = 1.8
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile project(':Domain')
compile project(':DBController')
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}
this is build.gradle in DOmain Module/DBController/build.gradle
group 'Group'
version '1.0-SNAPSHOT'
apply plugin: 'java'
compileJava {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'org.postgresql:postgresql:9.3-1103-jdbc3'
}
}
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}
dependencies {
compile('com.googlecode.json-simple:json-simple:1.1.1')
compile files('libs/json-simple-1.1.1.jar')
compile('org.postgresql:postgresql:9.3-1103-jdbc3')
}
And finally, build.gradle in Domain Module/Domain/build.gradle
group 'Group'
version '1.0-SNAPSHOT'
buildscript {
repositories {
mavenCentral()
jcenter()
}
}
apply plugin: 'java'
apply plugin: 'idea'
repositories {
mavenCentral()
jcenter()
}
sourceCompatibility = 8
targetCompatibility = 8
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
dependencies {
compile project(':DBController')
}
My main method is in Server.java, and it uses an instance of DBController. How do i assign this file in my java manifest? I've tried the simple
jar {
manifest {
attributes 'Main-Class': 'Domain.src.main.java.Server'
}
but whenever i try to execute java -jar -the generated jar in Domain Module/build/libs-
i get an error telling me it can't find the main file, and as the build gradles are now it gives me an error saying there's no reference to a main class at all.
The gist of my project is that DBController issues queries against a SQL server, and that Server.java will be a spring server. I decided to use gradle to do this so i would learn, and while i have learned alot about gradle, there is still much uncertainty.
I just figured out what was wrong.
jar {
manifest {
attributes 'Main-Class': '-insert main class here-'
}
}
This attribute assumes you're in the src/main/java dir to begin with, so if my filepath was
src/main/java/robert/util/Class.java
i would just have to say
jar {
manifest {
attributes 'Main-Class': 'robert.util.Class'
}
}
Spent so much time solving such a trivial error. My tip for anyone else is to not overlook the 'intro to gradle' sites and such. The solution was there all along.

Adding dependencies to a custom gradle plugin

I'm creating a gradle plugin that uses gson, but when I use the plugin at my client it throws this java.lang.NoClassDefFoundError: com/google/gson/Gson
I expect I am linking my dependencies in the plugin in a wrong way, but i'm not quite sure so any help would be great.
The build.gradle in the plugin
group 'nl.daanluttik.gradle'
version '0.1'
apply plugin: 'java'
apply plugin: 'maven' // the plugin to distribute to maven
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile group: 'com.google.code.gson', name: 'gson', version: '1.7.2'
compile gradleApi()/*The gradle plugin api*/
testCompile group: 'junit', name: 'junit', version: '4.11'
}
//To distribute to maven
uploadArchives {
repositories {
mavenLocal()
}
}
A segment of the buildgradle in the client project
buildscript {
repositories {
mavenLocal()
}
dependencies {
classpath group: 'nl.daanluttik.gradle', name: 'peach', version: '0.1'
}
}
Is this really the first error? I most often see NoClassDefFoundError (in contrast to ClassNotFoundException) if some static initializer threw some exception and because of that the class could not be loaded and is not available later on.
Your missing the pom file with your dependencies. If it's just java then you can easily use the maven-publish which will generate the pom for you correctly.
apply plugin: 'maven-publish'
publishing {
publications {
maven(MavenPublication) {
groupId 'nl.daanluttik.gradle'
artifactId 'peach'
version '0.1'
from components.java
}
}
}
Then you can publish to the repositories (default local only) with gradle publish
Reference: https://docs.gradle.org/current/userguide/publishing_maven.html

Categories

Resources