Maven Jetty Embedded(Fat jar)-Jar中缺少src/main/webapp [英] maven jetty embedded (fat jar) - src/main/webapp missing in jar

查看:128
本文介绍了Maven Jetty Embedded(Fat jar)-Jar中缺少src/main/webapp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在一个胖子罐中建造一个嵌入式码头.我使用一个tuto来构建该应用程序,该程序用于嵌入码头的提供休息服务,这在我的应用程序中也需要.现在,我正在尝试添加/包括基于jsf的上下文.一切在Eclipse中都可以正常工作,但是一旦使用maven clean install进行部署,并且尝试在esle某处执行jar,就会出现以下错误:

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:

线程主"中的异常java.lang.IllegalArgumentException:file:///home/debbie/src/main/webapp不是现有目录. 在org.eclipse.jetty.util.resource.ResourceCollection.(ResourceCollection.java:108) 在com.example.haaa.server.App.start(App.java:43) 在com.example.haaa.server.App.main(App.java:62)

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)

我检查了jar,但其中不包含任何webapp资源,但是我不明白如何告诉maven将这些文件包含在jar中,或者如何解决我的问题.

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.

在简化的pom.xml之后(这里不再依赖,因为我希望这没有关系.

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>

并按照我的com.example.haaa.server.App中的代码进行操作:

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.

推荐答案

如果您有WebAppContext,则说明您有一个正式的servlet webapp(war文件),而不是jar.

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

各种ResourceResourceBaseResourceCollection引用必须是完全限定的URI,而不是您所使用的相对路径. (这些引用可以是jar:file://path/to/my.jar!/path/to/resource样式的URI)

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)

由于您有2个Web应用程序,每个Web应用程序均通过WebAppContext声明,因此您将有2次Wars.您的胖子罐子环境不支持哪个.

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

也许您想简化,而不使用WebAppContext及其附带的所有行李.

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

如果使用ServletContextHandler,则可以轻松设置uber-jar,并支持多个webapp上下文.但是您将失去通过WEB-INF/web.xml而是依靠程序设置来配置Web应用程序的能力(这包括通过WebAppContext的字节码/注释扫描层发现的所有容器组件)

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)

看看Jetty维护的示例项目

Take a look at the example project that Jetty maintains at

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

这篇关于Maven Jetty Embedded(Fat jar)-Jar中缺少src/main/webapp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