Gradle 5.0: java.lang.String cannot be cast to class groovy.lang.Closure (lombok issue) - gradle

I am encountering error during Gradle 5.0 upgrade (from 4.10.2) and have isolated the code to the following block:
dependencyManagement {
dependencies {
compileOnly "org.projectlombok:lombok:1.18.2"
testCompileOnly "org.projectlombok:lombok:1.18.2"
annotationProcessor "org.projectlombok:lombok:1.18.2"
testAnnotationProcessor "org.projectlombok:lombok:1.18.2"
dependency "com.google.guava:guava:$guavaVersion"
dependency "mysql:mysql-connector-java:$mysqlversion"
dependency "com.google.inject:guice:${guiceVersion}"
dependency "org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:2.1.1.RELEASE"
}
imports {
mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
mavenBom "com.amazonaws:aws-java-sdk-bom:${awsVersion}"
}
}
Without the lombok lines, the code works with no issue.
dependencyManagement {
dependencies {
dependency "com.google.guava:guava:$guavaVersion"
dependency "mysql:mysql-connector-java:$mysqlversion"
dependency "com.google.inject:guice:${guiceVersion}"
dependency "org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:2.1.1.RELEASE"
}
imports {
mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
mavenBom "com.amazonaws:aws-java-sdk-bom:${awsVersion}"
}
}
The error is class java.lang.String cannot be cast to class groovy.lang.Closure (java.lang.String is in module java.base of loader 'bootstrap'; groovy.lang.Closure is in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader #675d3402)
I am changing the way lombok is declared due to changes in Gradle 5 annotation processing.

In the dependencies block under dependencyManagement there should not be any concrete dependency declaration eg. compileOnly, testCompileOnly etc. Only dependency is allowed as it is where you define all the - well - dependencies with the proper version for later use in the sub-projects.

Related

How to override a plugin dependency in a Gradle conventions plugin

I'm using Gradle's conventions as described here:
https://docs.gradle.org/current/samples/sample_convention_plugins.html
And in the convention plugin, I'm applying a plugin (io.freefair.gradle:lombok-plugin:6.5.1) however I now need to override a dependency that it uses (I need org.projectlombok:lombok:1.18.22 not 1.18.24)
I've tried this:
buildscript {
dependencies {
classpath 'org.projectlombok:lombok:1.18.22'
}
}
plugins {
id 'groovy-gradle-plugin'
}
...
dependencies {
implementation 'io.freefair.gradle:lombok-plugin:6.5.1'
...
implementation 'org.projectlombok:lombok:1.18.22'
}
But version 1.18.24 was used. I've also tried adding this to my build.gradle:
buildscript {
dependencies {
classpath 'org.projectlombok:lombok:1.18.22'
}
}
plugins {
id 'billforward.java-conventions'
}
but still 1.18.24 was used.
As an aside, the two underlying issues I'm trying to solve are:
https://github.com/freefair/gradle-plugins/issues/549 - Resolved in 6.5 of lombok-plugin
https://github.com/projectlombok/lombok/issues/3180 - Broken in 1.18.24 of lombok
The lombok version used by io.freefair.lombok can be customized by using the lombok extension property:
plugins {
id "io.freefair.lombok" version "6.5.1"
}
lombok.version = "1.18.22"
This is documented here: https://docs.freefair.io/gradle-plugins/6.5.1/reference/#_io_freefair_lombok_base
Adding lombok itself to the buildscript classpath (or the runtime classpath of your gradle plugin) will not do anything.

Gradle Spring Boot Dependency Not found Multi Module Project

I am trying to set up multimodule project
RootProject Settings
rootProject.name = 'Abc'
include 'catalog'
include 'catalog-common'
Root Project Abc/build.gradle
plugins {
id 'org.springframework.boot' version '2.7.3' apply false
id 'io.spring.dependency-management' version '1.0.13.RELEASE'
id 'java'
}
subprojects {
group = 'com.abc'
apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'
sourceCompatibility = 1.8
targetCompatibility = 1.8
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
dependencyManagement {
imports {
mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
mavenBom "org.springframework.cloud:spring-cloud-dependencies:2021.0.3"
}
}
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
}
}
Module catalog-common
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation('org.springframework.boot:spring-boot-starter-validation')
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
Module Catalog
plugins {
id 'org.springframework.boot' version '2.7.3'
}
dependencies {
implementation project(':catalog-common')
}
in Catalog Project it is expecting again to define spring dependencies but where as I am able to access java static classes
Please help
It looks like your catalog-common is a kind of "library" module, consumed by other sub-projects (catalog and maybe others). If so, you should use the Java Library plugin which can be used for this purpose. You will then need to configure all dependencies you want to "inherit" in consumer projects using api configuration instead of implementation. Dependencies declared in implementation will not leak into consumer projects, this is the expected Gradle behavior.
In you example, catalog-common build script should look like:
plugins {
id("java-library")
}
dependencies {
// choose between api or implementation, depending on the scope you want for each dependency
api 'org.springframework.boot:spring-boot-starter'
api 'org.springframework.boot:spring-boot-starter-actuator'
api 'org.springframework.boot:spring-boot-starter-web'
implementation('org.springframework.boot:spring-boot-starter-validation')
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
implementation 'org.springframework.boot:spring-boot-starter-test'
}
Please note that it seems a bit strange to configure some of the Spring dependencies like actuator in this common library: this should declared only in the main "application" project ( catalog in your case), unless you want to implement some common code depending on actuator in the catalog-common module.

