cdideltaspikeweld-se

"Argument bean must not be null" when packaging DeltaSpike & Weld SE as an uber-jar


I'm trying to package a command line application using CDI, DeltaSpike (to bootstrap) with Weld SE as the CDI implementation. The application runs fine when launched from my IDE but I'm getting an obtuse error message when packaging the application into an uber-jar:

Exception in thread "main" org.jboss.weld.exceptions.IllegalArgumentException: WELD-001456: Argument bean must not be null
at org.jboss.weld.util.Preconditions.checkArgumentNotNull(Preconditions.java:40)
at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:772)
at org.jboss.weld.util.ForwardingBeanManager.getReference(ForwardingBeanManager.java:61)
at org.jboss.weld.bean.builtin.BeanManagerProxy.getReference(BeanManagerProxy.java:85)
at org.apache.deltaspike.cdise.weld.WeldContainerControl.getContextControl(WeldContainerControl.java:138)
at com.katalystdm.sql.exec.CDIBootstrap.boot(CDIBootstrap.java:13)
at com.katalystdm.sql.exec.Main.main(Main.java:44)

Here's the maven-shade-plugin config:

  <plugin>
    <artifactId>maven-shade-plugin</artifactId>
    <configuration>
      <createDependencyReducedPom>false</createDependencyReducedPom>
      <filters>
        <filter>
          <artifact>*:*</artifact>
          <excludes>
            <exclude>**/LICENSE*</exclude>
            <exclude>**/NOTICE*</exclude>
          </excludes>
        </filter>
      </filters>
      <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
    </configuration>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

The boot() function is simple:

public static <T> T boot(CdiContainer container, Class<T> clazz)
{
  container.boot();

  ContextControl contextControl = container.getContextControl(); // <-- Exception thrown here!
  contextControl.startContexts();

  return BeanProvider.getContextualReference(clazz, false);
}

Given that this works when launching from the IDE it must be a packaging issue but I'm at a loss as to what the cause could be.

One difference could be what CDI picks up when scanning for beans. In my beans.xml I do exclude basically all packages except for my own application:

<?xml version="1.0" encoding="UTF-8"?>
<beans 
  xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
  xmlns:weld="http://jboss.org/schema/weld/beans" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
  version="1.1" 
  bean-discovery-mode="all">
  <scan>
    <exclude name="com.beust.**"/>
    <exclude name="com.google.**"/>
    <exclude name="com.opencsv.**"/>
    <exclude name="javax.**"/>
    <exclude name="oracle.**"/>
    <exclude name="org.**"/>
  </scan>  
</beans>

Any ideas on where to proceed from here?


Solution

  • I discovered the solution to the problem. The issue is that the exclusions defined in the beans.xml file were too broad. I had excluded everything under org.** due to several dependent libraries that caused CDI errors when scanned. Specifically, it is important that the org.apache.deltaspike packages are scanned by CDI. In my specific case the working beans.xml looked like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans 
      xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
      xmlns:weld="http://jboss.org/schema/weld/beans" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
      version="1.1" 
      bean-discovery-mode="all">
      <scan>
        <exclude name="com.beust.**"/>
        <exclude name="com.google.**"/>
        <exclude name="com.kelman.**"/>
        <exclude name="com.opencsv.**"/>
        <exclude name="javax.**"/>
        <exclude name="oracle.**"/>
        <exclude name="org.apache.commons.**"/>
        <exclude name="org.honton.**"/>
        <exclude name="org.jboss.**"/>
        <exclude name="org.skife.**"/>
        <exclude name="org.slf4j.**"/>
      </scan>  
    </beans>