chia7712 commented on code in PR #21337: URL: https://github.com/apache/kafka/pull/21337#discussion_r3695396780
########## api-checker/gradle-plugins/src/main/java/org/apache/kafka/gradle/KafkaPublicApiCheckerPlugin.java: ########## @@ -0,0 +1,117 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.kafka.gradle; + +import org.gradle.api.DefaultTask; +import org.gradle.api.Plugin; +import org.gradle.api.Project; +import org.gradle.api.Task; +import org.gradle.api.tasks.TaskProvider; +import org.gradle.api.tasks.bundling.Jar; + +/** + * Gradle plugin for checking public API consistency in the Kafka codebase. + * This is an internal plugin that runs as part of Kafka's own build process. + */ +public class KafkaPublicApiCheckerPlugin implements Plugin<Project> { + + /** Project property name that disables this checker for the current Gradle invocation. */ + static final String SKIP_PROPERTY = "kafkaPublicApiChecker.skip"; + + @Override + public void apply(Project project) { + // Create the extension for configuration + KafkaPublicApiCheckerExtension extension = project.getExtensions() + .create("kafkaPublicApiChecker", KafkaPublicApiCheckerExtension.class, project); + + if (isPropertyTruthy(project, SKIP_PROPERTY)) { + extension.getEnabled().set(false); + } + + // Register the task + TaskProvider<KafkaPublicApiCheckerTask> taskProvider = project.getTasks() + .register("kafkaPublicApiChecker", KafkaPublicApiCheckerTask.class, task -> { + task.getCheckerEnabled().set(extension.getEnabled()); + task.getFailOnViolation().set(extension.getFailOnViolation()); + // Intentionally NOT calling task.getJavadocJarPath().set(extension.getJavadocJarPath()) + // here — `set(Provider)` marks the property as explicitly assigned even if the upstream + // is absent, which silently disables any later `.convention(...)` we install in + // afterEvaluate. The convention block below is the single source of truth. + task.getProjectJarFiles().from(extension.getProjectJarFiles()); + task.getReferenceJarFiles().from(extension.getReferenceJarFiles()); + task.getReportFile().set(extension.getReportFile()); + }); + + // Configure task to run after javadoc + project.afterEvaluate(p -> { + TaskProvider<DefaultTask> docsJarTask = p.getTasks().named("docsJar", DefaultTask.class); + + // Wire the checker's `javadocJarPath` input to the `javadocJar` task's `archiveFile` + // output where it exists. Reading the task's lazy output Provider — rather than + // recomputing the path from project.name + version — guarantees we read the *exact* + // file this Gradle run produced, which closes a stale-jar foot-gun: + // + // build/libs/kafka-foo-4.3.0-SNAPSHOT-javadoc.jar (left over from a months-old build) + // build/libs/kafka-foo-4.4.0-SNAPSHOT-javadoc.jar (just produced this run) + // + // The pre-fix auto-detect listed the directory and picked files[0], which on APFS + // returned the 4.3.0 (alphabetically earlier) jar. The checker then compared + // months-old javadoc against current bytecode and reported "all clean" while the + // module had real MISSING_PUBLICAPI_ANNOTATION violations against the fresh code. + Task javadocJarRaw = p.getTasks().findByName("javadocJar"); Review Comment: Forcing the javadoc jar to be generated breaks some e2e tests. I have opened https://issues.apache.org/jira/browse/KAFKA-20881 to track this -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
