5.3.2. WAR deployment to Jetty

Below is an example of deployment of the WAR files to the Jetty web server.

We are going to use the following directory structure:

  • C:\work\jetty-home\ - Jetty distribution folder;

  • C:\work\jetty-base\ - Jetty configuration directory, used to store Jetty configuration files, additional libraries and web applications.

  • C:\work\app_home\ - CUBA application home directory.

    1. Use the CUBA project tree > Project > Deployment > WAR Settings dialog in Studio or just manually add the buildWar task to the end of build.gradle:

      task buildWar(type: CubaWarBuilding) {
          appHome = '${app.home}'
          appProperties = ['cuba.automaticDatabaseUpdate': 'true']
          singleWar = false
      }

      Please note that we are building two separate WAR files for Middleware and Web Client blocks here.

    2. Start build process by running buildWar from the command line (provided that you have created the Gradle wrapper beforehand):

      gradlew buildWar

      As a result, the app-core.war and app.war files will be created in the build\distributions\war project subdirectory.

    3. Create an application home directory, for example, c:\work\app_home.

    4. Copy the logback.xml file from the development Tomcat (deploy/tomcat/conf project sub-folder) to the application home and edit the logDir property in this file:

      <property name="logDir" value="${app.home}/logs"/>
    5. Download and install Jetty to a local directory, for example, c:\work\jetty-home. This example has been tested on jetty-distribution-9.4.22.v20191022.zip.

    6. Create the c:\work\jetty-base directory, open the command prompt in it and execute:

      java -jar c:\work\jetty-home\start.jar --add-to-start=http,jndi,deploy,plus,ext,resources
    7. Create the c:\work\jetty-base\app-jetty.xml file defining the database connection pool. Contents of the file for a PostgreSQL database should be based on the following template:

      <?xml version="1.0"?>
      <!DOCTYPE Configure PUBLIC "-" "http://www.eclipse.org/jetty/configure_9_0.dtd">
      <Configure id="wac" class="org.eclipse.jetty.webapp.WebAppContext">
          <New id="CubaDS" class="org.eclipse.jetty.plus.jndi.Resource">
              <Arg/>
              <Arg>jdbc/CubaDS</Arg>
              <Arg>
                  <New class="org.apache.commons.dbcp2.BasicDataSource">
                      <Set name="driverClassName">org.postgresql.Driver</Set>
                      <Set name="url">jdbc:postgresql://localhost/db_name</Set>
                      <Set name="username">username</Set>
                      <Set name="password">password</Set>
                      <Set name="maxIdle">2</Set>
                      <Set name="maxTotal">20</Set>
                      <Set name="maxWaitMillis">5000</Set>
                  </New>
              </Arg>
          </New>
      </Configure>

      The app-jetty.xml file for MS SQL databases should correspond to the following template:

      <?xml version="1.0"?>
      <!DOCTYPE Configure PUBLIC "-" "http://www.eclipse.org/jetty/configure_9_0.dtd">
      <Configure id="wac" class="org.eclipse.jetty.webapp.WebAppContext">
          <New id="CubaDS" class="org.eclipse.jetty.plus.jndi.Resource">
              <Arg/>
              <Arg>jdbc/CubaDS</Arg>
              <Arg>
                  <New class="org.apache.commons.dbcp2.BasicDataSource">
                      <Set name="driverClassName">com.microsoft.sqlserver.jdbc.SQLServerDriver</Set>
                      <Set name="url">jdbc:sqlserver://server_name;databaseName=db_name</Set>
                      <Set name="username">username</Set>
                      <Set name="password">password</Set>
                      <Set name="maxIdle">2</Set>
                      <Set name="maxTotal">20</Set>
                      <Set name="maxWaitMillis">5000</Set>
                  </New>
              </Arg>
          </New>
      </Configure>
    8. Download the following JARs required for the DB connection pool and put them to the c:\work\jetty-base\lib\ext folder. Two of these files can be found in the deploy\tomcat\shared\lib project sub-folder:

      commons-pool2-2.6.2.jar
      commons-dbcp2-2.7.0.jar
      commons-logging-1.2.jar
    9. Copy the start.ini file from the Jetty distribution to the c:\work\jetty-base folder. Add the following text to the beginning of the c:\work\jetty-base\start.ini file:

      --exec
      -Xdebug
      -agentlib:jdwp=transport=dt_socket,address=8787,server=y,suspend=n
      -Dapp.home=c:\work\app_home
      -Dlogback.configurationFile=c:\work\app_home\logback.xml
      app-jetty.xml
    10. Copy the JDBC driver for your database to the c:\work\jetty-base\lib\ext directory. You can take the driver file from the deploy\tomcat\lib project directory. In case of PostgreSQL database, it is postgresql-42.2.5.jar.

    11. Copy WAR files to the c:\work\jetty-base\webapps directory.

    12. Open the command prompt in the c:\work\jetty-base directory and run:

      java -jar c:\work\jetty-home\start.jar
    13. Open http://localhost:8080/app in your web browser.