Gradle doesn't find Google Protobuf package - java

I am trying to setup Google protobuf with netty, but when I start compilation gradle first download google protobuf (at least at the first attempt) but then at compilation it just tells me :
/src/main/java/GameMoveOuterClass.java:1536: error: package com.google.protobuf.GeneratedMessageV3 does not exist
com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
here is my build.gradle :
apply plugin: 'java'
apply plugin: 'com.google.protobuf'
repositories {
mavenCentral()
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.0'
}
}
dependencies {
compile group: 'io.netty', name: 'netty-all', version: '4.1.5.Final'
compile group: 'com.google.protobuf', name: 'protobuf-java', version: '2.4.1'
}
jar {
manifest {
attributes("Main-Class": 'server.Server',
"Class-Path": configurations.compile.collect { it.getPath() }.join(' '))
}
}
If someone knows what's wrong, please let me know
Thanks

You're using the version 2.4.1 of protobuf which doesn't come with GeneratedMessageV3.
Update to a new version of protobuf which include this class like the 3.0.0
dependencies {
compile group: 'io.netty', name: 'netty-all', version: '4.1.5.Final'
compile group: 'com.google.protobuf', name: 'protobuf-java', version: '3.0.0'
}

Using the maven central advanced search for com.google.protobuf.GeneratedMessageV3 it seems that the class is in com.google.cloud:google-cloud-nio:xxx or maybe com.trueaccord.scalapb:protobuf-runtime-scala_yyy:zzz. I'm guessing you'll need to add one of these to your classpath.

I am not familiar with Gradle but it looks to me like you are mixing new protobuf generated code with an older protobuf library, which is not supported. The GeneratedMessageV3 class was added only recently (around 3.0 I believe), and so new generated code that references that class cannot be linked against an older library which does not include it.

In my scenario, both my app and library module should add
implementation 'com.google.protobuf:protobuf-javalite:3.9.1'
even app has a dependency on library

Related

Java Modules, Gradle project - Problems with external libraries

I have a multi module project in IntelliJ, a java library project, using Gradle with quite old versions of libraries
I must say, and now switching to Java 11 (using right now OpenJDK 11.0.2 from https://jdk.java.net/archive/).
I want to modularize that library project, adding to all modules a module-info.java.
I keep getting an error in one of the modules with one of the dependencies, Saxon-HE.
I isolated that module in a separate project (using Gradle 7.6), and modified the build.gradle dependencies step by step as IntelliJ discovered
import errors, while using the latest versions of the dependencies.
The build.gradle of the project looks like this:
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}
test {
useJUnitPlatform()
}
The build.gradle of the module looks like this up to the point with the error with Saxon-HE.
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
implementation group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.1'
implementation group: 'org.jdom', name: 'jdom2', version: '2.0.6.1'
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0'
implementation group: 'commons-io', name: 'commons-io', version: '2.11.0'
implementation group: 'org.slf4j', name: 'slf4j-api', version: '2.0.6'
implementation group: 'com.squareup.okhttp3', name: 'okhttp', version: '4.10.0'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}
test {
useJUnitPlatform()
}
The module-info.java looks like this, I used the recommendations of IntelliJ so it added automatically the requires statements:
module mymodule1 {
requires org.apache.commons.lang3;
requires org.apache.commons.io;
requires org.slf4j;
requires okhttp3;
requires okio;
requires java.xml;
requires org.jdom2;
}
The next import error IntelliJ discovers while building it results because one of my classes has the import statement:
import net.sf.saxon.xpath.XPathFactoryImpl;
Building the project results in the error:
error: package net.sf.saxon.xpath does not exist
import net.sf.saxon.xpath.XPathFactoryImpl;
Looking at https://mvnrepository.com/artifact/net.sf.saxon/Saxon-HE/11.4 I added to build.gradle of the module:
implementation group: 'net.sf.saxon', name: 'Saxon-HE', version: '11.4'
In IntelliJ I can see in the Project navigator view, in External Libraries that there is
Gradle: net.sf.saxon.Saxon-HE:11.4
Saxon-HE-11.4.jar
IntelliJ recommends
Add 'requires Saxon.HE' directive to module-info.java
The module-info.java looks now like this:
module mymodule1 {
requires org.apache.commons.lang3;
requires org.apache.commons.io;
requires org.slf4j;
requires okhttp3;
requires okio;
requires java.xml;
requires org.jdom2;
requires Saxon.HE;
}
After that the error in the particular class using that import statement is gone, IntelliJ doesn't complain.
But when then building the project I get the error
C:\Users\ME\PROJECTS\myproject\mymodule1\src\main\java\module-info.java:9: error: module not found: Saxon.HE
requires Saxon.HE;
^
Removing the requires Saxon.HE and building the project results in the error:
error: package net.sf.saxon.xpath is not visible
import net.sf.saxon.xpath.XPathFactoryImpl;
^
(package net.sf.saxon.xpath is declared in the unnamed module, but module net.sf.saxon.xpath does not read it)
I find this error message weired, because it says but module net.sf.saxon.xpath does not read it, I would rather expect but module mymodule1 does not read it.
I don't know what's going wrong, other external dependencies are not problematic but Saxon-HE is.
I found here Gradle build - add module path a snippet which might solved it, but maybe only partially, so not sure if this answer can be marked as the solution.
I added to the project build.gradle
subprojects {
apply plugin: 'java'
sourceCompatibility = 19
targetCompatibility = 19
compileJava {
doFirst {
options.compilerArgs += [
'--module-path', classpath.asPath,
'-Xmaxerrs', 1000
]
classpath = files()
}
}
}
Now trying to build it (with Gradle 7.6 and OpenJDK 19), it doesn't complain to not finding modules, so far at least, but now I have the next problem which I desribed here Java Modules, Gradle, external dependencies - Modules reading from more then one

