Gradle build fails due to lombok - spring-boot

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.

Related

Could not resolve: org.thymeleaf.extras:thymeleaf-extras-springsecurity6:3.1.0.RELEASE

what is the problem, i just changed group name here, i get could not resolve for springsecuriy extras
i can't see why
plugins {
id 'java'
id 'org.springframework.boot' version '3.0.0'
id 'io.spring.dependency-management' version '1.1.0'
id 'org.hibernate.orm' version '6.1.5.Final'
id 'org.graalvm.buildtools.native' version '0.9.18'
}
group = '*********'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6'
runtimeOnly 'org.mariadb.jdbc:mariadb-java-client'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
}
tasks.named('test') {
useJUnitPlatform()
}
hibernate {
enhancement {
lazyInitialization true
dirtyTracking true
associationManagement true
}
}
Could not resolve all files for configuration ':compileClasspath'. > Could not resolve org.thymeleaf.extras:thymeleaf-extras-springsecurity6:3.1.0.RELEASE. Required by: project : > Could not resolve org.thymeleaf.extras:thymeleaf-extras-springsecurity6:3.1.0.RELEASE. > Could not parse POM repo.maven.apache.org/maven2/org/thymeleaf/extras/… > Could not find org.springframework.security:spring-security-bom:6.0.0-RC2
I tried nothing it is made by spring initializer and boot version 3.0.0
Thymeleaf 3.1.1 is released now, pls use
implementation("org.thymeleaf.extras:thymeleaf-extras-springsecurity6:3.1.1.RELEASE")
instead of
implementation("org.thymeleaf.extras:thymeleaf-extras-springsecurity5")
I resolved problem by changing gradle build
i replaced repositories with this
repositories {
mavenCentral()
maven { url "https://repo.spring.io/milestone"
content {
// Thymeleaf uses 6.0.0-RC2 of Security's bom in its dependency management
includeModule("org.springframework.security", "spring-security-bom")
}
}
}
I hope it help others

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.

How to upgrade existing Micronaut application to new Micronaut version 1.1.2?

I have been upgrading like this..
plugins {
id "io.spring.dependency-management" version "1.0.6.RELEASE"
id "com.github.johnrengelman.shadow" version "4.0.2"
}
apply plugin:"application"
apply plugin:"groovy"
version "0.1"
group "com.shanky"
repositories {
mavenLocal()
mavenCentral()
maven { url "https://jcenter.bintray.com" }
}
dependencyManagement {
imports {
mavenBom 'io.micronaut:micronaut-bom:1.1.2'
}
}
dependencies {
compile "io.micronaut:micronaut-runtime-groovy"
compile "io.micronaut:micronaut-validation"
compile "io.micronaut:micronaut-http-client"
compile "io.micronaut:micronaut-http-server-netty"
compileOnly "io.micronaut:micronaut-inject-groovy"
runtime "ch.qos.logback:logback-classic:1.2.3"
testCompile "io.micronaut:micronaut-inject-groovy"
testCompile("org.spockframework:spock-core") {
exclude group: "org.codehaus.groovy", module: "groovy-all"
}
compile 'io.micronaut:micronaut-views'
runtime 'org.thymeleaf:thymeleaf:3.0.11.RELEASE'
compile "io.micronaut.configuration:micronaut-hibernate-validator"
compile "io.micronaut.configuration:micronaut-hibernate-gorm"
runtime 'org.postgresql:postgresql:42.2.5'
annotationProcessor "io.micronaut:micronaut-security"
compile "io.micronaut:micronaut-security"
compile "io.micronaut:micronaut-security-session"
runtime "org.hibernate:hibernate-ehcache:5.1.9.Final"
}
shadowJar {
mergeServiceFiles()
}
run.jvmArgs('-noverify', '-XX:TieredStopAtLevel=1')
mainClassName = "com.shanky.Application"
tasks.withType(GroovyCompile) {
groovyOptions.forkOptions.jvmArgs.add('-Dgroovy.parameters=true')
}
So far this is my build.gradle file. I have updated mavenBom to upgrade Micronaut version. I've create a dummy project with Micronaut version 1.1.2 just to differentiate build.gradle file. I don't see any major difference in between them.
Any help would be appreciated
But it is not upgrading.
That is unlikely to be correct. It may be that you aren't seeing the effect of that change in your IDE if the IDE is out of sync with your Gradle build but changing that version number in build.gradle does in fact upgrade the project.

