Gradle dependency visible only for annotation processor - gradle

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

Related

gradle build failed due to dependency lock state

i have newly added a below dependency into my project
implementation 'org.jdbi:jdbi3-oracle12:3.29.0'
these are the dependencies which i already have in my project.
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.data:spring-data-jpa'
implementation 'org.hibernate:hibernate-core'
// Oracle JDBC drivers
implementation("com.oracle.database.jdbc:ojdbc11")
implementation("com.oracle.database.jdbc:ucp")
// Additional Jars for using Oracle Wallets
implementation("com.oracle.database.security:oraclepki")
implementation("com.oracle.database.security:osdt_core")
implementation("com.oracle.database.security:osdt_cert")
implementation 'org.apache.commons:commons-lang3:3.12.0'
implementation 'javax.xml.bind:jaxb-api:2.3.1'
implementation 'io.micrometer:micrometer-registry-prometheus'
implementation 'org.jdbi:jdbi3-core:3.28.0'
implementation 'org.jdbi:jdbi3-oracle12:3.29.0'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.mockito:mockito-inline:4.5.1'
}
while adding the new dependency and doing gradle build. i'm getting the below error. can some one guide me how to fix this?
Execution failed for task ':compileJava'.
Could not resolve all files for configuration ':compileClasspath'.
Resolved 'org.jdbi:jdbi3-oracle12:3.29.0' which is not part of the dependency lock state
Resolved 'com.oracle.database.jdbc:ojdbc8:21.3.0.0' which is not part of the dependency lock state

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.

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