Repository: quarkusio/quarkus
Issue: 2.4.0.CR1 Generated Bean Injection not working as before

### Describe the bug

I am working on a multi-module gradle project which uses MapStruct for @Entity to DTO conversion. Mapper beans are injected into microservice modules from shared data library modules. After upgrading from quarkus 2.3.0.Final to 2.4.0.CR1 injecting mapper beans does not work anymore. Using Jandex does not solve the problem either. 

### Expected behavior

Injecting MapStruct mapper beans from one module into another should work for 2.4.0.RC1.  

### Actual behavior

Injecting MapStruct mapper beans from one module into another does not work for 2.4.0.RC1. The application crashes before starting the server in devMode: 

```
2021-10-14 01:28:17,706 ERROR [io.qua.dep.dev.IsolatedDevModeMain] (main) Failed to start quarkus: java.lang.RuntimeException: io.quarkus.builder.BuildException: Build failure: Build failed due to errors
	[error]: Build step io.quarkus.arc.deployment.ArcProcessor#validate threw an exception: javax.enterprise.inject.spi.DeploymentException: javax.enterprise.inject.UnsatisfiedResolutionException: Unsatisfied dependency for type de.lj.v240.mapper.PersonMapper and qualifiers [@Default]
	- java member: de.lj.MapperTest#<init>()
	- declared on CLASS bean [types=[de.lj.MapperTest, java.lang.Object], qualifiers=[@Default, @Any], target=de.lj.MapperTest]
	at io.quarkus.arc.processor.BeanDeployment.processErrors(BeanDeployment.java:1190)
	at io.quarkus.arc.processor.BeanDeployment.init(BeanDeployment.java:268)
	at io.quarkus.arc.processor.BeanProcessor.initialize(BeanProcessor.java:129)
	at io.quarkus.arc.deployment.ArcProcessor.validate(ArcProcessor.java:418)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at io.quarkus.deployment.ExtensionLoader$2.execute(ExtensionLoader.java:821)
	at io.quarkus.builder.BuildContext.run(BuildContext.java:277)
	at org.jboss.threads.ContextHandler$1.runWith(ContextHandler.java:18)
	at org.jboss.threads.EnhancedQueueExecutor$Task.run(EnhancedQueueExecutor.java:2449)
	at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1478)
	at java.base/java.lang.Thread.run(Thread.java:834)
	at org.jboss.threads.JBossThread.run(JBossThread.java:501)
Caused by: javax.enterprise.inject.UnsatisfiedResolutionException: Unsatisfied dependency for type de.lj.v240.mapper.PersonMapper and qualifiers [@Default]
	- java member: de.lj.MapperTest#<init>()
	- declared on CLASS bean [types=[de.lj.MapperTest, java.lang.Object], qualifiers=[@Default, @Any], target=de.lj.MapperTest]
	at io.quarkus.arc.processor.Beans.resolveInjectionPoint(Beans.java:567)
	at io.quarkus.arc.processor.BeanInfo.init(BeanInfo.java:470)
	at io.quarkus.arc.processor.BeanDeployment.init(BeanDeployment.java:256)
	... 13 more

	at io.quarkus.runner.bootstrap.AugmentActionImpl.runAugment(AugmentActionImpl.java:418)
	at io.quarkus.runner.bootstrap.AugmentActionImpl.createInitialRuntimeApplication(AugmentActionImpl.java:276)
Caused by: javax.enterprise.inject.UnsatisfiedResolutionException: Unsatisfied dependency for type de.lj.v240.mapper.PersonMapper and qualifiers [@Default]

	at io.quarkus.runner.bootstrap.AugmentActionImpl.createInitialRuntimeApplication(AugmentActionImpl.java:67)
	at io.quarkus.deployment.dev.IsolatedDevModeMain.firstStart(IsolatedDevModeMain.java:92)
	at io.quarkus.deployment.dev.IsolatedDevModeMain.accept(IsolatedDevModeMain.java:441)
	at io.quarkus.deployment.dev.IsolatedDevModeMain.accept(IsolatedDevModeMain.java:65)
	at io.quarkus.bootstrap.app.CuratedApplication.runInCl(CuratedApplication.java:149)
	at io.quarkus.bootstrap.app.CuratedApplication.runInAugmentClassLoader(CuratedApplication.java:105)
	at io.quarkus.deployment.dev.DevModeMain.start(DevModeMain.java:145)
	at io.quarkus.deployment.dev.DevModeMain.main(DevModeMain.java:63)
Caused by: io.quarkus.builder.BuildException: Build failure: Build failed due to errors
	[error]: Build step io.quarkus.arc.deployment.ArcProcessor#validate threw an exception: javax.enterprise.inject.spi.DeploymentException: javax.enterprise.inject.UnsatisfiedResolutionException: Unsatisfied dependency for type de.lj.v240.mapper.PersonMapper and qualifiers [@Default]
	- java member: de.lj.MapperTest#<init>()
	- declared on CLASS bean [types=[de.lj.MapperTest, java.lang.Object], qualifiers=[@Default, @Any], target=de.lj.MapperTest]
	at io.quarkus.arc.processor.BeanDeployment.processErrors(BeanDeployment.java:1190)
	at io.quarkus.arc.processor.BeanDeployment.init(BeanDeployment.java:268)
	at io.quarkus.arc.processor.BeanProcessor.initialize(BeanProcessor.java:129)
	at io.quarkus.arc.deployment.ArcProcessor.validate(ArcProcessor.java:418)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at io.quarkus.deployment.ExtensionLoader$2.execute(ExtensionLoader.java:821)
	at io.quarkus.builder.BuildContext.run(BuildContext.java:277)
	at org.jboss.threads.ContextHandler$1.runWith(ContextHandler.java:18)
	at org.jboss.threads.EnhancedQueueExecutor$Task.run(EnhancedQueueExecutor.java:2449)
	at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1478)
	at java.base/java.lang.Thread.run(Thread.java:834)
	at org.jboss.threads.JBossThread.run(JBossThread.java:501)
Caused by: javax.enterprise.inject.UnsatisfiedResolutionException: Unsatisfied dependency for type de.lj.v240.mapper.PersonMapper and qualifiers [@Default]
	- java member: de.lj.MapperTest#<init>()
	- declared on CLASS bean [types=[de.lj.MapperTest, java.lang.Object], qualifiers=[@Default, @Any], target=de.lj.MapperTest]
	at io.quarkus.arc.processor.Beans.resolveInjectionPoint(Beans.java:567)
	at io.quarkus.arc.processor.BeanInfo.init(BeanInfo.java:470)
	at io.quarkus.arc.processor.BeanDeployment.init(BeanDeployment.java:256)
	... 13 more

	at io.quarkus.builder.Execution.run(Execution.java:116)
	at io.quarkus.builder.BuildExecutionBuilder.execute(BuildExecutionBuilder.java:79)
	at io.quarkus.deployment.QuarkusAugmentor.run(QuarkusAugmentor.java:161)
	at io.quarkus.runner.bootstrap.AugmentActionImpl.runAugment(AugmentActionImpl.java:416)
	... 9 more
Caused by: javax.enterprise.inject.spi.DeploymentException: javax.enterprise.inject.UnsatisfiedResolutionException: Unsatisfied dependency for type de.lj.v240.mapper.PersonMapper and qualifiers [@Default]
	- java member: de.lj.MapperTest#<init>()
	- declared on CLASS bean [types=[de.lj.MapperTest, java.lang.Object], qualifiers=[@Default, @Any], target=de.lj.MapperTest]
	at io.quarkus.arc.processor.BeanDeployment.processErrors(BeanDeployment.java:1190)
	at io.quarkus.arc.processor.BeanDeployment.init(BeanDeployment.java:268)
	at io.quarkus.arc.processor.BeanProcessor.initialize(BeanProcessor.java:129)
	at io.quarkus.arc.deployment.ArcProcessor.validate(ArcProcessor.java:418)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at io.quarkus.deployment.ExtensionLoader$2.execute(ExtensionLoader.java:821)
	at io.quarkus.builder.BuildContext.run(BuildContext.java:277)
	at org.jboss.threads.ContextHandler$1.runWith(ContextHandler.java:18)
	at org.jboss.threads.EnhancedQueueExecutor$Task.run(EnhancedQueueExecutor.java:2449)
	at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1478)
	at java.base/java.lang.Thread.run(Thread.java:834)
	at org.jboss.threads.JBossThread.run(JBossThread.java:501)
Caused by: javax.enterprise.inject.UnsatisfiedResolutionException: Unsatisfied dependency for type de.lj.v240.mapper.PersonMapper and qualifiers [@Default]
	- java member: de.lj.MapperTest#<init>()
	- declared on CLASS bean [types=[de.lj.MapperTest, java.lang.Object], qualifiers=[@Default, @Any], target=de.lj.MapperTest]
	at io.quarkus.arc.processor.Beans.resolveInjectionPoint(Beans.java:567)
	at io.quarkus.arc.processor.BeanInfo.init(BeanInfo.java:470)
	at io.quarkus.arc.processor.BeanDeployment.init(BeanDeployment.java:256)
Caused by: io.quarkus.builder.BuildException: Build failure: Build failed due to errors

	... 13 more

2021-10-14 01:28:17,706 INFO  [io.qua.dep.dev.IsolatedDevModeMain] (main) Attempting to start live reload endpoint to recover from previous Quarkus startup failure
Caused by: javax.enterprise.inject.UnsatisfiedResolutionException: Unsatisfied dependency for type de.lj.v240.mapper.PersonMapper and qualifiers [@Default]

Caused by: javax.enterprise.inject.spi.DeploymentException: javax.enterprise.inject.UnsatisfiedResolutionException: Unsatisfied dependency for type de.lj.v240.mapper.PersonMapper and qualifiers [@Default]

Caused by: javax.enterprise.inject.UnsatisfiedResolutionException: Unsatisfied dependency for type de.lj.v240.mapper.PersonMapper and qualifiers [@Default]

2021-10-14 01:28:17,838 INFO  [org.jbo.threads] (main) JBoss Threads version 3.4.2.Final

> Task :microservice-2.4.0:quarkusDev FAILED

Execution failed for task ':microservice-2.4.0:quarkusDev'.
> Build cancelled while executing task ':microservice-2.4.0:quarkusDev'

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

```

