openxava / documentation / Deploy in production with Tomcat

If you're using OpenXava 7.x or older look at the OpenXava 7 instructions

You can deploy your OpenXava applications in any Java application server with support for Jakarta Servlet 6.1 or better. For this guide we're going to use Apache Tomcat to deploy your OpenXava application. Tomcat is the most used and robust Java application server, ideal for production environments. OpenXava applications are Spring Boot applications, so besides running them directly with their embedded Tomcat, you can also build a standard WAR and deploy it in an external Tomcat, exactly as you always did.

When to use Tomcat vs an executable WAR

Since OpenXava 8.0 you have two ways to run your application in production: deploy the WAR in an external Tomcat, as explained in this guide, or run the WAR directly with java -jar as an executable WAR. Use Tomcat if you want to host several applications in the same server, or if you already have a Tomcat installed and want to reuse it. Use the executable WAR if you only have one application, or if you plan to deploy in Docker or Kubernetes, since the executable WAR is a self-contained artifact easy to containerize.

Download and install Tomcat

Download Tomcat 11 from Apache. Uncompress Tomcat in a folder of your choice. Go to the webapps folder and remove all the folders inside: docs, examples, host-manager, manager and ROOT. They are example and management apps that you don't want to have in a production system.

Create and deploy the WAR

In order to deploy your application into Tomcat you have to create a WAR file from your project. Open a terminal, or use the terminal of your IDE, and execute Maven from the root folder of your project:

$ cd invoicing
$ mvn clean package

Afterwards get the file invoicing.war (or yourapplicationname.war) from the target folder of your project and copy it to the webapps folder of your Tomcat. Your application is already deployed.

Run Tomcat

To start your Tomcat, with Windows click on the Start button, type cmd and press ENTER, then:

set JAVA_HOME=C:\Program Files\Java\jdk-17.0.9
cd tomcat-11.0.23\bin
startup

With Linux/Mac:

export JAVA_HOME=/usr/lib/jvm/java-17-openjdk
cd tomcat-11.0.23/bin
./startup.sh

Of course, adjust the paths for your own Tomcat and Java directories.

Congratulations! Your application is already running in production, just use your browser to access to it.

Configure against production database

By default your application goes against the same database you're using in development, the one defined with the spring.datasource.* properties in application.properties of your project. Most times you want to use a different database for production. For this case you should use the classic Tomcat mechanism, adding your datasource to the context.xml found in the conf folder of your Tomcat.

<?xml version="1.0" encoding="UTF-8"?>

<Context>

    <Resource name="jdbc/invoicingDS" auth="Container" type="javax.sql.DataSource"
        maxTotal="20" maxIdle="5" maxWaitMillis="10000"
        username="invoicing" password="sfh4tjdl" driverClassName="org.hsqldb.jdbcDriver"
        url="jdbc:hsqldb:hsql://dbserver:1999"/>

</Context>

We use invoicingDS as datasource name, the same JNDI name declared for the default datasource in persistence.xml of your project.