有没有人在Heroku上成功部署了一个GWT应用程序?

Tra*_*ebb 15 gwt heroku

Heroku最近开始支持Java应用程序.通过文档查看,它似乎与Java Servlet标准类似.有没有人知道在Heroku上成功部署GWT应用程序的实例?如果是这样,有任何限制吗?

Mat*_*ttR 5

是的,我在这里使用Java指令入门进行了成功的部署:http: //devcenter.heroku.com/articles/java

我使用带有appassembler插件方法的Maven项目,但在构建期间添加了gwt-maven-plugin来编译GWT应用程序.

当你推送到heroku时,你看到GWT编译过程正在运行,在一个线程上只有这么慢,但它工作正常.

嵌入式Jetty实例配置为在src/main/resources/static的/ static中提供静态资源,并在构建期间将已编译的GWT应用程序复制到此位置,然后正常引用.nocache.js.

你还想知道什么?

您可以选择,在您的Maven项目中本地构建GWT应用程序的Javascript表示,提交它并从您的应用程序中读取它,或者如我所提到的那样通过gwt-maven-plugin在Heroku中生成它.

通过嵌入式Jetty从jar中的静态位置提供文件的代码在Guice ServletModule中是这样的:

(请参阅下面的其他答案,以获得更简单且更少Guice驱动的方法.)

protected void configureServlets() {
  bind(DefaultServlet.class).in(Singleton.class);
  Map<String, String> initParams = new HashMap<String, String>();
  initParams.put("pathInfoOnly", "true");
  initParams.put("resourceBase", staticResourceBase());
  serve("/static/*").with(DefaultServlet.class, initParams);
}

private String staticResourceBase() {
    try {
        return WebServletModule.class.getResource("/static").toURI().toString();
    }
    catch (URISyntaxException e) {
        e.printStackTrace();
        return "couldn't resolve real path to static/";
    }
}
Run Code Online (Sandbox Code Playgroud)

嵌入式Jetty使用guice-servlet还有一些其他技巧,如果这还不够,请告诉我.


Mat*_*ttR 3

当 GWT 尝试读取其序列化策略时,我对此的第一个回答结果出现了问题。最后我选择了一种更简单的方法,不太基于 Guice。我必须单步执行 Jetty 代码才能理解为什么setBaseResource()要这样做——从 Javadoc 来看这并不是显而易见的。

这是我的服务器类 - 带有 main() 方法的服务器类,您可以根据 Heroku 文档通过应用程序组装器插件将 Heroku 指向该服务器类。

public class MyServer {

public static void main(String[] args) throws Exception {
    if (args.length > 0) {
        new MyServer().start(Integer.valueOf(args[0]));
    }
    else {
        new MyServer().start(Integer.valueOf(System.getenv("PORT")));
    }
}

public void start(int port) throws Exception {

    Server server = new Server(port);
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setBaseResource(createResourceForStatics());
    context.setContextPath("/");
    context.addEventListener(new AppConfig());
    context.addFilter(GuiceFilter.class, "/*", null);
    context.addServlet(DefaultServlet.class, "/");
    server.setHandler(context);

    server.start();
    server.join();
}

private Resource createResourceForStatics() throws MalformedURLException, IOException {
    String staticDir = getClass().getClassLoader().getResource("static/").toExternalForm();
    Resource staticResource = Resource.newResource(staticDir);
    return staticResource;
}
}
Run Code Online (Sandbox Code Playgroud)

AppConfig.java 是一个 GuiceServletContextListener。

然后,您将静态资源放在src/main/resources/static/.