Gradle to use a jar-with-dependencies in compile task

We have a project that make use of 'jrs-rest-java-client', version: '6.3.1'
The site we used to get the jar from has a certificate issue since September. https://jaspersoft.artifactoryonline.com
We then had to get the jar from a different site.
https://jaspersoft.jfrog.io/
The problem is that a dependency require is missing, but if we use the jar that has "-jar-with-dependencies" it is working. I tried by downloading that jar locally and changing the .gradle to use the local version.
What I would prefer is to have the build to fetch that version directly without having to download first.
How do we specify what jar to use?
dependencies {
compile fileTree(dir: 'lib',
includes: [
'ojdbc8.jar',
])
//compile group: 'com.jaspersoft', name: 'jrs-rest-java-client', version: '6.3.1'
compile group: 'com.jaspersoft', name: 'jrs-rest-java-client', version: '6.3.1', USETHISONE: 'jar-with-dependencies'
//compile files("${buildDir}/jrs-rest-java-client-6.3.1-jar-with-dependencies.jar")
}
I have now tried as suggested;
repositories {
mavenCentral()
// to handle broked jasper reports dependencies
maven {
url 'http://jasperreports.sourceforge.net/maven2'
url 'https://jaspersoft.jfrog.io/jaspersoft/third-party-ce-artifacts/'
url "https://jaspersoft.jfrog.io/jaspersoft/jaspersoft-clients-releases"
}
}
dependencies {
implementation project(':common:project-common-properties')
implementation project(':common:project-common-mail')
implementation fileTree(dir: 'lib', includes: [
'ojdbc8.jar'
])
implementation group: 'com.jaspersoft', name: 'jrs-rest-java-client', version: '6.3.1', classifier: 'jar-with-dependencies'
}
I'm still getting errors at build time...
FAILURE: Build failed with an exception.
* What went wrong:
Could not resolve all files for configuration ':services:notificationService:compileClasspath'.
> Could not find com.jaspersoft.jasperserver:jasperserver-dto:6.3.0.
Required by:
project :services:notificationService > com.jaspersoft:jrs-rest-java-client:6.3.1
That library is not required if the jrs-rest-java-client-6.3.1-jar-with-dependencies.jar is used.
Thanks all,
The solution was, as seen if the video (Thanks!)
adding a new url:
url "https://jaspersoft.jfrog.io/jaspersoft/jrs-ce-releases"
From the jfrog repo, it shows you how to do this:
compile(group: 'com.jaspersoft', name: 'jrs-rest-java-client', version: '6.3.1', classifier: 'jar-with-dependencies')
Add the repo for gradle:
repositories {
jcenter {
name "jaspersoft-releases"
url "https://jaspersoft.jfrog.io/jaspersoft/jaspersoft-clients-releases"
}
}
I'd recommend switching from compile to implementation and using a shorthand to declare the dependency:
implementation "com.jaspersoft:jrs-rest-java-client:6.3.1:jar-with-dependencies"
Give a man a fish and you feed him for a day. Teach him how to fish and you feed him for his life time.
I decided to record a short clip of how I found the appropriate repositories for the artifacts you needed, on jfrog:

