Configure gradle with annotationProcessor and log4j2 PluginProcessor - gradle

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'
}

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

Why did Kotlin JSR223 Scripting stop working when I upgraded to Kotlin 1.3.30

In a project that uses the javax.script scripting support added in 1.1 in its unit tests, upgrading the Kotlin language version from 1.3.21 to 1.3.30 caused those tests to fail with the following exception:
java.lang.NoClassDefFoundError: org/jetbrains/kotlin/scripting/compiler/plugin/ScriptingCompilerConfigurationComponentRegistrar
at org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngine.makeCompilerConfiguration(KotlinJsr223JvmLocalScriptEngine.kt:72)
at org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngine.access$makeCompilerConfiguration(KotlinJsr223JvmLocalScriptEngine.kt:38)
at org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngine$replCompiler$2.invoke(KotlinJsr223JvmLocalScriptEngine.kt:49)
at org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngine$replCompiler$2.invoke(KotlinJsr223JvmLocalScriptEngine.kt:38)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
at org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngine.getReplCompiler(KotlinJsr223JvmLocalScriptEngine.kt)
at org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngine$localEvaluator$2.invoke(KotlinJsr223JvmLocalScriptEngine.kt:53)
at org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngine$localEvaluator$2.invoke(KotlinJsr223JvmLocalScriptEngine.kt:38)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
at org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngine.getLocalEvaluator(KotlinJsr223JvmLocalScriptEngine.kt)
at org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngine.getReplEvaluator(KotlinJsr223JvmLocalScriptEngine.kt:55)
at org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngine.createState(KotlinJsr223JvmLocalScriptEngine.kt:59)
at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.createState$default(KotlinJsr223JvmScriptEngineBase.kt:46)
at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.getCurrentState(KotlinJsr223JvmScriptEngineBase.kt:53)
at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.nextCodeLine(KotlinJsr223JvmScriptEngineBase.kt:44)
at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.compileAndEval(KotlinJsr223JvmScriptEngineBase.kt:59)
at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.eval(KotlinJsr223JvmScriptEngineBase.kt:31)
The relevant lines in build.gradle are:
dependencies {
// ... other stuff ...
testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
testCompile "org.jetbrains.kotlin:kotlin-compiler-embeddable:$kotlin_version"
testCompile "org.jetbrains.kotlin:kotlin-script-util:$kotlin_version"
}
where ext.kotlin_version is either "1.3.21" or "1.3.30".
Why did this break, and how can I fix it?
It broke because JetBrains have refactored the scripting functionality into a plugin, and the dependencies required to successfully run Kotlin script through JSR223 have changed.
The relevant issue on the Kotlin bug tracker is KT-30972, which was closed as a duplicate of KT-30986.
The upshot is, you need to adjust the dependencies to include kotlin-scripting-compiler-embeddable.
dependencies {
// ... other stuff ...
testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
testCompile "org.jetbrains.kotlin:kotlin-compiler-embeddable:$kotlin_version"
testCompile "org.jetbrains.kotlin:kotlin-scripting-compiler-embeddable:$kotlin_version"
testCompile "org.jetbrains.kotlin:kotlin-script-util:$kotlin_version"
}
the working version nowadays is simply:
dependencies {
runtimeOnly("org.jetbrains.kotlin:kotlin-scripting-jsr223:${Deps.JetBrains.Kotlin.VERSION}")
}
which pulls in all necessary dependencies transitively.
Also the META-INF/services/javax.script.ScriptEngineFactory File seems not to be necessary if doing so.

liquibase.integration.spring.SpringLiquibase is available from 2 locations error in spring boot

I'm trying to integrate spring boot with liquibase, but when I run the application it throws the following error:
An attempt was made to call the method liquibase.integration.spring.SpringLiquibase.setLiquibaseSchema(Ljava/lang/String;)V but it does not exist. Its class, liquibase.integration.spring.SpringLiquibase, is available from the following locations:
jar:file:/C:/Users/Dev/.gradle/caches/modules-2/files-2.1/org.liquibase/liquibase-core/3.5.5/c65051f327382018bd09c30380f25eac96f210da/liquibase-core-3.5.5.jar!/liquibase/integration/spring/SpringLiquibase.class
It was loaded from the following location:
file:/C:/Users/Dev/.gradle/caches/modules-2/files-2.1/org.liquibase/liquibase-core/3.5.5/c65051f327382018bd09c30380f25eac96f210da/liquibase-core-3.5.5.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of liquibase.integration.spring.SpringLiquibase
It looks like that the problem is given by spring-boot-starter-data-jpa, as soon as I remove the dependency, the application runs fine. This is my full dependency list, but I would also need JPA to write my persistence classes.
dependencies {
compile('org.springframework.boot:spring-boot-starter-actuator')
compile 'org.springframework.boot:spring-boot-starter-web'
runtime('org.postgresql:postgresql:42.2.5')
compile 'org.webjars:bootstrap:4.1.3'
compile 'org.webjars:webjars-locator-core'
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.hibernate:hibernate-entitymanager:4.3.4.Final")
compile("org.hibernate:hibernate-validator:6.0.16.Final")
compile ("org.hibernate:hibernate-core:5.4.1.Final")
testCompile 'org.springframework.boot:spring-boot-starter-test'
compile("org.liquibase:liquibase-core:3.5.5")
liquibaseRuntime 'org.liquibase:liquibase-gradle-plugin:2.0.1'
liquibaseRuntime 'org.liquibase:liquibase-groovy-dsl:2.0.2'
liquibaseRuntime "org.liquibase:liquibase-core:3.5.5"
liquibaseRuntime 'org.postgresql:postgresql:42.2.5'
}
Any idea why there's a mismatch?
Thanks in advance
Remove the version from your configuration:
compile("org.liquibase:liquibase-core")
Let Spring Dependency Management take care of that for you.
I solved it in my maven project just by removing the Liquibase version in the POM
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
Leave it this way without the version and Spring takes care of the rest.
In my case upgrading to using both Spring Boot 2.1.0.RELEASE and org.liquibase:liquibase-core:3.6.3 solved the issue entirely.
Sounds like two different dependencies include a duplicate jar.
There seems to be a plugin that manages dependencies in Gradle, take a look here.

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