From ee80391a6e04a239f904bb801aeadcd8b927a17f Mon Sep 17 00:00:00 2001 From: Guillaume Le Floch Date: Fri, 23 Apr 2021 17:37:54 +0200 Subject: [PATCH] Declare input and output for quarkusGenerateCode gradle task --- .../java/io/quarkus/gradle/QuarkusPlugin.java | 27 ++++++-------- .../gradle/tasks/QuarkusGenerateCode.java | 36 ++++++++++++++++--- .../gradle/QuarkusPluginFunctionalTest.java | 1 + 3 files changed, 43 insertions(+), 21 deletions(-) diff --git a/devtools/gradle/src/main/java/io/quarkus/gradle/QuarkusPlugin.java b/devtools/gradle/src/main/java/io/quarkus/gradle/QuarkusPlugin.java index b8dee8f17413e..f61804ee4a884 100644 --- a/devtools/gradle/src/main/java/io/quarkus/gradle/QuarkusPlugin.java +++ b/devtools/gradle/src/main/java/io/quarkus/gradle/QuarkusPlugin.java @@ -153,11 +153,9 @@ public void execute(Task test) { compileJavaTask.getOptions().setAnnotationProcessorPath(annotationProcessorConfig); compileJavaTask.dependsOn(quarkusGenerateCode); - quarkusGenerateCode.setSourceRegistrar(compileJavaTask::source); JavaCompile compileTestJavaTask = (JavaCompile) tasks.getByName(JavaPlugin.COMPILE_TEST_JAVA_TASK_NAME); compileTestJavaTask.dependsOn(quarkusGenerateCodeTests); - quarkusGenerateCodeTests.setSourceRegistrar(compileTestJavaTask::source); Task classesTask = tasks.getByName(JavaPlugin.CLASSES_TASK_NAME); Task resourcesTask = tasks.getByName(JavaPlugin.PROCESS_RESOURCES_TASK_NAME); @@ -204,27 +202,24 @@ public void execute(Task test) { tasks.withType(Test.class).forEach(configureTestTask); tasks.withType(Test.class).whenTaskAdded(configureTestTask::accept); - sourceSets.create(QuarkusGenerateCode.QUARKUS_GENERATED_SOURCES).getOutput() + SourceSet generatedSourceSet = sourceSets.create(QuarkusGenerateCode.QUARKUS_GENERATED_SOURCES); + generatedSourceSet.getOutput() .dir(QuarkusGenerateCode.QUARKUS_GENERATED_SOURCES); - sourceSets.create(QuarkusGenerateCode.QUARKUS_TEST_GENERATED_SOURCES).getOutput() + SourceSet generatedTestSourceSet = sourceSets.create(QuarkusGenerateCode.QUARKUS_TEST_GENERATED_SOURCES); + generatedTestSourceSet.getOutput() .dir(QuarkusGenerateCode.QUARKUS_TEST_GENERATED_SOURCES); + + // Register the quarkus-generated-code + for (String provider : QuarkusGenerateCode.CODE_GENERATION_PROVIDER) { + mainSourceSet.getJava().srcDir(new File(generatedSourceSet.getJava().getOutputDir(), provider)); + testSourceSet.getJava().srcDir(new File(generatedTestSourceSet.getJava().getOutputDir(), provider)); + } + }); project.getPlugins().withId("org.jetbrains.kotlin.jvm", plugin -> { tasks.getByName("compileKotlin").dependsOn(quarkusGenerateCode); tasks.getByName("compileTestKotlin").dependsOn(quarkusGenerateCodeTests); - - // Register the quarkus-generated-code - SourceSetContainer sourceSets = project.getConvention().getPlugin(JavaPluginConvention.class) - .getSourceSets(); - SourceSet mainSourceSet = sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME); - SourceSet testSourceSet = sourceSets.getByName(SourceSet.TEST_SOURCE_SET_NAME); - SourceSet generatedSourceSet = sourceSets.getByName(QuarkusGenerateCode.QUARKUS_GENERATED_SOURCES); - SourceSet generatedTestSourceSet = sourceSets.getByName(QuarkusGenerateCode.QUARKUS_TEST_GENERATED_SOURCES); - for (String provider : QuarkusGenerateCode.CODE_GENERATION_PROVIDER) { - mainSourceSet.getJava().srcDir(new File(generatedSourceSet.getJava().getOutputDir(), provider)); - testSourceSet.getJava().srcDir(new File(generatedTestSourceSet.getJava().getOutputDir(), provider)); - } }); } diff --git a/devtools/gradle/src/main/java/io/quarkus/gradle/tasks/QuarkusGenerateCode.java b/devtools/gradle/src/main/java/io/quarkus/gradle/tasks/QuarkusGenerateCode.java index 0b1dbb5537e35..6ec53119ea683 100644 --- a/devtools/gradle/src/main/java/io/quarkus/gradle/tasks/QuarkusGenerateCode.java +++ b/devtools/gradle/src/main/java/io/quarkus/gradle/tasks/QuarkusGenerateCode.java @@ -1,10 +1,13 @@ package io.quarkus.gradle.tasks; +import java.io.File; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashSet; import java.util.List; import java.util.Optional; import java.util.Properties; @@ -16,6 +19,8 @@ import org.gradle.api.plugins.Convention; import org.gradle.api.plugins.JavaPluginConvention; import org.gradle.api.tasks.CompileClasspath; +import org.gradle.api.tasks.InputFiles; +import org.gradle.api.tasks.OutputDirectories; import org.gradle.api.tasks.SourceSet; import org.gradle.api.tasks.TaskAction; @@ -33,10 +38,12 @@ public class QuarkusGenerateCode extends QuarkusTask { public static final String QUARKUS_TEST_GENERATED_SOURCES = "quarkus-test-generated-sources"; // TODO dynamically load generation provider, or make them write code directly in quarkus-generated-sources public static final String[] CODE_GENERATION_PROVIDER = new String[] { "grpc", "avdl", "avpr", "avsc" }; + public static final String[] CODE_GENERATION_INPUT = new String[] { "proto", "avro" }; public static final String INIT_AND_RUN = "initAndRun"; private Set sourcesDirectories; - private Consumer sourceRegistrar; + private Consumer sourceRegistrar = (p) -> { + }; private boolean test = false; public QuarkusGenerateCode() { @@ -53,6 +60,29 @@ public FileCollection getClasspath() { return QuarkusGradleUtils.getSourceSet(getProject(), SourceSet.MAIN_SOURCE_SET_NAME).getCompileClasspath(); } + @InputFiles + public Set getInputDirectory() { + Set inputDirectories = new HashSet<>(); + + final String inputSourceSetName = test ? SourceSet.TEST_SOURCE_SET_NAME : SourceSet.MAIN_SOURCE_SET_NAME; + Path src = getProject().getProjectDir().toPath().resolve("src").resolve(inputSourceSetName); + + for (String input : CODE_GENERATION_INPUT) { + Path providerSrcDir = src.resolve(input); + if (Files.exists(providerSrcDir)) { + inputDirectories.add(providerSrcDir.toFile()); + } + } + + return inputDirectories; + } + + @OutputDirectories + public FileCollection getGeneratedOutputDirectory() { + final String generatedSourceSetName = test ? QUARKUS_TEST_GENERATED_SOURCES : QUARKUS_GENERATED_SOURCES; + return QuarkusGradleUtils.getSourceSet(getProject(), generatedSourceSetName).getOutput().getDirs(); + } + @TaskAction public void prepareQuarkus() { getLogger().lifecycle("preparing quarkus application"); @@ -118,10 +148,6 @@ public void setSourcesDirectories(Set sourcesDirectories) { this.sourcesDirectories = sourcesDirectories; } - public void setSourceRegistrar(Consumer sourceRegistrar) { - this.sourceRegistrar = sourceRegistrar; - } - public void setTest(boolean test) { this.test = test; } diff --git a/integration-tests/gradle/src/test/java/io/quarkus/gradle/QuarkusPluginFunctionalTest.java b/integration-tests/gradle/src/test/java/io/quarkus/gradle/QuarkusPluginFunctionalTest.java index d3829a0da725b..e00cb8403c932 100644 --- a/integration-tests/gradle/src/test/java/io/quarkus/gradle/QuarkusPluginFunctionalTest.java +++ b/integration-tests/gradle/src/test/java/io/quarkus/gradle/QuarkusPluginFunctionalTest.java @@ -64,6 +64,7 @@ public void canDetectUpToDateBuild() throws Exception { assertThat(firstBuild.getTasks().get(":quarkusBuild")).isEqualTo(BuildResult.SUCCESS_OUTCOME); BuildResult secondBuild = runGradleWrapper(projectRoot, "quarkusBuild", "--stacktrace"); + assertThat(secondBuild.getTasks().get(":quarkusGenerateCode")).isEqualTo(BuildResult.UPTODATE_OUTCOME); assertThat(secondBuild.getTasks().get(":quarkusBuild")).isEqualTo(BuildResult.UPTODATE_OUTCOME); }