Gradle build fails due to lombok

I'm using Gradle 4.10.2 (but would be fine using the latest version to get it to work). Here's my gradle file:
plugins {
id 'org.springframework.boot' version '2.2.5.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
jcenter()
}
ext {
set('springCloudVersion', "Hoxton.SR3")
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
runtime 'org.springframework.boot:spring-boot-devtools'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
When I execute bootJar, it fails with a lot of errors. The errors are all due to not finding getters, setters, etc. that are auto generated by lombok. How can I resolve the errors? I've seen other posts about this issue and they all recommend adding the lombok dependency as a annotationProcessor & compileOnly but I've already done this and still have this issue.
Update
Here are a couple of errors that I get:
C:\Users\user\eclipse-workspace\example\src\main\java\com\example\proj\service\CarService.java:60: error: cannot find symbol
log.debug("calling for cars {} ", cars);
^
symbol: variable log
location: class CarService
C:\Users\user\eclipse-workspace\example\src\main\java\com\example\proj\service\CarService.java:66: error: cannot find symbol
CarDtoBuilder dtoBuilder = dtoBuilderByCar.getOrDefault(
^
symbol: class CarDtoBuilder
location: class CarService
log should come from annotation #Slf4j. And the CarDtoBuilder is from #Builder(builderMethodName = "hiddenBuild") annotated on CarDto. Both are lombok annotations.
Update 2
Now trying Gradle 4.10.3. Same result. Here's output from gradle -v:
------------------------------------------------------------
Gradle 4.10.3
------------------------------------------------------------
Build time: 2018-12-05 00:50:54 UTC
Revision: e76905e3a1034e6f724566aeb985621347ff43bc
Kotlin DSL: 1.0-rc-6
Kotlin: 1.2.61
Groovy: 2.4.15
Ant: Apache Ant(TM) version 1.9.11 compiled on March 23 2018
JVM: 1.8.0_222 ( 25.222-b10)
OS: Windows 10 10.0 amd64
Update 3
I've tried this using Gradle 6 same result. I've tried using Windows and Mac... same result. I've tried using JDK 11 instead of JDK 8... same result.
Update 4
I'm using #Builder lombok annotation. I wonder if that is causing an issue.
I don't see your lombok-plugin configuration on your build.gradle file.
I had to setup lombok on my project for gradle 4.10.3 and this is what I did:
group 'io.metadata'
version '1.11-SNAPSHOT'
apply plugin: 'java'
project.ext.set("spring.boot.version", "2.2.6.RELEASE")
project.ext.set("spring.core.version", "5.2.5.RELEASE")
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath("io.freefair.gradle:lombok-plugin:5.0.0-rc4")
}
}
subprojects {
repositories {
mavenLocal()
mavenCentral()
}
apply plugin: 'java'
apply plugin: 'io.freefair.lombok'
}
Check the apply plugin: 'io.freefair.lombok' and the dependencies defined for it on buildscript.
Note that I am using multi module project but either way you can use lombok plugin and apply it.
It is not mandatory to use lombok-plugin but it works very well and it's easy to do. You can follow the documentation here:
https://plugins.gradle.org/plugin/io.freefair.lombok
Remove any references to static imports involving code generated by lombok. In my case, I had:
import static com.example.car.dto.CarDto.CarDtoBuilder;
CarDtoBuilder was generated via lombok #Builder on the CarDto class. Once resolved, the other lobmok compile related issues such as log symbol not found (created by #Slf4j) go away.

