Deprecate AP discovery in compile configurations in Gradle
#KT-24368 Fixed
This commit is contained in:
+2
@@ -452,6 +452,7 @@ open class Kapt3IT : Kapt3BaseIT() {
|
||||
}
|
||||
build("assemble") {
|
||||
assertSuccessful()
|
||||
assertContains("Annotation processors discovery from compile classpath is deprecated")
|
||||
}
|
||||
|
||||
buildGradle.modify {
|
||||
@@ -459,6 +460,7 @@ open class Kapt3IT : Kapt3BaseIT() {
|
||||
}
|
||||
build("assemble") {
|
||||
assertFailed()
|
||||
assertNotContains("Annotation processors discovery from compile classpath is deprecated")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+50
@@ -10,6 +10,7 @@ import org.jetbrains.kotlin.gradle.tasks.cacheOnlyIfEnabledForKotlin
|
||||
import org.jetbrains.kotlin.gradle.tasks.isBuildCacheSupported
|
||||
import org.jetbrains.kotlin.gradle.utils.isJavaFile
|
||||
import java.io.File
|
||||
import java.util.jar.JarFile
|
||||
|
||||
@CacheableTask
|
||||
abstract class KaptTask : ConventionTask() {
|
||||
@@ -109,4 +110,53 @@ abstract class KaptTask : ConventionTask() {
|
||||
|
||||
private fun FileCollection?.orEmpty(): FileCollection =
|
||||
this ?: project.files()
|
||||
|
||||
protected fun checkAnnotationProcessorClasspath() {
|
||||
if (!includeCompileClasspath) return
|
||||
|
||||
val kaptClasspath = kaptClasspath.toSet()
|
||||
val processorsFromCompileClasspath = classpath.files.filterTo(LinkedHashSet()) {
|
||||
hasAnnotationProcessors(it)
|
||||
}
|
||||
val processorsAbsentInKaptClasspath = processorsFromCompileClasspath.filter { it !in kaptClasspath }
|
||||
if (processorsAbsentInKaptClasspath.isNotEmpty()) {
|
||||
if (logger.isInfoEnabled) {
|
||||
logger.warn(
|
||||
"Annotation processors discovery from compile classpath is deprecated."
|
||||
+ "\nSet 'kapt.includeCompileClasspath = false' to disable discovery."
|
||||
+ "\nThe following files, containing annotation processors, are not present in KAPT classpath:\n"
|
||||
+ processorsAbsentInKaptClasspath.joinToString("\n") { " '$it'" }
|
||||
+ "\nAdd corresponding dependencies to any of the following configurations:\n"
|
||||
+ kaptClasspathConfigurations.joinToString("\n") { " '${it.name}'" }
|
||||
)
|
||||
} else {
|
||||
logger.warn(
|
||||
"Annotation processors discovery from compile classpath is deprecated."
|
||||
+ "\nSet 'kapt.includeCompileClasspath = false' to disable discovery."
|
||||
+ "\nRun the build with '--info' for more details."
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private fun hasAnnotationProcessors(file: File): Boolean {
|
||||
val processorEntryPath = "META-INF/services/javax.annotation.processing.Processor"
|
||||
|
||||
try {
|
||||
when {
|
||||
file.isDirectory -> {
|
||||
return file.resolve(processorEntryPath).exists()
|
||||
}
|
||||
file.isFile && file.extension.equals("jar", ignoreCase = true) -> {
|
||||
return JarFile(file).use { jar ->
|
||||
jar.getJarEntry(processorEntryPath) != null
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
logger.debug("Could not check annotation processors existence in $file: $e")
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -54,6 +54,7 @@ open class KaptWithKotlincTask : KaptTask(), CompilerArgumentAwareWithInput<K2JV
|
||||
@TaskAction
|
||||
fun compile() {
|
||||
logger.debug("Running kapt annotation processing using the Kotlin compiler")
|
||||
checkAnnotationProcessorClasspath()
|
||||
|
||||
val args = prepareCompilerArguments()
|
||||
|
||||
@@ -85,4 +86,4 @@ open class KaptWithKotlincTask : KaptTask(), CompilerArgumentAwareWithInput<K2JV
|
||||
val rtVersion = System.getProperty("java.runtime.version")
|
||||
return if (Character.isDigit(rtVersion[0])) rtVersion else System.getProperty("java.version")
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -47,7 +47,7 @@ open class KaptWithoutKotlincTask @Inject constructor(private val workerExecutor
|
||||
@TaskAction
|
||||
fun compile() {
|
||||
logger.info("Running kapt annotation processing using the Gradle Worker API")
|
||||
|
||||
checkAnnotationProcessorClasspath()
|
||||
clearLocalStateDirectories()
|
||||
|
||||
val compileClasspath = classpath.files.toMutableList()
|
||||
|
||||
Reference in New Issue
Block a user