简体   繁体   English

使用jetty从主类部署war文件

[英]Deploying war file from main class with jetty

I do want to run a main class which deploys a jetty server with the content of my vaadin application.我确实想运行一个主类,它使用我的 vaadin 应用程序的内容部署一个码头服务器。

final Server server = new Server();
     final Connector connector = new ServerConnector( server );
     (( AbstractNetworkConnector ) connector).setPort( 3131 );
     server.setConnectors( new Connector[] { connector } ); //
     final WebAppContext webappcontext = new WebAppContext();
     final File warFile = new File( "target/avx-gcms-1.0.war" );
     webappcontext.setClassLoader( Thread.currentThread().getContextClassLoader() );
     webappcontext.setContextPath( "/" );
     webappcontext.setWar( warFile.getAbsolutePath() );
     server.setHandler( webappcontext );
     server.start();
     System.out.println( "Jetty started, please go to http://localhost:" + 3131 + "/" );

Here is my code for the main method but when I do run this, it works without an error but I do get this in m localhost.这是我的主要方法的代码,但是当我运行它时,它可以正常工作而没有错误,但我确实在 m localhost 中得到了它。

This is my workspace这是我的工作区

I have seen some related questions to this yet they were not very helpful.我已经看到了一些与此相关的问题,但它们并不是很有帮助。 Thank you if you help in advance.如果您提前提供帮助,谢谢。

You might want to do it the other way around: instead of war file, make the application as runnable jar directly.您可能希望以相反的方式执行此操作:将应用程序直接设为可运行的 jar,而不是 war 文件。

You need to have this main class to the project along with your Vaadin UI class, and wire it up directly to Jetty:您需要将此主类与您的 Vaadin UI 类一起添加到项目中,并将其直接连接到 Jetty:

public static void main(String[] args) {
    Server server = new Server(3131);

    ServletContextHandler contextHandler
            = new ServletContextHandler(ServletContextHandler.SESSIONS);
    contextHandler.setContextPath("/");

    ServletHolder sh = new ServletHolder(new VaadinServlet());
    contextHandler.addServlet(sh, "/*");
    contextHandler.setInitParameter("ui", HelloWorldUI.class.getCanonicalName());
    server.setHandler(contextHandler);

    try {
        server.start();
        server.join();

    } catch (Exception ex) {
        Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
    }
}

Then configure maven-shade-plugin and exec-maven-plugin in your pom.xml to create a runnable jar file like in here .然后在pom.xml配置maven-shade-pluginexec-maven-plugin以创建一个可运行的 jar 文件, 如下所示

Check out that project for full example, but note that the setup is bit different with externalised static resources (widgetset and css), but you can skip that configuration in your case.查看该项目的完整示例,但请注意,设置与外部化静态资源(widgetset 和 css)略有不同,但您可以在您的情况下跳过该配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM