Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] embedded jetty and onejar plugin

Instead of one-jar, try the assembly plugin with it's `jar-with-dependencies` built-in descriptorRef:
http://ondra.zizka.cz/stranky/programovani/java/maven/maven-create-distribution-package.texy

Ondra



On Fri, 2011-04-22 at 09:29 +0200, Lars Vonk wrote:
Hi,


I am trying to create an application using embedded Jetty (version 7.4.0.v20110414) and want to package it using the onejar maven plugin (http://code.google.com/p/onejar-maven-plugin/).


Here is the code where I start jetty:


    Server server = new Server(8180);
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    context.setWelcomeFiles(new String[]{"index.html"});


    ServletHolder servletHolder = new ServletHolder(new DefaultServlet());
    URL resource = getClass().getResource("/web/");
    String resourceBase = resource.toURI().toString();
    servletHolder.setInitParameter("resourceBase", resourceBase);
    context.addServlet(servletHolder, "/");


    server.setHandler(context);
    server.start();




When I do java -jar onejar.jar and go to http://localhost:8180/index.html I get a 404.




My onejar.jar looks like this:


onejar.jar
main/myapp.jar
main/myapp.jar/web/index.html




(Rest omitted for readability).




I debugged through the application and noticed it throws (and ignores) an exception in the org.eclipse.jetty.util.resource.JarFileResource class in the method exists() in this piece of code:


try
                {
                    JarURLConnection c=(JarURLConnection)((new URL(_jarUrl)).openConnection());
                    c.setUseCaches(getUseCaches());
                    jarFile=c.getJarFile();
                }
                catch(Exception e)
                {
                       Log.ignore(e);
                }


The _jarUrl contains: jar:file:/target/onejar.jar!/main/myapp.jar!/web/index.html and throws a ZipException when doing c.getJarFile(): java.util.zip.ZipException: error in opening zip file.




Does anyone has experience in using embedded jetty with the mentioned onejar plugin? What I'd like to do is do java -jar myjar.jar where myjar contains everything that is needed, no additional files and such.


Extra information:


I also created a unit test for this asfollows:


@Test
  public void defaultServlet() throws Exception {
    final Server server = new Server(8181);
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    context.setWelcomeFiles(new String[]{"index.html"});
    DefaultServlet defaultServlet = new DefaultServlet();
    ServletHolder servlet = new ServletHolder(defaultServlet);
    servlet.setInitParameter("resourceBase", "jar:file:/target/onejar.jar!/main/myapp.jar!/web/");
    context.addServlet(servlet, "/");
    server.setHandler(context);
    server.start();
    Resource resource = defaultServlet.getResource("/index.html");
    assertNotNull(resource);
    server.stop();
  }


This tests succeeds, meaning resource is not null. Does a real request use something else to find the resources that the getResource method on the servlet?


Thanks in advance,


Lars 


_______________________________________________
jetty-users mailing list
jetty-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/jetty-users


Back to the top