首页 文章

如何设置jetty.home嵌入式码头与maven

提问于
浏览
2

我正在使用jetty-server:9.2.11并且我希望在我的嵌入式jetty应用程序中有一个用于war部署的webapps目录,就像我们在使用独立的jetty服务器时一样 . 我阅读了一些文档并找到了“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应用程序配置“jetty.home”,因为我没有使用任何标准的jetty发行版 . 我只有单独的jar .

2 回答

  • 1

    jetty.home 在使用embedded-jetty时意义不大 .

    LikeJettyXml 正如其名称所暗示的那样,它是jetty-distribution的 etc/jetty-*.xml 文件的java版本,这就是它所声称的全部内容 .

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

    为了获得一些成功,你需要了解Jetty操作和功能的相当不错的方法,重点是 DeploymentManagerWebAppContext .

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

    例如:

    • jetty-distribution有一个 etc/webdefault.xml ,由 setDefaultsDescriptor() 调用引用,但不需要设置,因为 DeploymentManager 可以只使用jar文件中存在的相同webdefault.xml .

    • jetty-distribution有一个jetty.base,可以在启用 deploy 模块时预先构建 ${jetty.base}/webapps 目录 . 那个目录,jetty.base的概念,模块的思想和特征,甚至名称 webapps 都是任意的,并且不需要以嵌入式码头的方式存在 . 将该目录放在您想要的任何位置,根据您的心愿命名 .

    • scanIntervalextractWarsConfigurationManager 都是可选的 . 事实上,唯一的强制配置是 setMonitoredDirNamesetContexts
      大多数嵌入式Jetty设置都不需要

    • 设置 ContainerIncludeJarPattern (请参阅有关该属性的stackoverflow的其他问题)

    • 设置系统属性对于embedded-jetty也没有意义,嵌入式jetty中没有任何东西使用这些属性(这些属性仅用于jetty-distribution,它的start.jar和各种 ${jetty.home}/etc/*.xml 文件)

    希望这会有所帮助,请花点时间真正了解部署在Jetty中的工作方式(DeploymentManager的javadoc做了很好的解释) .

    部署可能是一个很大的主题,因此尝试在单个stackoverflow答案中覆盖所有内容都超出了范围 .

    你想要明白:

    • Jetty组件 LifeCycle

    • DeploymentManager 角色

    • 部署 AppLifeCycle

    • AppProvider 角色

    • WebAppContext

    • 服务器/ WebAppContext Configuration

    • WebAppContext初始化LifeCycle

  • 1

    通过一些研究,我从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( “web应用”);

相关问题