Gradle spring boot 2 spring-boot-dependencies cannot download dependencies

I am using eclipse 2018, gradle 5.2.1, buildship 3.0.1.
My config looks like:
I try to create spring boot 2 according to building-spring-boot-2-projects-with-gradle
The build.gradle is:
plugins {
id 'java'
id 'com.gradle.build-scan' version '2.1'
id 'org.springframework.boot' version '2.1.3.RELEASE'
}
repositories {
jCenter()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-dependencies:2.1.3.RELEASE'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
components {
withModule('org.springframework:spring-beans') {
allVariants {
withDependencyConstraints {
// Need to patch constraints because snakeyaml is an optional dependency
it.findAll { it.name == 'snakeyaml' }.each { it.version { strictly '1.23' } }
}
}
}
}
}
buildScan {
// always accept the terms of service
termsOfServiceUrl = 'https://gradle.com/terms-of-service'
termsOfServiceAgree = 'yes'
// always publish a build scan
publishAlways()
}
bootJar {
mainClassName = 'gt4.App'
}
However, after I save build.gradle, the Project and External Dependencies disappear, and the spring boot jars are not downloaded too.
What I was wrong?
If I create spring boot project with gradle by Spring Tool Suite 4, the generated build.gradle is:
plugins {
id 'org.springframework.boot' version '2.1.3.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
then it works.
Do I use spring-boot-dependencies wrong?
try to deleting the local Repository and build project again to download dependancy.
Delete .m2 or .gradle folder and then Rebuild your project.
a)On a Windows machine, the .gradle or .m2 path will be:
c:\Users\username\.m2 or c:\Users\username\.m2
b)On linux machine, the .gradle or .m2 path will be:
USER_HOME/.m2/

How can I generate JPA metamodel in spring-boot with gradle?(+ lombok)

I want add JPA metamodel to my project - Spring boot + gradle
I find a lot of examles how can i do it but all with Maven. Also I find this site: https://plugins.gradle.org/search?term=metamodel
and try first three plugins. With each plugins I get errors: error: cannot find symbol in classes marked lombok #Builder annotation and some classes is not entity. It is example some plugin:
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "com.github.iboyko.gradle.plugins:jpamodelgen-plugin:1.0.1"
}
}
dependencies {
compile ('org.hibernate:hibernate-jpamodelgen')
}
1) Which plugin or method is the most official (correct) to create JPA metamodel in Spring boot + spring-data-jpa + gradle ?
2) How can I specify only the package with entities and not scan another classes?
3) how to make friends with it with lombok?
EDIT
I add this code to gradle file:
sourceSets.configureEach { sourceSet ->
tasks.named(sourceSet.compileJavaTaskName).configure {
options.annotationProcessorGeneratedSourcesDirectory = file("$buildDir/generated/sources/java")
}
}
and it generate classes_ fine. After that I mark
generated/sources/java
folder ass root of generated classes(rigth clik to this folder and mark as)
After that I try import generated classes in my repository and IDE show this:
For each module I have 2 module - my_module and my_module_main(I don't understand why) and all classes generate in my_module but all code in my_module_main. If I add this dependency - I have this:
and in generated class I have this:
I was struggling through the same issue. Finally I got it working by adding both dependencies in both compileOnly and annotationProcessor forms. Don't ask me why, but it somehow works.
compileOnly 'org.projectlombok:lombok:1.18.10'
annotationProcessor 'org.projectlombok:lombok:1.18.10'
compileOnly 'org.hibernate:hibernate-jpamodelgen'
annotationProcessor('org.hibernate:hibernate-jpamodelgen')
Did you use Spring Initalizr to generate the project?
This is what you get from Initializr (expect the jpamodelgen that I added by myself):
buildscript {
ext {
springBootVersion = '2.1.1.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
implementation('org.springframework.boot:spring-boot-starter-data-jpa')
runtime('com.h2database:h2')
compileOnly('org.projectlombok:lombok')
compileOnly ('org.hibernate:hibernate-jpamodelgen')
testImplementation('org.springframework.boot:spring-boot-starter-test')
}
// Add source set to define where the generated source code should go to
sourceSets.configureEach { sourceSet ->
tasks.named(sourceSet.compileJavaTaskName).configure {
options.annotationProcessorGeneratedSourcesDirectory = file("$buildDir/generated/sources/java")
}
}
You can find a demo project on GitHub:
https://github.com/simasch/demo-gradle-jpa
In IntelliJ you can right click on generated/sources/java and choose

Resources