### How to Reproduce?

I added code which allows for reproduction of the error to [my github repo](https://github.com/lorenzjosten/mapper-injection). Run quarkusDev for gradle module microservice-2.4.0 in order to reproduce the error. Run quarkusDev for gradle module microservice-2.3.0 in order to get the expected behaviour.

### Output of `uname -a` or `ver`

Darwin 20.6.0 Darwin Kernel Version 20.6.0: Mon Aug 30 06:12:21 PDT 2021; root:xnu-7195.141.6~3/RELEASE_X86_64 x86_64

### Output of `java -version`

openjdk version "11.0.10" 2021-01-19 OpenJDK Runtime Environment GraalVM CE 21.0.0.2 (build 11.0.10+8-jvmci-21.0-b06) OpenJDK 64-Bit Server VM GraalVM CE 21.0.0.2 (build 11.0.10+8-jvmci-21.0-b06, mixed mode, sharing)

### GraalVM version (if different from Java)

_No response_

### Quarkus version or git rev

2.4.0.CR1

### Build tool (ie. output of `mvnw --version` or `gradlew --version`)

Gradle 7.2

### Additional information

Canonical bean injection, i.e. creating an @ApplicationScoped bean in the data-libraries, still works. 

Changed files:
devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/builder/GradleApplicationModelBuilder.java

diff --git a/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/builder/GradleApplicationModelBuilder.java b/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/builder/GradleApplicationModelBuilder.java
index 1d2b9ac2253..ca779244d87 100644
--- a/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/builder/GradleApplicationModelBuilder.java
+++ b/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/builder/GradleApplicationModelBuilder.java
@@ -24,6 +24,8 @@
 import org.gradle.api.artifacts.ResolvedArtifact;
 import org.gradle.api.artifacts.ResolvedConfiguration;
 import org.gradle.api.artifacts.component.ProjectComponentIdentifier;
+import org.gradle.api.file.FileCollection;
+import org.gradle.api.file.FileTree;
 import org.gradle.api.initialization.IncludedBuild;
 import org.gradle.api.internal.artifacts.dependencies.DefaultDependencyArtifact;
 import org.gradle.api.internal.artifacts.dependencies.DefaultExternalModuleDependency;
@@ -321,7 +323,7 @@ private void collectDependencies(ResolvedConfiguration configuration,
         if (artifactFiles != null) {
             // detect FS paths that aren't provided by the resolved artifacts
             for (File f : configuration.getFiles()) {
-                if (artifactFiles.contains(f)) {
+                if (artifactFiles.contains(f) || !f.exists()) {
                     continue;
                 }
                 // here we are trying to represent a direct FS path dependency
@@ -469,7 +471,7 @@ private void processQuarkusDependency(ResolvedDependencyBuilder artifactBuilder,
             if (Files.isDirectory(artifactPath)) {
                 processQuarkusDir(artifactBuilder, artifactPath.resolve(BootstrapConstants.META_INF), modelBuilder);
             } else {
-                try (FileSystem artifactFs = FileSystems.newFileSystem(artifactPath, null)) {
+                try (FileSystem artifactFs = FileSystems.newFileSystem(artifactPath, (ClassLoader) null)) {
                     processQuarkusDir(artifactBuilder, artifactFs.getPath(BootstrapConstants.META_INF), modelBuilder);
                 } catch (IOException e) {
                     throw new RuntimeException("Failed to process " + artifactPath, e);
@@ -481,103 +483,79 @@ private void processQuarkusDependency(ResolvedDependencyBuilder artifactBuilder,
     private static void initProjectModule(Project project, DefaultWorkspaceModule module, SourceSet sourceSet, boolean test) {
 
         module.setBuildFiles(PathList.of(project.getBuildFile().toPath()));
-        final List<Path> allJavaDirs = new ArrayList<>(2);
-        sourceSet.getAllJava().getSourceDirectories().forEach(f -> {
-            if (f.exists()) {
-                allJavaDirs.add(f.toPath());
-            }
-        });
-
-        if (!allJavaDirs.isEmpty()) {
-            final TaskCollection<AbstractCompile> compileTasks = project.getTasks().withType(AbstractCompile.class);
-            compileTasks.forEach(t -> {
-                String compiler = t.getName();
-                if (compiler.startsWith("compile")) {
-                    compiler = compiler.substring("compile".length()).toLowerCase();
-                }
 
-                final File destDir = t.getDestinationDirectory().getAsFile().get();
-
-                final List<Path> srcDirs = new ArrayList<>(1);
-                for (File f : t.getSource().getFiles()) {
-                    if (!f.exists()) {
-                        return;
-                    }
-                    final Path p = f.toPath();
-                    int i = 0;
-                    while (i < srcDirs.size()) {
-                        if (p.startsWith(srcDirs.get(i))) {
-                            break;
-                        }
-                        ++i;
-                    }
-                    if (i < srcDirs.size()) {
-                        continue;
-                    }
-                    for (Path srcDir : allJavaDirs) {
-                        if (p.startsWith(srcDir)) {
-                            srcDirs.add(srcDir);
-                            DefaultProcessedSources sources = new DefaultProcessedSources(srcDir.toFile(), destDir,
-                                    Collections.singletonMap("compiler", compiler));
-                            if (test) {
-                                module.addTestSources(sources);
-                            } else {
-                                module.addMainSources(sources);
-                            }
-                            break;
+        final FileCollection allClassesDirs = sourceSet.getOutput().getClassesDirs();
+        // some plugins do not add source directories to source sets and they may be missing from sourceSet.getAllJava()
+        // see https://github.com/quarkusio/quarkus/issues/20755
+        final TaskCollection<AbstractCompile> compileTasks = project.getTasks().withType(AbstractCompile.class);
+        compileTasks.forEach(t -> {
+            if (!t.getEnabled()) {
+                return;
+            }
+            final FileTree source = t.getSource();
+            if (source.isEmpty()) {
+                return;
+            }
+            final File destDir = t.getDestinationDirectory().getAsFile().get();
+            if (!allClassesDirs.contains(destDir)) {
+                return;
+            }
+            final List<File> srcDirs = new ArrayList<>(1);
+            source.visit(a -> {
+                // we are looking for the root dirs containing sources
+                if (a.getRelativePath().getSegments().length == 1) {
+                    final File srcDir = a.getFile().getParentFile();
+                    if (srcDirs.add(srcDir)) {
+                        DefaultProcessedSources sources = new DefaultProcessedSources(srcDir, destDir,
+                                Collections.singletonMap("compiler", t.getName()));
+                        if (test) {
+                            module.addTestSources(sources);
+                        } else {
+                            module.addMainSources(sources);
                         }
                     }
                 }
             });
-        }
-
-        final List<Path> allResourcesDirs = new ArrayList<>(1);
-        final List<Path> addedSrcDirs = new ArrayList<>(1);
-        final File outputDir = sourceSet.getOutput().getResourcesDir();
-        for (File resourcesDir : sourceSet.getResources().getSourceDirectories()) {
-            allResourcesDirs.add(resourcesDir.toPath());
-            addResources(module, resourcesDir, outputDir, test, addedSrcDirs);
-        }
+        });
 
-        if (!allResourcesDirs.isEmpty()) {
-            final TaskCollection<ProcessResources> resources = project.getTasks().withType(ProcessResources.class);
-            resources.forEach(t -> {
-                final File destDir = t.getDestinationDir();
-                for (File f : t.getSource().getFiles()) {
-                    if (!f.exists()) {
-                        return;
-                    }
-                    final Path p = f.toPath();
-                    int i = 0;
-                    while (i < addedSrcDirs.size()) {
-                        if (p.startsWith(addedSrcDirs.get(i))) {
-                            break;
-                        }
-                        ++i;
-                    }
-                    if (i < addedSrcDirs.size()) {
-                        continue;
-                    }
-                    for (Path srcDir : allResourcesDirs) {
-                        if (p.startsWith(srcDir)) {
-                            addResources(module, srcDir.toFile(), destDir, test, addedSrcDirs);
-                            break;
-                        }
+        final File resourcesOutputDir = sourceSet.getOutput().getResourcesDir();
+        final TaskCollection<ProcessResources> resources = project.getTasks().withType(ProcessResources.class);
+        resources.forEach(t -> {
+            if (!t.getEnabled()) {
+                return;
+            }
+            final FileCollection source = t.getSource();
+            if (source.isEmpty()) {
+                return;
+            }
+            final File destDir = t.getDestinationDir();
+            if (!destDir.equals(resourcesOutputDir)) {
+                return;
+            }
+            final List<File> srcDirs = new ArrayList<>(1);
+            source.getAsFileTree().visit(a -> {
+                // we are looking for the root dirs containing sources
+                if (a.getRelativePath().getSegments().length == 1) {
+                    final File srcDir = a.getFile().getParentFile();
+                    if (srcDirs.add(srcDir)) {
+                        addResources(module, srcDir, destDir, test);
                     }
                 }
             });
+        });
+        // there could be a task generating resources
+        if (resourcesOutputDir.exists() && (test ? module.getTestResources().isEmpty() : module.getMainResources().isEmpty())) {
+            sourceSet.getResources().getSrcDirs().forEach(srcDir -> addResources(module, srcDir, resourcesOutputDir, test));
         }
     }
 
-    private static void addResources(DefaultWorkspaceModule module, File srcDir, final File destDir, boolean test,
-            Collection<Path> addedSrcDirs) {
+    private static void addResources(DefaultWorkspaceModule module, File srcDir, final File destDir, boolean test) {
         final DefaultProcessedSources resrc = new DefaultProcessedSources(srcDir, destDir);
         if (test) {
             module.addTestResources(resrc);
         } else {
             module.addMainResources(resrc);
         }
-        addedSrcDirs.add(srcDir.toPath());
     }
 
     private void addSubstitutedProject(PathList.Builder paths, File projectFile) {