NoSuchMethodError - Intellij and lombok - gradle

I am getting a weird issue in IntelliJ. I am using Lombok (1.18.2), IntelliJ(2018.2.2) and JUnit 5 in gradle 4 multi-module project.
It took me hours to replicate this.
Run all test case with coverage.
Following a pop up will come
if I press OK and re-run the unit test case. It will start failing with java.lang.NoSuchMethodError for the getters and setters.
removing Lombok and generating getter and setter after this has no effect as well.
Tried everything to fix this (enable annotation processor, Lombok plugin, clean build) but no luck.
EDIT 1:
Root build.gradle:
implementation 'org.projectlombok:lombok:1.18.2'
testImplementation('org.junit.jupiter:junit-jupiter-api:5.3.1')
testImplementation('org.junit.jupiter:junit-jupiter-params:5.3.1')
testRuntime('org.junit.jupiter:junit-jupiter-engine:5.3.1')
testImplementation("org.junit.platform:junit-platform-launcher:1.3.1")
testImplementation("org.junit.vintage:junit-vintage-engine:5.3.1")
testImplementation "org.mockito:mockito-core:2.+"
testImplementation('org.mockito:mockito-junit-jupiter:2.22.0')
sub-project build.gradle
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'

I solved this issue by making following changes:
Enable annotation processing in Intellij.(File | Settings | Build, Execution, Deployment | Compiler | Annotation Processors)
select processor path and point to lombok-1.18.4.jar because I just want to enable it for Lombok and not for Mapstruct. If enabled for both then it generates the Mapstruct classes in default location.
under File "Invalidate cache and restart"
under Build option "Rebuild project"
This will solve it. It took me a while to make it working in Intellij.

Related

Gradle dependency visible only for annotation processor

I want to add a dependency to Gradle project that is visible to the annotation processor during the processing.
But at the same time I do not want this dependency to be accessible from the source code.
How can this be done?
If you are using a recent version of Gradle, then annotation processor dependencies are declared in a separate configuration annotationProcessor that is only resolved for that purpose.
Versions prior to 4.6 used to find them from the compile classpath, and if you have to use old versions, I don't think there is much you can do.
Example for 4.6+:
dependencies {
annotationProcessor 'com.google.dagger:dagger-compiler:2.8'
}
If your annotation processor requires any other dependencies for compiling your source code, you have no choice but to add them to the compile classpath, which will make them visible in an IDE. But you can use the compileOnly configuration to limit the scope so they won't be visible at runtime or selected as a transitive dependency. Example:
dependencies {
compileOnly 'org.projectlombok:lombok:1.18.10'
annotationProcessor 'org.projectlombok:lombok:1.18.10'
}

java.lang.NoSuchMethodError: org.mockito.MockingDetails.getMockCreationSettings() - Gradle links same class twice for Mockito jar

I have configured the following dependencies in the build.gradle file.
testCompile "org.springframework.boot:spring-boot-starter-test"
testCompile "org.mockito:mockito-all:1.10.19"
And when running the tests getting the following error stack trace.
java.lang.NoSuchMethodError: org.mockito.MockingDetails.getMockCreationSettings()Lorg/mockito/mock/MockCreationSettings;
at org.springframework.boot.test.mock.mockito.MockReset.get(MockReset.java:107)
at org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.resetMocks(ResetMocksTestExecutionListener.java:81)
at org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.resetMocks(ResetMocksTestExecutionListener.java:69)
at org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.beforeTestMethod(ResetMocksTestExecutionListener.java:56)
at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:289)
And I've tried to debug it. Found something weird in the IntelliJ linked jar files. Almost most of the classes in the Mockito jar displayed twice.
Refreshing gradle dependencies or cache clear both did not work.
After so many days, I figured out that the issue is with the mockito jar version mismatch.
Changing the version from mockito-all to mockito-core build.gradle to
testCompile "org.mockito:mockito-core:2.24.0"
testCompile ("org.springframework.boot:spring-boot-starter-test") {
exclude group: "org.mockito", module: "mockito-all"
}
fixed my problem.
Thanks

IntelliJ IDE 2018.3 Ultimate + Gradle 5.2.1 AND compileOnly

I was about building a gradle 5 project in IDEA.
The lombok dependency was set to compileOnly in the build.gradle file and running it resulted in success from command line and IDEA gradle tool as well.
"Funny" fact, that lombok isn't included in Source Sets, so my lombok imports are failing....
I googled for it, and in theory since IDEA 2017.2 + Gradle 4 it's a fixed issue, IDEA should pick up compileOnly dependencies as well.
In the reality, it does not work.
Any Gradle plugin, or idea to solve this issue? :)
Thanks.
ui.:
VERSION 1
Using compileOnly & annotationProcessor both for prod code and tests resulted in
from command-line, gradle is able to run everything with SUCCESS
from IDEA, it's impossible, imports are failing with lombok
VERSION 2
Using compile & annotationProcessor
from command-line, gradle is able to run everything with SUCCESS
from IDEA, imports are OK, everything is fine
dependencies {...compileOnly "org.projectlombok:lombok:${lombokVersion}"
annotationProcessor("org.projectlombok:lombok:${lombokVersion}")
testCompileOnly "org.projectlombok:lombok:${lombokVersion}"
testAnnotationProcessor("org.projectlombok:lombok:${lombokVersion}")...}
According to https://projectlombok.org/setup/gradle
you need to write in your build.gradle
dependencies {
compileOnly 'org.projectlombok:lombok:1.18.6'
annotationProcessor 'org.projectlombok:lombok:1.18.6'
}
It works for me
You can upgrade the IDEA version to 2019.1 or use the 4.x version of Gradle.

