From 639462292fbccd17be596105b240b6bd8a65161c Mon Sep 17 00:00:00 2001 From: Guillaume Smet Date: Fri, 30 Aug 2024 12:46:34 +0200 Subject: [PATCH] Gradle - Correctly merge classes dir when using dev mode We were returning an empty dir before which would trigger the use of the resources dir instead. Also simplify the logic as it was quite hard to follow and it's actually quite simple. Fixes #42860 --- .../io/quarkus/gradle/tasks/QuarkusDev.java | 2 +- .../gradle/tasks/QuarkusGradleUtils.java | 63 ++++++++----------- 2 files changed, 28 insertions(+), 37 deletions(-) diff --git a/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusDev.java b/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusDev.java index 8ffbba22d9ef9..d748b2d1e078b 100644 --- a/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusDev.java +++ b/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusDev.java @@ -633,7 +633,7 @@ private void addLocalProject(ResolvedDependency project, GradleDevModeLauncher.B } } Path classesDir = classesDirs.isEmpty() ? null - : QuarkusGradleUtils.mergeClassesDirs(classesDirs, project.getWorkspaceModule().getBuildDir(), root, false); + : QuarkusGradleUtils.mergeClassesDirs(classesDirs, project.getWorkspaceModule().getBuildDir(), true, false); Path generatedSourcesPath = sources.getSourceDirs().isEmpty() ? null : sources.getSourceDirs().iterator().next().getAptSourcesDir(); diff --git a/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusGradleUtils.java b/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusGradleUtils.java index fc38292cf7498..d5e924f6dd27c 100644 --- a/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusGradleUtils.java +++ b/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusGradleUtils.java @@ -7,7 +7,7 @@ import java.nio.file.Path; import java.util.Collection; import java.util.HashSet; -import java.util.Iterator; +import java.util.List; import java.util.Set; import org.gradle.api.Project; @@ -46,44 +46,35 @@ public static String getClassesDir(SourceSet sourceSet, File tmpDir, boolean pop } public static Path mergeClassesDirs(Collection classesDirs, File tmpDir, boolean populated, boolean test) { - Path classesDir = null; - final Iterator i = classesDirs.iterator(); - int dirCount = 0; - while (i.hasNext()) { - final Path next = i.next(); - if (!Files.exists(next)) { - continue; + List existingClassesDirs = classesDirs.stream().filter(p -> Files.exists(p)).toList(); + + if (existingClassesDirs.size() == 0) { + return null; + } + + if (existingClassesDirs.size() == 1) { + return existingClassesDirs.get(0); + } + + try { + Path mergedClassesDir = tmpDir.toPath().resolve("quarkus-app-classes" + (test ? "-test" : "")); + + if (!populated) { + return mergedClassesDir; } - try { - switch (dirCount++) { - case 0: - classesDir = next; - break; - case 1: - //there does not seem to be any sane way of dealing with multiple output dirs, as there does not seem - //to be a way to map them. We will need to address this at some point, but for now we just stick them - //all in a temp dir - final Path tmpClassesDir = tmpDir.toPath().resolve("quarkus-app-classes" + (test ? "-test" : "")); - if (!populated) { - return tmpClassesDir; - } - if (Files.exists(tmpClassesDir)) { - IoUtils.recursiveDelete(tmpClassesDir); - } - IoUtils.copy(classesDir, tmpClassesDir); - classesDir = tmpClassesDir; - default: - IoUtils.copy(next, classesDir); - - } - } catch (IOException e) { - throw new UncheckedIOException(ERROR_COLLECTING_PROJECT_CLASSES, e); + + if (Files.exists(mergedClassesDir)) { + IoUtils.recursiveDelete(mergedClassesDir); } + + for (Path classesDir : existingClassesDirs) { + IoUtils.copy(classesDir, mergedClassesDir); + } + + return mergedClassesDir; + } catch (IOException e) { + throw new UncheckedIOException(ERROR_COLLECTING_PROJECT_CLASSES, e); } - if (classesDir == null) { - return null; - } - return classesDir; } }