Deploy Java Spring Projects with DeployHQ

This guide uses DeployHQ to automate deployments of your Java Spring project.

Prerequisites:

  • A hosted server with Java and Maven installed.

Setting Up DeployHQ

  1. Create a DeployHQ project: Sign up and navigate to Projects > New Project. Follow the wizard to connect your project repository.
  2. Create a DeployHQ user: Enhance security by creating a dedicated user for deployments:
sudo adduser deployhq
sudo usermod -a -G some-app-group deployhq

Replace some-app-group with your application group (typically tomcat or similar).

  1. Configure a server: In DeployHQ, go to Servers > New Server.
    • Server name: Choose a recognizable name.
    • Protocol: Select SSH.
    • Hostname: Enter your server's IP address.
    • Username: Enter deployhq.
    • Authentication: Use SSH key. Run the following commands on your server:
su - deployhq
mkdir ~/.ssh
chmod 700 ~/.ssh
nano ~/.ssh/authorized_keys

Paste DeployHQ's public key, then press Ctrl + X, y, and Enter to save.

chmod 600 ~/.ssh/authorized_keys
  • Deployment Path: Set this to your application directory (e.g., /var/www/myapp).
  • Zero-downtime deployments: Enable for seamless transitions during deployment.
    • This creates a path like /var/www/myapp/current for application files. Update your web server configuration accordingly.
  • Environment: Set to production.
  • Automatic deployments: Enable for automated deployments on pushes.
  1. Add a config file (Optional): DeployHQ allows uploading configuration files without storing them in the repository. This is useful for sensitive files like .env.
-   Go to **Config Files** and click **New Config File**.
-   Paste the contents of your `.env.example` file into the config file content field.
  1. Set up SSH commands: In DeployHQ, navigate to SSH Commands and click New SSH Command. Create the following commands:
  • Install Dependencies:
cd %path% && mvn clean install
  • Build Application:
cd %path% && mvn package
  • Copy Artifact: (Assuming a WAR file is generated)
cp %path%/target/*.war /var/lib/tomcat/webapps/yourapp.war

Replace /var/lib/tomcat/webapps/yourapp.war with your actual deployment path. - Restart Tomcat: (assuming a systemd service)

sudo systemctl restart tomcat

Adjust the command based on your specific service management system.

Deployment:

Click Deploy Project in the DeployHQ header. Select the server and desired revisions. Once confirmed, click Deploy to initiate the deployment process.

Additional Resources:

If you’ve got any other projects you’d like to automate deployments for, we’ve written lots of guides for deploying the most popular application frameworks and content management systems.