0

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

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
Jessy
  • 311
  • 4
  • 12
  • Are you using [tag:embedded-jetty]? (either your own, or from gradle?) or are you deploying to jetty-distribution? – Joakim Erdfelt Dec 16 '15 at 20:49
  • @JoakimErdfelt I'm deploying to a jetty distribution (literally dropping the .war inside $JETTY_HOME/webapps and running `java -jar start.jar`). – Jessy Dec 16 '15 at 20:55

1 Answers1

1

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>
Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
  • Thanks for the step-by-step explanation! I've tried to follow it, but now I'm getting the following error: `2015-12-17 21:14:32.102:INFO:oejw.StandardDescriptorProcessor:main: NO JSP Support for /, did not find org.eclipse.jetty.jsp.JettyJspServlet` again followed by `java.lang.IllegalStateException: Singleton not set for STATIC_INSTANCE => []` I've tried looking for the jar containing the JettyJspServlet class, but I can't find the right dependency in www.mvnrepository.com. Do you know where to find it? – Jessy Dec 17 '15 at 20:15
  • jsp is a different topic, a new / different question please. – Joakim Erdfelt Dec 17 '15 at 20:19