How to use gradle 'api' dependency

I tried using the 'api' dependency keyword in my project , but I got this error saying it cannot find method api()
I tried it on a new project. this is the build.gradle file:
plugins {
id 'java'
}
group 'com.test'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
api group: 'com.google.guava', name: 'guava', version: '27.0.1-jre'
}
I am using gradle V4.9.
when I run gradle build I get this:
Could not find method api() for arguments [{group=com.google.guava, name=guava, version=27.0.1-jre}] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler
If I replace 'api' with 'implementation' everything works fine
What am I missing here?
Is there any setting that needs to be done?
The api configuration comes from the java-library plugin, in your build script you have just applied java plugin. See https://docs.gradle.org/current/userguide/java_library_plugin.html
The key difference between the standard Java plugin and the Java Library plugin is that the latter introduces the concept of an API exposed to consumers. A library is a Java component meant to be consumed by other components. It’s a very common use case in multi-project builds, but also as soon as you have external dependencies.
Just apply the java-library plugin (which extends java plugin) and it should work:
plugins {
id 'java-library'
}
For those using kotlin build scripts use the following:
plugins {
id("org.gradle.kotlin.kotlin-dsl")
}

SpringBoot project that includes Apache MetaModel fails to start with java.lang.NoClassDefFoundError: com/google/common/util/concurrent/FutureFallback

I'm building a SpringBoot-based project that will include Apache MetaModel. SpringBoot is version 1.5.8.
Starting up a SpringBoot app works fine, and I can get to published pages. At the moment, the project is pretty much a skeleton app with only very basic features.
However, as soon as I add:
compile 'org.apache.metamodel:MetaModel-full:5.0.0'
to my Gradle dependencies, startup fails with the notorious NoClassDefFound exception for FutureFallback. No other code added, and no configuration added or modified: reproducing the problem simply requires including the compile declaration to cause the startup failure.
I've tried including various versions of Guava and of MetaModel itself, as recommended by other answers, but none of them change the startup failure.
My gradle.build is currently thus:
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.1'
}
}
apply plugin: 'java'
apply plugin: 'org.junit.platform.gradle.plugin'
repositories {
mavenCentral()
}
dependencies {
compile project(':commons')
compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.0.1'
compile 'org.apache.metamodel:MetaModel-full:5.0.0'
compile 'mysql:mysql-connector-java:8.0.8-dmr'
compile 'com.microsoft.sqlserver:mssql-jdbc:6.3.4.jre8-preview'
compile 'org.reflections:reflections:0.9.11'
compile 'com.datastax.cassandra:cassandra-driver-core:3.3.1'
// compile 'com.google.guava:guava:23.4-jre'
compile 'com.google.guava:guava:20.0'
testCompile (
'org.junit.jupiter:junit-jupiter-api:5.0.1',
'org.mockito:mockito-core:2.10.0'
)
testRuntime(
'org.junit.jupiter:junit-jupiter-engine:5.0.1',
'org.mockito:mockito-core:2.10.0'
)
}
Evidence of tinkering with Guava versions is evident. Versions 16, 19, 20 and 23.4-jre have been tried.
Ideas for other avenues of repair greatly appreciated.

