【问题标题】:Error when try run Weld Se Applicaton - Weld SE container cannot be initialized - no bean archives found尝试运行 Weld Se Applicaton 时出错 - Weld SE 容器无法初始化 - 找不到 bean 档案
【发布时间】:2016-09-13 00:09:54
【问题描述】:

我使用 Weld SE 创建了一个简单的 JavaSE 应用程序。 我尝试使用 gradle run 运行它会引发异常:

:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:runmai 17, 2016 12:55:55 AM org.jboss.weld.bootstrap.WeldStartup <clinit>
INFO: WELD-000900: 2.3.4 (Final)
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:690)

        at org.jboss.weld.environment.se.Weld.initialize(Weld.java:570)
        at br.com.alexpfx.crawler.run.Main.main(Main.java:14)
 FAILED
1 result found for 'bean-discovery-mode'Finding with Options: Case Insensitive

y
bean-discovery-mode
1 found
Find






Replace in current buffer
Replace
Replace All
Gradle: runFile 0Project 0No Issuessrc\main\resources\META-INF\beans.xml10:1
CRLFUTF-8XML1 update

我的理解是它找不到 beans.xml 文件。但它在 src/main/resources/META-INF/beans.xml 中:

<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
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"
bean-discovery-mode="all"
        version="1.1">


</beans>

我的主要课程是:

package br.com.alexpfx.crawler.run;
import org.jboss.weld.environment.se.*;
import javax.enterprise.inject.*;
import javax.inject.*;
import br.com.alexpfx.crawler.*;
public class Main {

    @Inject
    private @Named("SuperMarket") Crawler crawler;


    public static void main(String[] args) {
        Weld weld = new Weld();
        WeldContainer container = weld.initialize();
        Main main = container.instance().select(Main.class).get();
        main.run();
        weld.shutdown();
    }


    public void run() {

        System.out.println (crawler);

    }
}

构建 gradle 文件是:

apply plugin: 'java'
apply plugin: 'application'



repositories{
    mavenCentral()
}


dependencies{
    // http://mvnrepository.com/artifact/org.jsoup/jsoup
    compile group: 'org.jsoup', name: 'jsoup', version: '1.9.1'

    // http://mvnrepository.com/artifact/org.jboss.weld.se/weld-se-core
    compile group: 'org.jboss.weld.se', name: 'weld-se-core', version: '2.3.4.Final'



}

sourceCompatibility = '1.8'
version = '1.0'
mainClassName = 'br.com.alexpfx.crawler.run.Main'

【问题讨论】:

    标签: java gradle jboss-weld weld-se


    【解决方案1】:

    问题是gradle run 没有为您的应用构建 jar 文件。它只是编译类并使用类路径上的资源目录运行主类(请执行gradle --info run 自己查看)。 Weld 需要一个bean archive,它是一个包含META-INF/beans.xml 的jar 文件或一个包含WEB-INF/classes/META-INF/beans.xml 的war 文件。 beans.xml 仅仅在类路径上是不够的。

    要解决这个问题,由于您使用的是应用程序插件,只需执行 gradle installDist 并在 build/install/myapp/bin 中运行 shell 脚本或批处理文件。

    【讨论】:

    • 找不到任务 installDist
    【解决方案2】:

    我通过在 build.gradle 中添加 sourceSets.main.resources 解决了这个问题:

    sourceSets.main.resources {
       exclude 'META-INF/beans.xml'
    }
      classes {
       inputs.dir 'src/main/resources/META-INF/beans.xml'
       outputs.dir "$buildDir/classes/main/META-INF/beans.xml"
    } << {
       copy {
          from('src/main/resources') { include 'META-INF/beans.xml' }
          into "$buildDir/classes/main/"
       }
    }
    

    用给定的例子:

    apply plugin: 'java'
    apply plugin: 'application'
    
    
    
    repositories{
        mavenCentral()
    }
    
    
    dependencies{
        // http://mvnrepository.com/artifact/org.jsoup/jsoup
        compile group: 'org.jsoup', name: 'jsoup', version: '1.9.1'
    
        // http://mvnrepository.com/artifact/org.jboss.weld.se/weld-se-core
        compile group: 'org.jboss.weld.se', name: 'weld-se-core', version: '2.3.4.Final'
    }
    
    sourceSets.main.resources {
       exclude 'META-INF/beans.xml'
    }
      classes {
       inputs.dir 'src/main/resources/META-INF/beans.xml'
       outputs.dir "$buildDir/classes/main/META-INF/beans.xml"
    } << {
       copy {
          from('src/main/resources') { include 'META-INF/beans.xml' }
          into "$buildDir/classes/main/"
       }
    }
    
    sourceCompatibility = '1.8'
    version = '1.0'
    mainClassName = 'br.com.alexpfx.crawler.run.Main'
    

    【讨论】:

      【解决方案3】:

      我已经尝试过@drenedo 提供的解决方案(对不起,我没有足够的评论声誉),以下代码对我来说很好:

      sourceSets.main.resources {
          exclude 'META-INF/beans.xml'
      }
      classes {
          doLast{
              copy {
                  from('src/main/resources') { include 'META-INF/beans.xml' }
                  into "$buildDir/classes/java/main/"
              }
          }
      }
      

      我对以下代码的用途有些困惑。由于src/main/resources/META-INF/beans.xml$buildDir/classes/main/META-INF/beans.xml 不是目录,因此此代码会导致错误。所以我只是简单地删除它:

      inputs.dir 'src/main/resources/META-INF/beans.xml'
      outputs.dir "$buildDir/classes/main/META-INF/beans.xml"
      

      @J 回答的解决方案。 Lenthe 也很适合我。

      你也可以试试这个bean-discovery-problems-when-using-weld-se-with-gradle-application-plugin

      task copyResources(type: Copy) {
          from "${projectDir}/src/main/resources"
          into "${buildDir}/classes/java/main"
      }
      processResources.dependsOn copyResources
      

      Gradle 4+ 对类使用不同的输出目录,因此请记住使用 ${buildDir}/classes/java/main 而不是 ${buildDir}/classes/main

      【讨论】:

        猜你喜欢
        • 2018-05-04
        • 2014-11-06
        • 1970-01-01
        • 2019-06-28
        • 2020-03-25
        • 1970-01-01
        • 1970-01-01
        • 2014-10-24
        • 2016-02-14
        相关资源
        最近更新 更多