Search code examples
javamavencdiquarkus

CDI: Injecting a bean produced by a dependency


I'm creating a maven dependency, which has a MessageHandler class. Alongside which there is a MessageHandlerProduceer class:

public class MessageHandlerProducer {

    @Produces
    public MessageHandler messageHandler() {
        return new MessageHandler();
    }
}

Having listed the dependency in the core project, and when I'm trying to inject the MessageHandler bean, I'm getting the following 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 com.example.MessageHandler and qualifiers [@Default]

Solution

  • In a multi-module setup, we need information of beans provided by sibling modules. The easiest way to generate those information is enabling Jandex support through the jandex-plugin.

    For Maven, the following snippet should be added to the module providing the producer:

    <build>
      <plugins>
        <plugin>
          <groupId>org.jboss.jandex</groupId>
          <artifactId>jandex-maven-plugin</artifactId>
          <version>1.1.0</version>
          <executions>
            <execution>
              <id>make-index</id>
              <goals>
                <goal>jandex</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </build>
    

    For gradle, the following snippet can be used:

    plugins {
        id 'org.kordamp.gradle.jandex' version '0.11.0'
    }
    

    Both snippets are taken from the documentation linked above and are only included to make this answer self-contained. Credits for those go to Red Hat, as they are the maintainer of quarkus and provide the snippets in the documentation linked above.