Quarkus dependency injection failing with bean from Gradle Library Module

I´m trying to get dependency injection working in my multi-module project where I want to inject a bean from a library module. However, it´s failing because it cannot find the bean.
project root settings.gradle
pluginManagement {
repositories {
mavenLocal()
mavenCentral()
gradlePluginPortal()
}
plugins {
id 'io.quarkus' version "${quarkusPluginVersion}"
}
}
include ':service-module'
include ':library-module'
service-module build.gradle
Tried compile, as well as implementation
dependencies {
compile project(":library-module")
// implementation project(":library-module")
}
Bean from library-module
import javax.enterprise.context.ApplicationScoped;
#ApplicationScoped
public class LibraryBean {
public void hello() {
System.out.println("Hello");
}
}
service-module where injection happens
#ApplicationScoped
public class Application {
#Inject
LibraryBean libraryBean;
}
Stacktrace
Unsatisfied dependency for type com.mylibrary.LibraryBean and
qualifiers [#Default]
- java member: com.myservice.Application#LibraryBean
- declared on CLASS bean [types=[com.myservice.Application, java.lang.Object], qualifiers=[#Default, #Any],
target=com.myservice.Application]
I´m not sure if this issue is Quarkus-related or a general problem that exists with CDI and Gradle modules.
How can I make the DI working accross modules?
Do you have a beans.xml file in your library module? See this question/answer for more info: https://stackoverflow.com/a/55513723/742081

Spring Cloud Stream Configuration classes are not being loaded

After updating from Spring Boot 2.0.6 / Spring Cloud Finchley.RELEASE to Spring Boot 2.1.3 / Spring Cloud Greenwich.SR1, I am experiencing a very odd configuration / component scanning issue.
The application is structured as follows:
main-application
messaging-lib
commons-lib
Main application is annotated with #SpringBootApplication(scanBasePackages = {"com.app.libs", "com.app"}), where the com.app packages are those under the main-application, while com.app.libs are the packages of the libs.
The commons-lib configuration which is simply a framework-esque library has the following main configuration entrypoint:
package com.app.libs.commons
#Target(ElementType.TYPE)
#Retention(RetentionPolicy.RUNTIME)
#Documented
#Inherited
#Import(CommonsConfiguration.class)
public #interface EnableCommons {
}
With CommonsConfiguration having:
package com.app.libs.commons.config
#Configuration
#ConditionalOnProperty(value = "com.app.config.commons", matchIfMissing = true)
#ComponentScan(basePackages = "com.app.libs.commons")
public class FrameworkConfiguration {
This way, I can basically annotate a single configuration class in my main-application and have it pick up common beans.
The messaging-lib configuration which uses Spring Cloud Stream has the following configurations:
#Configuration
#Import({ CustomBinding.class })
#PropertySource("classpath:kafka.properties")
#EnableBinding(Source.class)
#EnableAutoConfiguration
public class BindingConfiguration {
}
The cloud stream configuration is as follows:
spring.cloud.stream.bindings.output.destination=${spring.application.name}
spring.cloud.stream.kafka.bindings.output.producer.sync=true
spring.cloud.stream.default.group=${spring.application.name}
spring.cloud.stream.default.producer.partitionCount=9
spring.cloud.stream.default.producer.partitionKeyExpression=headers.entityId
spring.cloud.stream.kafka.binder.autoAddPartitions=true
spring.cloud.stream.kafka.binder.autoCreateTopics=false
When starting the application, the following error message is produced:
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to bind properties under 'spring.cloud.stream.default.producer.partition-key-expression' to org.springframework.expression.Expression:
Property: spring.cloud.stream.default.producer.partitionkeyexpression
Value: headers.entityId
Origin: "spring.cloud.stream.default.producer.partitionKeyExpression" from property source "class path resource [kafka.properties]"
Reason: No converter found capable of converting from type [java.lang.String] to type [#com.fasterxml.jackson.databind.annotation.JsonSerialize org.springframework.expression.Expression]
Action:
Update your application's configuration
After some looking through the source code, I've noticed that the SpelConverter in question is the one that is supposed to be initialized in SpelExpressionConverterConfiguration. Indeed, if I add #Import({ SpelExpressionConverterConfiguration.class }) or #Import({ BindingServiceConfiguration }) (the #Configuration class that imports the SpelExpressionConverterConfiguration) to my BindingConfiguration, the converter seems to get initialized. However, another error is then produced:
***************************
APPLICATION FAILED TO START
***************************
Description:
A component required a bean of type 'org.springframework.messaging.core.DestinationResolver' that could not be found.
The following candidates were found but could not be injected:
- Bean method 'binderAwareChannelResolver' in 'BindingServiceConfiguration' not loaded because #ConditionalOnBean (types: org.springframework.cloud.stream.binder.BinderTypeRegistry; SearchStrategy: current) did not find any beans of type org.springframework.cloud.stream.binder.BinderTypeRegistry
Action:
Consider revisiting the entries above or defining a bean of type 'org.springframework.messaging.core.DestinationResolver' in your configuration.
I'm not quite sure what seems to be going on. When debugging, it seems that both BindingBeansRegistrar and BinderFactoryConfiguration (the classes imported through #EnableBinding) are loaded - yet no scanning occurs and the rest of the component scanning simply does not seem to happen.
I've tried importing the configuration classes and/or scanning the packages myself, but in both cases I am still missing the DestinationResolver bean, although it should have been initialized.
I've noted that, prior to the update, there were also some issues: I had to import BindingServiceConfiguration, BinderFactoryConfiguration, and SpelExpressionConverterConfiguration manually as well.
Would anybody have pointers as to what could be causing this issue?
EDIT: Here are the build.grade files (some includes are omitted for brevity):
Application build.gradle
apply from: new File(project(':scripts').projectDir, '/service-impl.gradle')
dependencies {
implementation project(':dependency-A')
implementation project(':dependency-B')
implementation project(':messaging-library')
testImplementation 'org.springframework.cloud:spring-cloud-stream-test-support'
}
service-impl.gradle contents:
buildscript {
apply from: new File(project(':buildscripts').projectDir, '/repositories.gradle') // Repository definitions
apply from: new File(project(':buildscripts').projectDir, '/dm-boot.gradle') // DM via spring boot plugin
apply from: new File(project(':buildscripts').projectDir, '/dm-versions.gradle') // DM versions
}
configurations {
all*.exclude module: 'spring-boot-starter-tomcat'
all*.exclude group: 'org.apache.bval'
}
artifacts {
archives bootJar
}
dependencies {
compileOnly 'org.projectlombok:lombok'
compile 'org.springframework.boot:spring-boot-starter'
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.boot:spring-boot-starter-jetty'
compile 'org.springframework.boot:spring-boot-starter-actuator'
compile 'org.springframework.boot:spring-boot-starter-cache'
compile 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
compile 'org.springframework.cloud:spring-cloud-starter-config'
testCompile 'junit:junit'
testCompile 'org.springframework.boot:spring-boot-starter-test'
testCompileOnly 'org.projectlombok:lombok'
compile 'net.logstash.logback:logstash-logback-encoder'
}
Commons Library build.gradle
apply from: new File(project(':buildscripts').projectDir, '/repositories.gradle') // Repository definitions
apply from: new File(project(':buildscripts').projectDir, '/dm-plain.gradle') // DM via spring boot plugin
apply from: new File(project(':buildscripts').projectDir, '/dm-versions.gradle') // DM versions
dependencies {
api 'com.restfb:restfb:2.3.0'
api 'commons-io:commons-io'
implementation 'com.jcraft:jsch:0.1.54'
//lombok
compileOnly 'org.projectlombok:lombok'
//spring
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-jetty'
implementation 'org.springframework.boot:spring-boot-starter-security'
api 'org.springframework.security:spring-security-core'
api 'org.springframework.security:spring-security-web'
api 'org.springframework:spring-jdbc'
api 'org.springframework.boot:spring-boot-starter-actuator'
//thrift modules
compileOnly 'com.facebook.swift:swift-codec'
compileOnly 'com.facebook.swift:swift-service'
//db modules
api 'org.postgresql:postgresql'
api 'org.flywaydb:flyway-core'
api 'com.zaxxer:HikariCP'
api 'org.jooq:jooq'
//embedded pg modules
compileOnly 'com.opentable.components:otj-pg-embedded'
api group: 'net.minidev', name: 'json-smart', version: '2.2.1'
api group: 'org.json', name: 'json', version: '20140107'
//tests
testImplementation 'junit:junit'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'com.opentable.components:otj-pg-embedded'
testImplementation 'org.springframework.security:spring-security-core'
testImplementation 'org.springframework.security:spring-security-web'
testImplementation 'org.springframework.security:spring-security-config'
testImplementation 'org.springframework:spring-jdbc'
testImplementation 'org.springframework.boot:spring-boot-starter-actuator'
testImplementation 'org.apache.sshd:sshd-core:1.7.0' // sftpFileTransfer testing - embedded sftp server
testCompileOnly 'org.projectlombok:lombok'
testCompileOnly 'javax.servlet:javax.servlet-api'
}
Messaging Lib build.gradle (used as a standalone lib, hence does not use the above .gradle files)
buildscript {
ext {
springBootVersion = '2.1.3.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply from: new File(rootDir.parentFile, 'buildscripts/publish.gradle')
apply from: new File(rootDir.parentFile, 'buildscripts/coverage.gradle')
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.app.libs.messaging'
version = '0.0.3'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
ext {
springCloudVersion = 'Greenwich.SR1'
jacksonVersion = '2.9.6'
lombokVersion = '1.16.18'
jooqVersion = '3.11.2'
flywayVersion = '5.2.1'
}
dependencies {
compile 'org.aspectj:aspectjweaver'
compile 'org.springframework:spring-tx'
compile 'org.springframework.cloud:spring-cloud-stream'
compile 'org.springframework.cloud:spring-cloud-stream-binder-kafka'
compile 'org.apache.commons:commons-lang3:3.5'
compile 'org.javassist:javassist:3.22.0-GA'
compile "org.flywaydb:flyway-core:${flywayVersion}"
compileOnly "org.jooq:jooq:${jooqVersion}"
compileOnly "org.projectlombok:lombok:${lombokVersion}"
testCompileOnly "org.projectlombok:lombok:${lombokVersion}"
testCompile 'org.springframework.boot:spring-boot-starter-test'
testCompile 'org.springframework.boot:spring-boot-starter-json'
testCompile 'org.springframework.cloud:spring-cloud-stream-test-support'
testCompile "com.fasterxml.jackson.core:jackson-core:${jacksonVersion}"
testCompile "com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}"
testCompile 'junit:junit:4.12'
testCompile "org.jooq:jooq:${jooqVersion}"
testCompile "org.flywaydb:flyway-core:${flywayVersion}"
testCompile 'org.postgresql:postgresql:42.2.2'
testCompile 'com.opentable.components:otj-pg-embedded:0.13.0'
testCompile 'org.springframework:spring-jdbc'
}
dependencyManagement {
// disable maven exclusion to enhance gradle import performance
// https://github.com/spring-gradle-plugins/dependency-management-plugin/issues/153
applyMavenExclusions = false
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
jar.enabled = true
bootJar.enabled = false
I've tried to omit any files that I think should be unrelated.

Resources