Shaharg
Shaharg

Reputation: 1029

Using Weld SE and CDI in IntelliJ

I am trying to write a basic project using Weld SE and CDI in Intellij (Ultimate) When I create the project I choose Jakarta EE, the template is Web Application and the build system is Maven (default). In the dependencies, I add CDI and Weld SE. I'm using JDK 17.

This is my code:

public interface Pet {
}



public class Dog implements Pet {
}


public class Person {
    Pet itsPet;

    @Inject
    public Student(Pet pet){
        itsPet = pet;
    }
}


import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;

    
public class MainApp {
    public static void main(String[] args) {
        WeldContainer container = new Weld().initialize();
        Person person = container.select(Person.class).get();
    }
}

Here is the error code I get

Mar 17, 2023 8:21:41 AM org.jboss.weld.bootstrap.WeldStartup <clinit>
INFO: WELD-000900: 4.0.3 (Final)
Mar 17, 2023 8:21:41 AM org.jboss.weld.environment.deployment.discovery.ReflectionDiscoveryStrategy processAnnotatedDiscovery
INFO: WELD-ENV-000014: Falling back to Java Reflection for bean-discovery-mode="annotated" discovery. Add org.jboss:jandex to the classpath to speed-up startup.
Exception in thread "main" java.lang.IllegalStateException: WELD-ENV-002009: Weld SE container cannot be initialized - no bean archives found
at org.jboss.weld.environment.se.Weld.createDeployment(Weld.java:995)
at org.jboss.weld.environment.se.Weld.initialize(Weld.java:787)
at student.MainApp.main(MainApp.java:8)

I'm not sure if this is relevant, but here is my pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>try</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>try</name>
    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.target>11</maven.compiler.target>
        <maven.compiler.source>11</maven.compiler.source>
        <junit.version>5.9.1</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>jakarta.servlet</groupId>
            <artifactId>jakarta.servlet-api</artifactId>
            <version>5.0.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.weld.se</groupId>
            <artifactId>weld-se-core</artifactId>
            <version>4.0.3.Final</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.2</version>
            </plugin>
        </plugins>
    </build>
</project>

Under target/classes/META-INF I have the beans.xml file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://jakarta.ee/xml/ns/jakartaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/beans_3_0.xsd"
       bean-discovery-mode="annotated">

</beans>

Can you please suggest a solution?

Upvotes: 0

Views: 584

Answers (1)

Shaharg
Shaharg

Reputation: 1029

A possible solution is to change the bean-discovery-mode (in beans.xml) from "annotated" to "all".

My problem was mainly due to the fact that I looked at the beans.xml under target/classes/META-INF instead of resources/classes/META-INF. The beans.xml file is copied from the resources to the target at the beginning of the execution. My earlier attempts were to change the beans in the target, which makes no sense since it is overridden.

Upvotes: 0

Related Questions