Repository: quarkusio/quarkus
Issue: Gradle quarkusGenerateCode task with composite build dependency causes jar does not exist

### Describe the bug

Our gradle composite build intermittently fails with dependent jars not existing (see below sample output). It always fails on the quarkusGenerateCode task of a module that depends on a jar produced by the included build. We don't usually run this task directly, but running it directly reproduces the issue much more consistently.

```
> Task :service-mocks:aws-orchestration-mock:quarkusGenerateCode FAILED
preparing quarkus application

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':service-mocks:aws-orchestration-mock:quarkusGenerateCode'.
> [...]/quarkus-composite-build-issue/common/common-util/build/libs/common-util-1.0.jar does not exist
```

### Expected behavior

I should be able to run the gradle quarkusGenerateCode task and dependencies should correctly be resolved prior to execution.

### Actual behavior

quarkusGenerateCode task fails with an error indicating the composite build dependency jar does not exist

### How to Reproduce?

From the root of the attached project if you run the gradle command:

`./gradlew :common:common-util:clean :services:acme-service:clean :services:acme-service:quarkusGenerateCode`

It should reproduce the issue. There is a commented out line in `services/acme-service/build.gradle` that adds an explicit dependency on the included composite build's jar task that can be used as a workaround.

[quarkus-composite-build-issue.zip](https://github.com/quarkusio/quarkus/files/8227922/quarkus-composite-build-issue.zip)



### Output of `uname -a` or `ver`

Darwin Kernel Version 21.3.0

### Output of `java -version`

openjdk version "11.0.11"

### GraalVM version (if different from Java)

_No response_

### Quarkus version or git rev

2.7.4.Final

### Build tool (ie. output of `mvnw --version` or `gradlew --version`)

Gradle 7.4

### Additional information

_No response_

Changed files:
devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusGenerateCode.java
devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/QuarkusPlugin.java

diff --git a/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/QuarkusPlugin.java b/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/QuarkusPlugin.java
index dc53392336a..cef5d097ac7 100644
--- a/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/QuarkusPlugin.java
+++ b/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/QuarkusPlugin.java
@@ -198,9 +198,25 @@ public void execute(Task test) {
                     TaskProvider<Task> testClassesTask = tasks.named(JavaPlugin.TEST_CLASSES_TASK_NAME);
                     TaskProvider<Task> testResourcesTask = tasks.named(JavaPlugin.PROCESS_TEST_RESOURCES_TASK_NAME);
 
-                    quarkusGenerateCode.configure(task -> task.dependsOn(resourcesTask));
-                    quarkusGenerateCodeDev.configure(task -> task.dependsOn(resourcesTask));
-                    quarkusGenerateCodeTests.configure(task -> task.dependsOn(resourcesTask));
+                    quarkusGenerateCode.configure(task -> {
+                        task.dependsOn(resourcesTask);
+                        task.setCompileClasspath(
+                                project.getConfigurations().getByName(
+                                        ApplicationDeploymentClasspathBuilder.getBaseRuntimeConfigName(LaunchMode.NORMAL)));
+                    });
+                    quarkusGenerateCodeDev.configure(task -> {
+                        task.dependsOn(resourcesTask);
+                        task.setCompileClasspath(
+                                project.getConfigurations().getByName(
+                                        ApplicationDeploymentClasspathBuilder
+                                                .getBaseRuntimeConfigName(LaunchMode.DEVELOPMENT)));
+                    });
+                    quarkusGenerateCodeTests.configure(task -> {
+                        task.dependsOn(resourcesTask);
+                        task.setCompileClasspath(
+                                project.getConfigurations().getByName(
+                                        ApplicationDeploymentClasspathBuilder.getBaseRuntimeConfigName(LaunchMode.TEST)));
+                    });
 
                     quarkusDev.configure(task -> {
                         task.dependsOn(classesTask, resourcesTask, testClassesTask, testResourcesTask,
diff --git a/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusGenerateCode.java b/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusGenerateCode.java
index 623c20a54dd..0021c5ea005 100644
--- a/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusGenerateCode.java
+++ b/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusGenerateCode.java
@@ -15,7 +15,7 @@
 import java.util.function.Consumer;
 
 import org.gradle.api.GradleException;
-import org.gradle.api.file.FileCollection;
+import org.gradle.api.artifacts.Configuration;
 import org.gradle.api.plugins.JavaPluginConvention;
 import org.gradle.api.tasks.CompileClasspath;
 import org.gradle.api.tasks.InputFiles;
@@ -42,6 +42,7 @@ public class QuarkusGenerateCode extends QuarkusTask {
 
     public static final String INIT_AND_RUN = "initAndRun";
     private Set<Path> sourcesDirectories;
+    private Configuration compileClasspath;
     private Consumer<Path> sourceRegistrar = (p) -> {
     };
     private boolean test = false;
@@ -57,8 +58,12 @@ public QuarkusGenerateCode() {
      * @return resolved compile classpath
      */
     @CompileClasspath
-    public FileCollection getClasspath() {
-        return QuarkusGradleUtils.getSourceSet(getProject(), SourceSet.MAIN_SOURCE_SET_NAME).getCompileClasspath();
+    public Configuration getClasspath() {
+        return compileClasspath;
+    }
+
+    public void setCompileClasspath(Configuration compileClasspath) {
+        this.compileClasspath = compileClasspath;
     }
 
     @InputFiles