0

I am trying to built a embedded jetty in one fat jar. I built the app using a tuto for jetty-embedded offering rest service, which I also need in my application. Now I am trying to add/include a jsf based context. Everything works fine in Eclipse, but as soon as deploy using maven clean install and I try to execute the jar somewhere esle, I get the following error:

Exception in thread "main" java.lang.IllegalArgumentException: file:///home/debbie/src/main/webapp is not an existing directory. at org.eclipse.jetty.util.resource.ResourceCollection.(ResourceCollection.java:108) at com.example.haaa.server.App.start(App.java:43) at com.example.haaa.server.App.main(App.java:62)

I checked the jar and it doesnt contain none of the webapp resources, but I do not understand how I can tell maven to include those files into the jar, or how I can solve my issue.

Following my simplified pom.xml (got rid of dependencies here, as I dont expect that to be any matter.

<project xmlns="http://maven.apache.org/POM/4.0.0"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example</groupId>
  <artifactId>haaa</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>haaa</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

    <repositories>
        <repository>
            <id>prime-repo</id>
            <name>Prime Repo</name>
            <url>http://repository.primefaces.org</url>
        </repository>
    </repositories>


  <build>
    <resources>
    <resource>
        <directory>src/main/resources</directory>
    </resource>
    <resource>
        <directory>src/main/webapp</directory>
    </resource>
    </resources>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>1.6</version>
        <configuration>
          <createDependencyReducedPom>true</createDependencyReducedPom>
          <filters>
            <filter>
              <artifact>*:*</artifact>
              <excludes>
                <exclude>META-INF/*.SF</exclude>
                <exclude>META-INF/*.DSA</exclude>
                <exclude>META-INF/*.RSA</exclude>
              </excludes>
            </filter>
          </filters>
        </configuration>

        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer
                  implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
                <transformer
                  implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <manifestEntries>
                    <Main-Class>com.example.haaa.server.App</Main-Class>
                  </manifestEntries>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

And following the code in my com.example.haaa.server.App:

package com.example.haaa.server;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.eclipse.jetty.security.HashLoginService;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.resource.ResourceCollection;
import org.eclipse.jetty.webapp.WebAppContext;
import org.glassfish.jersey.servlet.ServletContainer;

import com.example.haaa.service.AbuseService;

public class App {

    final static Logger logger = Logger.getLogger(App.class);

    private final Server server;

    public App() {
        server = new Server(8080);
    }

    public void start() throws Exception {    
        // REST
        WebAppContext restHandler = new WebAppContext();

        restHandler.setResourceBase("./");
        restHandler.setClassLoader(Thread.currentThread().getContextClassLoader());

        ServletHolder restServlet = restHandler.addServlet(ServletContainer.class,  "/abuse/*");
        restServlet.setInitOrder(0);
        Map<String, String> initparams = new HashMap<String, String>();
        initparams.put("jersey.config.server.provider.classnames", AbuseService.class.getCanonicalName()+","+"org.glassfish.jersey.filter.LoggingFilter;org.glassfish.jersey.moxy.json.MoxyFeature;org.glassfish.jersey.media.multipart.MultiPartFeature");
        restServlet.setInitParameters(initparams);
        // Web
        WebAppContext webHandler = new WebAppContext();
        webHandler.setResourceAlias("/WEB-INF/classes/", "/classes/");
        webHandler.setBaseResource(
                new ResourceCollection(
                    new String[] { "./src/main/webapp", "./target" }));
        webHandler.setContextPath("/web/");

        HashLoginService loginService = new HashLoginService("HaaaRealm");
        loginService.setConfig("./src/main/webapp/loginrealm.txt");
        System.out.println(loginService.getConfig());
        server.addBean(loginService);

        // Server
        HandlerCollection handlers = new HandlerCollection();
        handlers.addHandler(webHandler);
        handlers.addHandler(restHandler);

        server.setHandler(handlers);
        server.start();
    }

    public static void main(String[] args) throws Exception {
        new App().start();
    }
}

If I should have left out any relevant information, please let me know.

Lucky
  • 16,787
  • 19
  • 117
  • 151
Ilir
  • 430
  • 2
  • 7
  • 19

1 Answers1

2

If you have a WebAppContext, you have a formal servlet webapp (a war file), not a jar.

The various Resource, ResourceBase, and ResourceCollection references must be fully qualified URIs, not relative paths like you are using. (These references can be jar:file://path/to/my.jar!/path/to/resource style of URIs)

Since you have 2 webapps, each declared via WebAppContext, you'll have 2 wars. Which your fat jar environment does not support.

Perhaps you want to simplify, and not use a WebAppContext and all of the baggage it comes with.

If you use a ServletContextHandler, you'll gain the ability to setup a uber-jar easier, and supporting multiple webapp contexts. But you'll lose the ability to configure the webapp via WEB-INF/web.xml relying on programmatic setup instead (this includes any container discovered components via the WebAppContext's bytecode/annotation scanning layer)

Take a look at the example project that Jetty maintains at

https://github.com/jetty-project/embedded-jetty-uber-jar

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136