vego
vego

Reputation: 1049

What is the general way to deploy jetty application in production?

I just take over a project which is a java servlet application, I just figured out the way running the application is mvn jetty:run by using history. The previous only developer just quit suddenly, and there's no document. I have to create production server and deploy the application.

I have no previous Java experiences, so have not idea how to do it. Should I install nginx or apache and run the application like PHP? Or I just do something like nodejs ?

Upvotes: 0

Views: 1743

Answers (1)

Joakim Erdfelt
Joakim Erdfelt

Reputation: 49462

Jetty is a Java Servlet container/server (with many more features).

There's no need for another server.

You have a few choices on how to start Jetty.

  1. Standalone server using the jetty-home (or the older jetty-distribution) tarballs.

    In this mode you unpack the jetty-home tarball (this will become the ${jetty.home} directory), create a new directory for your configuration / deployment (this will become the ${jetty.base} directory), and then run Jetty against this ${jetty.base} directory.

    There are many many examples online of how to do this.

    From the official Jetty documentation at https://www.eclipse.org/jetty/documentation/current/

    To various examples here on stackoverflow.

    # Grab the tarball
    [~]$ curl -O https://repo1.maven.org/maven2/org/eclipse/jetty/jetty-home/9.4.30.v20200611/jetty-home-9.4.30.v20200611.tar.gz
    
    # Unpack the tarball    
    [~]$ tar -zxvf jetty-home-9.4.30.v20200611.tar.gz
    
    # Make a {jetty.base} directory to house your configuration
    [~]$ mkdir myappbase
    [~]$ cd myappbase
    
    # Since this is a new {jetty.base}, lets initialize it with a 
    # few common modules
    [myappbase]$ java -jar ../jetty-home-9.4.30.v20200611/start.jar \
       --add-to-start=http,deploy
    INFO  : webapp          transitively enabled, ini template available with --add-to-start=webapp
    INFO  : server          transitively enabled, ini template available with --add-to-start=server
    INFO  : security        transitively enabled
    INFO  : servlet         transitively enabled
    INFO  : http            initialized in ${jetty.base}/start.ini
     ...(snip)...
    INFO  : deploy          initialized in ${jetty.base}/start.ini
    MKDIR : ${jetty.base}/webapps
    INFO  : Base directory was modified
    
    # Lets see what we have now
    [myappbase]$ ls -F
    start.ini  webapps/
    
    # Copy your webapp into place
    [myappbase]$ cp ~/Projects/mywebapp.war webapps/
    
    # See this Jetty Configuration
    [myappbase]$ java -jar ../jetty-home-9.4.30.v20200611/start.jar --list-config
    
    # Run Jetty
    [myappbase]$ java -jar ../jetty-home-9.4.30.v20200611/start.jar
    
  2. Embedded Jetty

    This means you have initialized everything entirely in code, from the Server object, to the ServerConnector, to the WebAppContext (or the ServletContextHandler) and are issuing a Server.start(); to make the Server execute.

    This is probably the more common usage of Jetty, we find far more users on this approach then the standalone approach.

  3. For development using the jetty-maven-plugin

    This is what you have discovered already.

    In a maven project with <packaging>war</packaging> you can execute the various jetty-maven-plugin goals to do various things with that maven project during development time.

    NOTE: This mode is not suitable for production!

    jetty:run is the jetty-maven-plugin and the run goal.

    See: https://www.eclipse.org/jetty/documentation/current/jetty-maven-plugin.html

  4. Alternate environments where Jetty is embedded.

    This mode is seeing increasing use, and if your application is using libraries like Spark or SpringBoot then you are essentially using Jetty under the covers, and have no need for a separate server installation / configuration, it's all done within the API/SDK of that library.

Upvotes: 4

Related Questions