Effortless Jakarta EE: Deploying Apps Directly from Your Java main() Method With Payara Micro

Photo of Luqman Saeed by Luqman Saeed

Jakarta EE offers a reliable foundation for enterprise-scale Java applications. Sometimes, however, the traditional deployment model can add a layer of complexity. Payara Micro addresses this challenge by providing a streamlined approach for managing your Jakarta EE deployments.

Jakarta EE Resources

Why Embed Payara Micro?

Let's first explore why you would want to embed Payara Micro directly into your application:

  • Simplified Deployment:  With Payara Micro, your Java main() method becomes the control centre for deployment and management.
  • Self-Contained Applications:  Create truly portable applications by packaging Payara Micro as a dependency.  This produces a single executable JAR encompassing both your application and the entire Jakarta EE runtime, making distribution and execution easier and portable.
  • Customizable Environments:  Gain the freedom to fine-tune your Jakarta EE environment. Selectively include the components your application truly needs and optimise configurations, leading to smaller, more efficient deployments.
  • Easing into Jakarta EE (especially for Spring Developers):  Payara Micro provides a familiar embedded server experience for developers coming from a Spring Boot background. This bridges the gap, facilitating a smoother transition to the Jakarta EE world.
  • Robust Testing: Integrate Payara Micro into your testing suites. This allows you to run comprehensive tests that mimic a production-like Jakarta EE environment, ensuring your applications perform as intended before deployment.

The How: Setting Up Payara Micro

Adding Payara Micro to your project is straightforward. Let's assume you're using a build system like Maven or Gradle.

Include the following dependency in your project's pom.xml (for Maven) or build.gradle (for Gradle).

<dependency>

    <groupId>fish.payara.extras</groupId>

    <artifactId>payara-micro</artifactId>

    <version>6.2024.2</version>

</dependency>

 

Payara Micro Deployment Options

Deployment During Bootstrap

You can package your application (WAR file) alongside the Payara Micro libraries. With a few lines of code, the deployment happens seamlessly as part of the application startup:

 

import fish.payara.micro.PayaraMicro;

import fish.payara.micro.BootstrapException;




public class MyApplication {

    public static void main(String[] args) throws BootstrapException {

        PayaraMicro.getInstance()

                   .addDeployment("/path/to/my-application.war")

                   .bootstrap();

    }

}

 

Deployment After Bootstrap

Perhaps you want more flexibility to deploy applications after your instance is running. You can achieve that with Payara Micro as follows.

 

import fish.payara.micro.PayaraMicro;

import fish.payara.micro.BootstrapException;

import fish.payara.micro.PayaraMicroRuntime;




public class MyApplication {

    public static void main(String[] args) throws BootstrapException {

        PayaraMicroRuntime runtime = PayaraMicro.bootstrap();




        // ...later, when you're ready to deploy...

        runtime.deploy("my-application", new FileInputStream("/path/to/my-application.war"));  

    }

}

 

Remote Deployment with asadmin Commands

Use the run method for targeted or cluster-wide deployment of the asadmin command on multiple Payara Micro instances. This method offers two variants:



Subset Deployment: Run the command on chosen instances.
Cluster-wide Deployment: Run the command on all instances.
PayaraMicroRuntime runtime = PayaraMicro.getInstance().setHttpAutoBind(true).bootstrap();

runtime.run("deploy", "/path/to/my-application.war"); 



 

Maven Integration

Payara Micro also plays nicely with Maven repositories.  Deploy applications directly from Maven coordinates by simply specifying the group ID, artifact ID, and version as follows.

 

PayaraMicro.getInstance()

           .addDeployFromGAV("com.example,my-application,1.0.0")

           .bootstrap();

For broader artefact searches, Payara Micro allows you to specify additional Maven repositories. Use the addRepoUrl() method for this customization. For instance;

 PayaraMicro.getInstance()

                  .addRepoUrl("https://nexus.payara.fish/repository/payara-artifacts")

                  .addDeployFromGAV("fish.payara.testing,clusterjsp,1.1")

                  .bootStrap();

Conclusion

Traditional Jakarta EE deployment methods can sometimes feel overly complex. Payara Micro offers a simplified solution by embedding the Jakarta EE runtime directly within your Java applications. This approach provides several key benefits:

  • Ease of Deployment: Manage deployments seamlessly from your Java main() method, streamlining your workflow.
  • Portable Applications: Create self-sufficient executable JARs, simplifying distribution and ensuring consistent execution in diverse environments.
  • Tailored Environments: Optimise your Jakarta EE deployment by including only the essential components and fine-tuning configurations.
  • Smooth Jakarta EE Adoption: Payara Micro's embedded server model offers a familiar transition path, especially for developers with experience in Spring Boot.
  • Robust Testing: Integrate Payara Micro into your test suites to ensure your applications perform flawlessly in a true Jakarta EE setting.

Ready to transform your Jakarta EE development experience? Download Payara Micro today and discover the power of simplified deployments, flexibility, and control within your Java codebase. Simplify and accelerate your enterprise projects with Payara Micro.

Chronicles of Java’s Enterprise Evolution  E-BOOK DOWNLOAD 

Comments