尝试运行焊接时出错应用程序 - 无法初始化焊接SE容器 - 未找到任何Bean存档

ale*_*pfx 3 java gradle jboss-weld weld-se

我使用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
Run Code Online (Sandbox Code Playgroud)

我的理解是它找不到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>
Run Code Online (Sandbox Code Playgroud)

我的主要课程是:

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

    }
}
Run Code Online (Sandbox Code Playgroud)

构建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'
Run Code Online (Sandbox Code Playgroud)

J. *_*the 6

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

要解决这个问题,因为你正在使用应用程序插件,只需执行一个gradle installDist并运行shell脚本或批处理文件build/install/myapp/bin.