【问题标题】:updating embedded jetty webapp in eclipse during run在运行期间在 Eclipse 中更新嵌入式码头 webapp
【发布时间】:2015-10-14 21:41:53
【问题描述】:

我们有一个 maven 生成的 webapp,它运行在一个嵌入式码头服务器中,所有这些都被塞进一个胖罐子里。 这似乎工作正常,但我们的 UI 开发人员很不高兴,因为在做他的 UI 工作时,他想对他的代码进行更改,而不必重新启动 web 应用程序以使其显示。在我做所有罐子的东西之前,它就是这样工作的。 我们在 pom 中有一个资源部分,如下所示:

    <resources>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
        <resource>
            <directory>src/main/webapp</directory>
            <targetPath>webapp</targetPath>
        </resource>
    </resources>

我们已经确定似乎正在发生的事情是 eclipse 正在将 webapp 复制到它在目标中的位置,然后从那里执行。我认为他想要的是让 eclipse 使用 src 文件夹中的 webapp 源,并且只在打包部署时复制 webapp。

将其设置为按预期方式工作的最佳方法是什么?

【问题讨论】:

  • 澄清一下,这是他想要更新的静态 html。

标签: java eclipse maven jetty embedded-jetty


【解决方案1】:

您有一个拆分资源的场景。

传统上,WebApp 有 1 个可去处,即 webapp 资源库(Jetty 中的WebAppContext.setBaseResource())。这是所有静态内容、WEB-INF 元数据 (xml)、WEB-INF/classesWEB-INF/lib/*.jar 文件的加载位置。

在您的场景中,您将从您的 target/mywebapp-1.0-SNAPSHOT 加载所有内容(由 maven-war-plugin 的 ${webappDirectory} 定义的实际路径)

这是生产能力WebAppContext 的典型和正常行为,因为它被配置为使用简单的战争路径基础资源。但是,您正在编辑 src/main/webapp/ 目录(由 maven-war-plugin 的 ${warSourceDirectory} 定义的实际路径)中的文件,该目录不属于 Base Resource 目录。

您需要做的是忽略${webappDirectory} 中的静态内容,并将${warSourceDirectory} 中的静态内容用于WebAppContext.setBaseResource(),并使用@ 外部的类和jar(maven 依赖项) 987654338@和${baseResource}/WEB-INF/lib

一般技术:

WebAppContext context = new WebAppContext();
context.setContextPath("/");

switch (getOperationalMode())
{
    case PROD:
        // Configure as WAR
        context.setWar(basePath.toString());
        break;
    case DEV:
        // Configuring from Development Base
        context.setBaseResource(new PathResource(basePath.resolve("src/main/webapp")));
        // Add webapp compiled classes & resources (copied into place from src/main/resources)
        Path classesPath = basePath.resolve("target/thewebapp/WEB-INF/classes");
        context.setExtraClasspath(classesPath.toAbsolutePath().toString());
        break;
    default:
        throw new FileNotFoundException("Unable to configure WebAppContext base resource undefined");
}

有关live version of this behavior,请参阅 Eclipse Jetty 项目团队维护的embedded-jetty-live-war example project

【讨论】:

  • 感谢您的示例!这看起来正是我们正在寻找的那种东西。
【解决方案2】:

我不确定这是 Maven POM 问题。不就是为了包装吗?

您的资源库使用什么? WebAppContext 的 setResourceBase 需要设置为 Web 应用的根目录。

【讨论】:

  • webapp,与 pom 文件中的 targetPath 使用的值相同。
猜你喜欢
  • 2018-03-13
  • 2017-02-16
  • 1970-01-01
  • 2014-05-24
  • 1970-01-01
  • 2012-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多