2021 Call for Code Awards: Live from New York, with SNL’s Colin Jost! Learn more

Achieve rapid, iterative cloud-native application development

Introduction

The world of application development has changed at a staggering pace, with new technologies flooding the ecosystem at a rapid pace, and product improvements and new features expected continuously. Thus, to succeed in this fast-paced environment, it is now essential for developers to deliver cloud-native applications quickly and effectively. Utilizing the right tools and technologies can help streamline this process and enable developers to achieve truly rapid, iterative cloud-native application development.

Selecting the right application server runtime can be key in optimizing your application development. There are many different runtimes to choose from, but carefully considering what each offer as well as the architecture and design of your specific applications is essential. There are some awesome open source runtime options as well, including Open Liberty and Quarkus.

If you’re interested in learning more about how to choose the right Java runtime, check out the article “Choose the right Java runtime for the job” (IBM Developer, November 2020). Or, if you’re interested in Open Liberty specifically, check out the article “6 reasons why Open Liberty is an ideal choice for developing and deploying microservices” (IBM Developer, November 2020).

One of the most interesting features of both Open Liberty and Quarkus, when it comes to rapid and iterative application development, is their ability to provide developers with a form of “hot reloading.” This feature is referred to as development (dev) mode. Usually, when developing in Java, it is necessary to launch a compilation and restart the application when any changes are made to ensure that these changes are taken into account. However, running in dev mode and utilizing the hot reload capability enables the application code to be automatically compiled and deployed to a running server, making it easy to iterate on your changes. So, developers can make code and configuration changes, and have those take immediate effect on a local running server — even a server running in a local container. This removes the need for a full rebuild and redeploy, greatly reducing the time taken to develop and test updates. You can run tests on demand or even automatically so that you can get immediate feedback on your changes. You can also attach a debugger at any time to debug your running application, and with Open Liberty IDE support, you can even do this directly from your integrated development environment (IDE) or text editor. This all helps to enable a more rapid and iterative process for application development.

When developing an application that will eventually be deployed to production, you can avoid potential issues by ensuring that your development and production environments are as similar as possible, as laid out in the 12-factor application methodology. For more information on building a 12-factor application, take a look at this blog “Twelve-factor app best practices in microservices with MicroProfile and Kubernetes” (Open Liberty, September 2019). For cloud-native applications, part of this issue is addressed by using containers where your environment can be codified to provide consistency between development and production. Dev mode’s container support helps to ensure this dev-prod parity by enabling developers to develop and test in an environment that’s closer to production. With container support for dev mode, you can use the same Dockerfile for both development and production. You use the same base image and customizations, and specify the same configuration files in your Dockerfile that you use for your application in production.

Get started using dev mode

You can enable dev mode to work with either Maven or Gradle build automation tools. Dev mode is available as:

Maven

To enable the Liberty Maven Plug-in in your project, specify the Liberty Maven Plug-in with version 3.1 or later in your project’s pom.xml file, as shown in the following code example:

<plugin>

<groupId>io.openliberty.tools</groupId>

<artifactId>liberty-maven-plugin</artifactId>

<version>3.1</version>

</plugin>

Gradle

Within your Gradle build script, you need to set up the classpath to include the Liberty Gradle Plug-in. You also need to define the Maven Central repository to find the plug-in and its dependencies.

If you are using a snapshot version of the plug-in, make sure to define the Sonatype Nexus Snapshots repository in addition to the Maven Central repository.

Your build script should look like the following:

buildscript {
    repositories {
        mavenCentral()
        maven {
            name ='Sonatype Nexus Snapshots'
            url ='https://oss.sonatype.org/content/repositories/snapshots/'
        }
    }
    dependencies {
        classpath 'io.openliberty.tools:liberty-gradle-plugin:3.0-SNAPSHOT'
    }
}

To use the Liberty Gradle Plug-in, include the following code in your build script:

apply plugin: 'liberty'

Start dev mode

Start dev mode by running the following command in your project:

  • Maven:

     mvn liberty:dev
    
  • Gradle:

     gradle libertyDev
    

Automatically recompile and deploy to the running server

Once dev mode is started, source code changes are automatically compiled and deployed to the running server. You can add new dependencies in your pom.xml file to be used for compilation. Resources and server configuration changes are also picked up. If any features are added to the server.xml file, they are installed automatically.

Dev mode automatically detects the following changes to your application source:

  • Java source file and test file changes
  • Resource file changes
  • Configuration directory and configuration file changes
  • New dependency additions to your pom.xml file for Maven users or build.gradle file for Gradle users
  • New feature additions in the Open Liberty server configuration

You can configure how dev mode handles changes to your code by specifying parameters when you start dev mode. For more information about configuration parameters, see the:

Run tests automatically and on demand

To run tests on demand, press Enter in the terminal where dev mode is running. This invokes the unit tests and integration tests that are configured for your project.

You can get immediate feedback on your changes by configuring dev mode to run hot tests. Hot tests are unit or integration tests that run automatically whenever you start dev mode or make a code change. To configure hot testing, specify the hot test parameter when you start dev mode, as shown in the following examples:

  • Maven:

     mvn liberty:dev -DhotTests
    
  • Gradle:

     gradle libertyDev --hotTests
    

Attach a debugger

At any point during dev mode, you can attach a Java debugger to debug your application. By default, the debug port is 7777. But another port can be used and shown in the console output, if the default port is not available.

Stop dev mode

You can quit dev mode at any time by pressing CTRL+C in the terminal, or type q and press Enter.

Start dev mode with container support

For Maven projects, specify the Liberty Maven plug-in with version 3.3 in your project pom.xml file, as shown in the following code example:

<plugin>
    <groupId>io.openliberty.tools</groupId>
    <artifactId>liberty-maven-plugin</artifactId>
    <version>3.3</version>
</plugin>

For Gradle projects, specify the Liberty Gradle plug-in with version 3.1 in your project build.gradle file, as shown in the following code example:

apply plugin: 'liberty'

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'io.openliberty.tools:liberty-gradle-plugin:3.1'
    }
}

Create a Dockerfile in your project root directory that includes COPY commands for your application and configuration files. For more information, see the Open Liberty Docker image documentation.

To start dev mode with container support, run one of following commands in your project:

  • Maven:

     mvn liberty:devc
    
  • Gradle:

     gradle libertyDevc
    

These commands compile your application, build the development image, and run the server in the container. You can edit your source code or configuration files while dev mode is running.

Dev mode IDE extensions

To make it even easier to run dev mode, there are also several available IDE extensions. These extensions enable developers to start Open Liberty in development mode, make changes to the application while the server is up, run tests and view results, and even debug the application without leaving the editor.

Next steps