Configure gradle with annotationProcessor and log4j2 PluginProcessor

I was trying to build my project with gradle. I got this error:
"Detecting annotation processors on the compile classpath has been deprecated. Gradle 5.0 will ignore annotation processors on the compile classpath. The following annotation processors were detected on the compile classpath: 'org.apache.logging.log4j.core.config.plugins.processor.PluginProcessor'. Please add them to the annotation processor path instead. If you did not intend to use annotation processors, you can use the '-proc:none' compiler argument to ignore them."
I got that I have to use annotattionProcessor instead of compile. But how actually to do it.
Here is the part of my gradle file dependencies:
dependencies {
implementation 'org.apache.logging.log4j:log4j-api:2.11.1'
implementation 'org.apache.logging.log4j:log4j-core:2.11.1'
}
I was trying to use it like this (implicit way), but it doesn't work
dependencies {
implementation 'org.apache.logging.log4j:log4j-api:2.11.1'
implementation 'org.apache.logging.log4j:log4j-core:2.11.1'
annotationProcessor 'org.apache.logging.log4j.core:config.plugins.processor.PluginProcessor'
}
Could anyone help me?
The answer was pretty simple:
dependencies {
implementation 'org.apache.logging.log4j:log4j-api:2.11.1'
annotationProcessor 'org.apache.logging.log4j:log4j-core:2.11.1'
}

Resolving warning for Micronaut and Gradle 5 "Detecting annotation processors on the compile classpath is deprecated" warning

I am attempting to resolve the warning below from Gradle. It only occurs when I invoke testing code. The main Java (only) run and build tasks work fine.
The testing code is in Groovy. It seems that Groovy in combination with Gradle and Micronaut raise this message on projects where the message was resolved weeks back but stubbornly persists on the test and testCompile related tasks.
> Task :plumbing:compileTestGroovy
The following annotation processors were detected on the compile classpath:
'io.micronaut.annotation.processing.TypeElementVisitorProcessor' and 'io.micronaut.annotation.processing.PackageConfigurationInjectProcessor' and
'io.micronaut.annotation.processing.BeanDefinitionInjectProcessor'.
Detecting annotation processors on the compile classpath is
deprecated and Gradle 5.0 will ignore them. Please add them
to the annotation processor path instead. If you did not
intend to use annotation processors, you can use the
'-proc:none' compiler argument to ignore them.
By starting with Micronaut the instructions to work with Lombok I found through trial-and-error that so far the only way for my Groovy tests to run is to use the following recipe in the build.gradle file. The Micronaut recipe specifies to put Lombok before Micronaut. Which is effective for Java builds.
In order to have Groovy code compile and then execute I'm (seemingly) required to write my dependencies as so:
configurations {
annotationProcessor
}
dependencies
{
compileOnly (
dep_lombok // Must come first
)
annotationProcessor (
dep_lombokAtnProc, // Must come first
dep_micronautAtnProc
)
compileOnly (
dep_micronaut // Must follow annotationProcessor
)
implementation (
project( ':base'),
)
testImplementation (
project( ':TestingSupport')
)
testImplementation (
dep_micronaut,
dep_commonsConfig
)
}
The dependencies dep_XXX are just strings.
The "AtnProc..." label is to specifically identify the Annotation Processor (even when it is the same coordinate).
If the compileOnly ( dep micronaut ) clause it necessary for things like #Inject to be processed with the Groovy builds. And ...
It must be in the order shown, following the annotationProcessor(..) clause.
Despite the situation that there are no annotations in the Groovy files at this point anyway.
With the above build information, the Groovy specificaitons run and work correctly. However I still get the Deprecated ... warning.
Without the compileOnly( Micronaut ) phrase I get compile errors and nothing runs. testCompileOnly, a groovyCompileOnly or testAnnotationProcessor do nothing.
Who knows how to build and run Groovy tests with Gradle when using Micronaut? Tests coexist with Lombok fine.
The things missing are #Inject, #Singleton, etc.
Looking forward to suggestions and ideas.
The fact that the warning was resolved for your main module once adding the annotationProcessor configuration but continues for your test module, would lead me to believe that annotation processors are being used in your test code.
Similarly to the kapt Kotlin annotation processor plugin, I believe that you need to configure your annotation processors for your test module with testAnnocationProcessor as below:
testAnnotationProcessor (
dep_lombokAtnProc,
dep_micronautAtnProc
)

Resources