"Argument bean must not be null" 将 DeltaSpike & Weld SE 打包为 uber-jar 时

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

我正在尝试使用 CDI、DeltaSpike(至 bootstrap)打包一个命令行应用程序,并将 Weld SE 作为 CDI 实现。该应用程序从我的 IDE 启动时运行良好,但在将应用程序打包到 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)

这是 maven-shade-plugin 配置:

  <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>

boot()函数很简单:

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);
}

鉴于这在从 IDE 启动时有效,这一定是包装问题,但我不知道是什么原因。

一个不同点可能是 CDI 在扫描 beans 时拾取的内容。在我的 beans.xml 中,除了我自己的应用程序之外,我基本上排除了所有包:

<?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>

关于从哪里开始的任何想法?

我找到了问题的解决方案。问题是 beans.xml 文件中定义的排除项过于宽泛。由于几个依赖库在扫描时导致 CDI 错误,我已经排除了 org.** 下的所有内容。具体来说,org.apache.deltaspike 包由 CDI 扫描很重要。在我的具体情况下,工作 beans.xml 看起来像这样:

<?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>