如何 运行 在 eclipse 中的 jetty 中使用多个 webapps

How to run multiple webapps in jetty in eclipse

我正在尝试在 Eclipse 中为 gradle 多 Web 应用程序设置开发环境。该应用程序部署在码头的生产环境中,所以这就是我想在开发中使用的东西。我无法让 eclipse 访问 运行 所有 web 应用程序,也无法进行调试。

我在网上找到的解决方案使用只能 运行 单个网络应用程序的插件。或者他们 运行 服务器中的 gradle (gretty) 网络应用程序会导致调试问题。

我的来源是一个多项目 gradle 应用程序。它编译正确,有 docker 脚本可以 运行 软件。在 Eclipse 中,所有内容都可以毫无错误地编译并且似乎工作正常。我不知所措,但是如何在 Eclipse 的码头中同时实际 run/debug 所有 Web 应用程序。我可以用 tomcat 和 websphere 做的事情。

你们中的任何人都可以向我建议一种允许我在 eclipse 中调试此设置的方法吗?理想情况下,我可以从 gradle 进行配置。我应该构建一个 运行 嵌入式服务器的项目吗? (这可以自动检测并使用我现有的 web.xml 文件吗?)或者我应该继续使用 gretty(可以直接通过 eclipse 进行调试)还是我缺少其他工具?

我不可能是唯一一个使用此设置的人。常见的解决方案是什么?

互联网上很少有专门解决此问题的信息。所以我会花时间回答我自己的问题,希望它能帮助别人。

Gretty 无法处理这个问题,Gretty 对您的应用程序做出了强烈的假设,如果您从一开始就构建它并坚持这些假设,那么您就可以了,这可能会很有帮助。但解决方案仍然缓慢且不灵活。

没有jetty的eclipse插件,虽然jetty是一个eclipse(基础)项目,wtp帮不了你..

我最终选择了嵌入式码头路线。这似乎比实际更难。但最终结果就像一个魅力。极快的启动时间(我们的 12 个 webapp 项目从 30+ 秒减少到 5 秒)(Gretty 花了几分钟)整个应用程序变得更加清晰。有一个简单的 java class 定义了什么是 运行。不难读 XML.

这是基本结构

this.server = new Server(port);

setupAnnotationScanning(server);
setupJAASLoginService(server, config);

HandlerCollection hc = new HandlerCollection(true);
startWebApp(config, hc, "/app1", "app1");

// Root must be defined last or it will interfere with non root webapps
startWebApp(config, hc, "/", "rootapp");

server.setHandler(hc);

注释扫描不错,我要

private static void setupAnnotationScanning(Server server) {
    Configuration.ClassList classlist = Configuration.ClassList.setServerDefault(server);
    classlist.addAfter("org.eclipse.jetty.webapp.FragmentConfiguration", "org.eclipse.jetty.plus.webapp.EnvConfiguration", "org.eclipse.jetty.plus.webapp.PlusConfiguration");
    classlist.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration", "org.eclipse.jetty.annotations.AnnotationConfiguration");
}

JAAS 登录服务更难设置,它假定磁盘上有一个配置文件,我没有那个根目录,无论如何我想从我自己的 bootstrap 属性 服务中获取它。

private static void setupJAASLoginService(Server server, BootstrapProperties config) throws Exception {
    JAASLoginService loginService = new JAASLoginService("ldaploginmodule");
    loginService.setName("WebRealm");
    loginService.setConfiguration(setupLDAPConfiguration(config));
    loginService.start();

    server.addBean(loginService);
}

private static javax.security.auth.login.Configuration setupLDAPConfiguration(BootstrapProperties config) {
  // Basically what I do here is make my own implementation of the Configuration and use it
  // The existing class assumes code to be in a very specific file location.
    return new javax.security.auth.login.Configuration() {
        @Override
        public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
            Map<String, Object> options = new HashMap<String, Object>();

            options.put("authenticationMethod", "simple");
            options.put("bindDn", config.get("ldap.bind.user"));
            options.put("bindPassword", config.get("ldap.bind.password"));
            options.put("contextFactory", "com.sun.jndi.ldap.LdapCtxFactory");
            options.put("debug", "true");
            options.put("forceBindingLogin", "true");
            options.put("hostname", config.get("ldap.host"));
            options.put("port", config.get("ldap.port"));
            options.put("roleBaseDn", config.get("ldap.groups.dn") + "," + config.get("ldap.root.dn"));/**/
            options.put("roleMemberAttribute", "uniqueMember");
            options.put("roleNameAttribute", "cn");
            options.put("roleObjectClass", "groupOfUniqueNames");
            options.put("userBaseDn", config.get("ldap.people.dn") + "," + config.get("ldap.root.dn"));/**/
            options.put("userIdAttribute", "uid");
            options.put("userObjectClass", "caUser");
            options.put("userPasswordAttribute", "userPassword");
            options.put("userRdnAttribute", "cn");

            AppConfigurationEntry cfg = new AppConfigurationEntry("org.eclipse.jetty.jaas.spi.LdapLoginModule", LoginModuleControlFlag.REQUIRED, options);
            return new AppConfigurationEntry[] { cfg };
        }
    };
}

您可能需要更改选项以匹配您自己的 ldap。如果将上述选项与文件进行比较,它们几乎是一对一的映射。

现在设置网络应用程序:

请注意,我从一个文件夹开始这个 class,该文件夹是我的多项目文件夹的根目录,Web 应用程序位于该根目录下的子文件夹中。

另请注意,appname 必须引用文件夹名称。它们所在的应用程序名称和文件夹名称在此设置中相同。

private static void startWebApp(BootstrapProperties config, HandlerCollection hc, String contextRoot, String appName) throws Exception {
    boolean isProd = config.getBoolean("isProduction", false);

  // When running a production server you're probably working from warfiles.
  // In dev you're working from eclipse webapp folders (WebContent/webapp/the place where your index.html resides)
    String pathStr = isProd
            ? "dist/webapps/" + appName + ".war"
            : "webapps/" + appName;

    WebAppContext context = new WebAppContext();
    // This is where you can find the webapp on your server http://example.com{/contextRoot}
    context.setContextPath(contextRoot);
    // Optional, but I found it very useful for debugging
    context.addLifeCycleListener(LIFE_CYCLE_LISTENER);

    // Very important if you want JSTL to work, otherwise you get the error:
    // The absolute uri: [http://java.sun.com/jsp/jstl/core] cannot be resolved in
    // either web.xml or the jar files deployed with this application
    // This was very hard to figure out!
    context.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*/[^/]*jstl.*\.jar$");

    if (isProd) {
        // Again production server refers to warfile, simple basic function for jetty.
        context.setWar(pathStr);

    } else {
        // Otherwise things get a little more complicated
        // For me the app and classes folders are in two separate places.
        // But fortunately Jetty still supports that.
        Path path = Paths.get(pathStr);
        Path basePath = path.toRealPath();

        // These are folders in your eclipse projects 
        Path appFolder = basePath.resolve("webapp"); // WebContent also often used
        Path classesPath = basePath.resolve("bin/main"); 

        if (Files.exists(appFolder)) {
            context.setBaseResource(new PathResource(appFolder));
            LOGGER.log(Level.FINE, " webapp " + appFolder);
        }
        if (Files.exists(classesPath)) {
            context.setExtraClasspath(classesPath.toString());
            LOGGER.log(Level.FINE, " classes " + classesPath);
        }

        // A pure webapp project without classes works fine classesPath wont exist and is thus not added.
    }

    // Add to the handler context.
    hc.addHandler(context);
}