Jessy
Jessy

Reputation: 311

How to add Weld CDI support to a working Jetty application?

I am currently using Jetty 9 for deploying my WAR files.

For dependency management, I'm using Gradle; my build.gradle file looks as follows:

apply plugin: 'java'
apply plugin: 'war'

repositories {
    mavenCentral()
}

dependencies {
    /**
     * Jersey.
     */
    compile 'org.glassfish.jersey.containers:jersey-container-servlet:2.22.1'
    compile 'org.glassfish.jersey.core:jersey-server:2.22.1'
    /**
     * Weld CDI.
     */
    compile 'org.jboss.weld.servlet:weld-servlet-core:2.3.2.Final'

    /**
     * JUnit.
     */
    testCompile 'junit:junit:4.12'
}

My web.xml looks as follows:

<web-app version="3.0">
<display-name>Example Api</display-name>
<servlet>
    <servlet-name>RESTful Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.example.api</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>RESTful Service</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
<listener>
    <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>
</web-app>

However, when I try to deploy, I get the following stack trace:

[2015-12-16 09:13:33,315] Artifact Gradle : api : api.war: Artifact is being deployed, please wait...
Dec 16, 2015 9:13:36 PM org.jboss.weld.environment.servlet.EnhancedListener onStartup
INFO: WELD-ENV-001008: Initialize Weld using ServletContainerInitializer
Dec 16, 2015 9:13:36 PM org.jboss.weld.bootstrap.WeldStartup <clinit>
INFO: WELD-000900: 2.3.2 (Final)
Dec 16, 2015 9:13:36 PM org.jboss.weld.environment.servlet.WeldServletLifecycle initialize
INFO: WELD-ENV-000028: Weld initialization skipped - no bean archive found
Dec 16, 2015 9:13:36 PM org.jboss.weld.environment.servlet.Listener contextInitialized
INFO: WELD-ENV-001007: Initialize Weld using ServletContextListener
Dec 16, 2015 9:13:36 PM org.jboss.weld.environment.servlet.WeldServletLifecycle initialize
INFO: WELD-ENV-000028: Weld initialization skipped - no bean archive found

Followed by java.lang.IllegalStateException: Singleton not set for STATIC_INSTANCE => [].

Can somebody please tell me what needs to be done to get this working?

I am just trying to @Inject my dependencies in Jetty, but I only find myself struggling with configuration settings for days on end...

Upvotes: 0

Views: 2139

Answers (1)

Joakim Erdfelt
Joakim Erdfelt

Reputation: 49487

When you want to use Weld/CDI on Jetty Distribution, you'll need to do the following.

This is partly documented on the Weld/CDI side at Environments: Jetty

  1. Include the weld jars appropriate for your usage in the WEB-INF/lib directory of your WAR file. (lets call this mywebapp.war)
  2. Create a ${jetty.base} directory, and configure it properly.
  3. DO NOT EDIT / CHANGE / DELETE / RENAME any content in ${jetty.home} or $JETTY_HOME. If you are doing this, you are already not using jetty properly.
  4. Create a XML Deployable to allow Weld/CDI to see through the Standard Servlet Classloader Isolation with a file called ${jetty.base}/webapps/mywebapp.xml
  5. Copy mywebapp.war to ${jetty.base}/webapps/ directory.
  6. Start jetty.

Example of ${jetty.base} setup

$ mkdir /path/to/mybase
$ cd /path/to/mybase
$ java -jar /path/to/jetty-dist/start.jar --add-to-start=http,deploy

Example of ${jetty.base}/webapps/mywebapp.xml

<?xml version="1.0"?>

<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <Set name="contextPath">/weld-numberguess</Set>
  <Set name="war"><Property name="jetty.webapps" default="."/>/mywebapp.war</Set>
  <Call name="prependServerClass">
    <Arg>-org.eclipse.jetty.server.handler.ContextHandler</Arg>
  </Call>
  <Call name="prependServerClass">
    <Arg>-org.eclipse.jetty.servlet.FilterHolder</Arg>
  </Call>
  <Call name="prependServerClass">
    <Arg>-org.eclipse.jetty.servlet.ServletContextHandler</Arg>
  </Call>
  <Call name="prependServerClass">
    <Arg>-org.eclipse.jetty.servlet.ServletHolder</Arg>
  </Call>
</Configure>

Upvotes: 1

Related Questions