Search code examples
javamavencdiquarkus

Quarkus CDI not working when creating a library


I have two projects A and B in project B and i want to injcet a CDI bean from project A however, this fails due too

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: jakarta.enterprise.inject.spi.DeploymentException: jakarta.enterprise.inject.UnsatisfiedResolutionException: Unsatisfied dependency for type test.xml.filereader and qualifiers [@Default] - java member: org.acme.hibernate.orm.XmlResource#fileReader - declared on CLASS bean [types=[org.acme.hibernate.orm.XmlResource, java.lang.Object], qualifiers=[@Default, @Any], target=org.acme.hibernate.orm.XmlResource]

Project A has a CDI bean like this

@Slf4j
@ApplicationScoped
public class FileReader {

    @Inject
    Configuration configuration;

    public void testCDIInject() {
    }
    
}

project B wants to use this


@Path("/test")
public class XmlResource {

  @Inject
  FileReader fileReader;

  @GET
  public String get() {
    fileReader.testCDIInject();
    return "asdf";
  }
}

for some reason i get the error above. why?

i install project A with mvn clean install -U

and have it in project Bs pom.xml


Solution

  • From the related Quarkus guide

    By default, Quarkus will not discover CDI beans inside another module.

    The best way to enable CDI bean discovery for a module in a multi-module project would be to include the jandex-maven-plugin, unless it is the main application module already configured with the quarkus-maven-plugin, in which case it will be indexed automatically.

    In project A you should generate the jandex.idx like:

    <?xml version="1.0"?>
    <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
             xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.example</groupId>
        <artifactId>project-a</artifactId>
        <version>1.0-SNAPSHOT</version>
    
    <!-- dependencies and others... -->
    
        <build>
            <plugins>
                <plugin>
                    <groupId>io.smallrye</groupId>
                    <artifactId>jandex-maven-plugin</artifactId>
                    <version>3.1.1</version>
                    <executions>
                        <execution>
                            <id>make-index</id>
                            <goals>
                                <goal>jandex</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
    <!-- Other plugin configurations e.q. quarkus-maven-plugin... -->
    
            </plugins>
        </build>
    
    
    </project>
    

    Now during project B build CDI bean which defined in project A will be available in project B. Just add project A as a dependency to project B

    <?xml version="1.0"?>
    <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
             xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.example</groupId>
        <artifactId>project-b</artifactId>
        <version>1.0-SNAPSHOT</version>
    
    <!-- properties and managed dependencies -->
    
        <dependencies>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>project-a</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
    
            <!-- other  dependencies -->
        </dependencies>
    
        <!-- plugins, site configurations and so on -->
    </project>