openxava / documentation / Deploy in production as an executable WAR

Since OpenXava 8.0 your applications are Spring Boot applications. This means that the WAR file you generate with Maven is an executable WAR, you can run it directly with Java without needing an external application server. This is an alternative to deploying in Tomcat, simpler and ideal for many production environments.

When to use an executable WAR vs Tomcat

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. Use Tomcat instead if you want to host several applications in the same server, or if you already have a Tomcat installed and want to reuse it.

Build the WAR

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

This generates invoicing.war (or yourapplicationname.war) inside the target folder of your project.

Run the WAR

Make sure you have Java 17 or better installed and JAVA_HOME configured. Then, just run the WAR with Java:

$ java -jar invoicing.war

Your application is already running. Use your browser to access it at http://localhost:8080/invoicing.

Run as a background service

In a production server you usually want to run the application in the background and keep it running after you log out. A simple way is to use nohup in Linux/Mac:

$ nohup java -jar invoicing.war &

For a more robust setup consider using a service manager like systemd or a process manager like Supervisor to manage the lifecycle of your application.

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. You can override the database configuration without modifying the WAR by using Spring Boot externalized configuration.

Place an application.properties file in the same folder where you run the WAR, and it will be picked up automatically:

spring.datasource.url=jdbc:hsqldb:hsql://dbserver:1999
spring.datasource.username=invoicing
spring.datasource.password=sfh4tjdl
spring.datasource.driver-class-name=org.hsqldb.jdbcDriver

Alternatively, you can pass the properties as command-line arguments:

$ java -jar invoicing.war \
  --spring.datasource.url=jdbc:hsqldb:hsql://dbserver:1999 \
  --spring.datasource.username=invoicing \
  --spring.datasource.password=sfh4tjdl

Or use environment variables:

$ export SPRING_DATASOURCE_URL=jdbc:hsqldb:hsql://dbserver:1999
$ export SPRING_DATASOURCE_USERNAME=invoicing
$ export SPRING_DATASOURCE_PASSWORD=sfh4tjdl
$ java -jar invoicing.war

These approaches allow you to use the same WAR against different databases in different environments without rebuilding.