如何使用 Maven 设置 jetty.home 嵌入式码头

how to set jetty.home Embedded jetty with maven

我正在使用 jetty-server:9.2.11,我想在我的嵌入式码头应用程序中有一个用于 war 部署的 webapps 目录,就像我们在使用独立码头服务器时所拥有的那样。我阅读了一些文档,发现 "Like Jetty XML" 程序化配置如下

    // Path to as-built jetty-distribution directory
    String jettyHomeBuild = "../../jetty-distribution/target/distribution";

    // Find jetty home and base directories
    String homePath = System.getProperty("jetty.home", jettyHomeBuild);
    File homeDir = new File(homePath);
    if (!homeDir.exists())
    {
        throw new FileNotFoundException(homeDir.getAbsolutePath());
    }
    String basePath = System.getProperty("jetty.base", homeDir + "/demo-base");
    File baseDir = new File(basePath);
    if(!baseDir.exists())
    {
        throw new FileNotFoundException(baseDir.getAbsolutePath());
    }

    // Configure jetty.home and jetty.base system properties
    String jetty_home = homeDir.getAbsolutePath();
    String jetty_base = baseDir.getAbsolutePath();
    System.setProperty("jetty.home", jetty_home);
    System.setProperty("jetty.base", jetty_base);







    // === jetty-deploy.xml ===
    DeploymentManager deployer = new DeploymentManager();
    deployer.setContexts(contexts);
    deployer.setContextAttribute(
            "org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
            ".*/servlet-api-[^/]*\.jar$");

    WebAppProvider webapp_provider = new WebAppProvider();
    webapp_provider.setMonitoredDirName(jetty_base + "/webapps");
    webapp_provider.setDefaultsDescriptor(jetty_home + "/etc/webdefault.xml");
    webapp_provider.setScanInterval(1);
    webapp_provider.setExtractWars(true);
    webapp_provider.setConfigurationManager(new PropertiesConfigurationManager());

    deployer.addAppProvider(webapp_provider);
    server.addBean(deployer);

但这里使用 "jetty-distribution" 但我的 lib 目录中只有 jetty 相关的 jar 文件被复制为 maven 依赖项。我有点被困在这里为我的嵌入式码头应用程序做我的配置 "jetty.home" 因为我没有使用任何标准码头 distribution.I 只有单独的罐子。

jetty.home 在使用 时意义不大。

LikeJettyXml 正如它的名字所暗示的那样,它是 jetty-distribution 的 etc/jetty-*.xml 文件的 java 版本,仅此而已并声称是。

如果你想设置一个 webapps 目录(又名 DeploymentManagerWebAppProvider),那么就这样做。

为了在这方面取得一些成功,您需要相当全面地了解 Jetty 的运作和功能,重点是 DeploymentManagerWebAppContext

虽然 LikeJettyXml 向您展示了一些功能,但它是从码头分布而非嵌入式码头的角度向您展示的。

例如:

  • jetty-distribution 有一个 etc/webdefault.xmlsetDefaultsDescriptor() 调用引用,但不需要设置,因为 DeploymentManager 可以使用相同的 webdefault.xml 而是存在于 jar 文件中。
  • jetty-distribution 有一个 jetty.base 可以在您启用 deploy 模块时预先构建 ${jetty.base}/webapps 目录。该目录、jetty.base 的概念、模块的概念和功能,甚至名称 webapps 都是任意的,对于嵌入式码头而言不需要以这种方式存在。将该目录放在您想要的任何位置,随心所欲地命名。
  • scanIntervalextractWarsConfigurationManager都是可选的。事实上,唯一必须的配置是 setMonitoredDirNamesetContexts
  • 大多数嵌入式码头设置不需要设置 ContainerIncludeJarPattern(有关该属性的信息,请参阅关于 Whosebug 的其他问题)
  • 设置系统属性对 embedded-jetty 也没有意义,embedded-jetty 中没有任何东西使用这些属性(这些属性仅用于 jetty-distribution,其 start.jar,以及各种 ${jetty.home}/etc/*.xml文件)

希望这对您有所帮助,请花一些时间真正了解部署在 Jetty 中的工作原理(javadoc for DeploymentManager 对此做了很好的解释)。

部署可能是一个大话题,因此试图在一个 Whosebug 答案中涵盖所有内容超出了范围。

你会想明白:

  • Jetty 组件LifeCycle
  • DeploymentManager角色
  • 部署AppLifeCycle
  • AppProvider角色
  • WebAppContext
  • 服务器/WebAppContextConfiguration
  • WebAppContext 初始化生命周期

通过一些研究,我从 jetty-like.xml 中过滤了我需要的东西,下面是我们需要保留的东西

    DeploymentManager deployer = new DeploymentManager();
    deployer.setContexts(contexts);
    deployer.setContextAttribute(
            "org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
            ".*/servlet-api-[^/]*\.jar$");

    WebAppProvider webapp_provider = new WebAppProvider();
    webapp_provider.setMonitoredDirName("webapps");
    webapp_provider.setScanInterval(1);
    webapp_provider.setExtractWars(true);
    webapp_provider.setConfigurationManager(new PropertiesConfigurationManager());

    deployer.addAppProvider(webapp_provider);
    server.addBean(deployer);

看这里不需要jetty_base或jetty_home

webapp_provider.setMonitoredDirName("webapps");