Gradle can't find package in dependent test project

I'm trying to run ./gradlew build for a nested multi project structure and I'm running into issues that only seem to appear for projects with test source roots. Being new to both java and gradle I'm sure I'm breaking more than one convention, but I still think this should be working.
Essentially, all the dependencies seem to be added fine, but when I have one project that only has a Test srcDir that depends on another project that has a Test srcDir, it doesn't recognize packages/symbols in that root project. However, projects with regular srcDirs (not test) don't seem to have a problem.
My project has more going on than this, but here is the simplest setup I've tried that illustrates the problem.
My project structure:
qatests
/Applications
/AppGroupName
/AppBasePageObjects
/src
/AppBaseTests
/src
/BasePageObjects
/src
/BaseTests
/src
My settings.gradle in qatests:
rootProject.name = 'QaTests'
include 'BasePageObjects'
include 'BaseTests'
include 'Applications:AppGroupName'
include 'Applications:AppGroupName:AppBasePageObjects'
include 'Applications:AppGroupName:AppBaseTests'
My build.gradle in qatests:
version '1.0-SNAPSHOT'
allprojects {
repositories {
mavenCentral()
}
}
subprojects{
if(project.name.contains("Tests")){
apply plugin: 'java'
sourceCompatibility = 1.8
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '2.+'
}
if(project.name != "BaseTests")
{
println "$project.name depends on BaseTests"
dependencies {
testCompile project(':BaseTests')
}
}
sourceSets {
test {
java {
srcDir 'src'
}
}
}
}
if(project.name.contains("PageObjects")){
apply plugin: 'java'
sourceCompatibility = 1.8
dependencies {
compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '2.+'
}
if(project.name !="BasePageObjects")
{
dependencies {
compile project(':BasePageObjects')
}
}
sourceSets {
main {
java {
srcDir 'src'
}
}
}
}
}
I won't include my build.grade for the BaseTest project since that seems to compile fine during the gradlew build, but here is my build.gradle for AppBaseTests:
version '1.0-SNAPSHOT'
dependencies {
compile 'org.apache.commons:commons-lang3:3.4'
compile project(':Applications:AppGroupName:AppBasePageObjects')
}
When I run ./gradlew build in the qatests root, the BaseTests, BasePageObjects, and AppBasePageObjects projects seem to compile fine and AppBasePageObjects successfully uses packages and symbols from BasePageObjects. For some reason however, AppBaseTests can't seem to recognize packages and symbols in BaseTests.
If I clone this project from scratch, IntelliJ runs the gradle scripts scripts automatically and everything seems to work out of the box just fine with the dependencies, so this just confuses me even more.
I've tried adding compile and testcompile for all the project dependencies since that's the only real difference between AppBasePageObjects which works and AppBaseTests which doesn't work. I've also tried adding compileJava.dependsOn(':BaseTests:build') to the AppBaseTests build.gradle file. And a few other rabbit holes, but nothing seems to have any effect on this dependency issue.
For what it's worth, here is the first error I see in the actual build:
error: package Tests does not exist import Tests.BaseTest;
Any help or suggestions are greatly appreciated. If you would like to distribute some harsh insults I'll take those as well. Thank you.
I believe I found an answer while reading another solution here:
Multi-project test dependencies with gradle
It seems I needed to use testCompile project(':BaseTests').sourceSets.test.output for command line builds as well as testCompile project(':BaseTests') for IDE functionality in my root build.gradle file. This only seems to be required for the test projects.
Here are the actual changes in my root build.gradle file:
if(project.name != "BaseTests")
{
println "$project.name depends on BaseTests"
dependencies {
testCompile project(':BaseTests')
testCompile project(':BaseTests').sourceSets.test.output
}
}
This seems a bit hacky but works, if somebody has a more thorough and intelligent answer please post!

Categories