简体   繁体   中英

How to integrate Weld to the maven project?

Trying to import this projects (With Weld) and it can`t

import org.jboss.weld.environment.se

to use Weld class in project.

https://github.com/agoncal/agoncal-book-javaee7

It really depends on what you are trying to achieve. Are you writing a Java EE web application (war/ear) or a standalone app? Also: Weld is the reference implementation of CDI and CDI is just a part of Java EE. If you use CDI only (by setting up weld-se), you cannot use EJB/EntityManager/Transactional/... , its "just" CDI.

That all being said: using weld-se in a java standalone application can be done by importing

<dependency>
  <groupId>org.jboss.weld.se</groupId>
  <artifactId>weld-se</artifactId>
  <version>2.2.6.Final</version>
 </dependency>

Bootstrapping the application "Main.class" is done via

    // Initialize Weld
    Weld theWeld = new Weld();
    WeldContainer theContainer = theWeld.initialize();


    // Execute the run method
    theContainer.instance().select(Main.class).get().run();


    // Shutting down Weld again
    theWeld.shutdown();

According this book at first you should install aplication server to run your applications. In your case it will be Glassfish. The same question were discussed here .

I have also tried to run a sample from this book without any modifications. The reason it does not work is that non-existing Weld version is specified in the <dependency> block:

<dependency>
  <groupId>org.jboss.weld.se</groupId>
  <artifactId>weld-se</artifactId>
  <version>2.0.0</version>
 </dependency>

Just compare with the list of available versions and ensure the version 2.0.0 is not present here: http://repo1.maven.org/maven2/org/jboss/weld/se/weld-se/

You can use any other version, for example

<dependency>
  <groupId>org.jboss.weld.se</groupId>
  <artifactId>weld-se</artifactId>
  <version>2.0.0.Final</version>
</dependency>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM