back to all blogsSee all blog posts

Automatically generate features in your Open Liberty server configuration (technology preview)

image of author image of author
Kathryn Kodama and Trevor Crawford on Feb 8, 2022

In this post, we introduce a technology preview for automatically generating a list of features as part of your project’s Open Liberty server configuration. When you run Open liberty in dev mode, features are now automatically generated in your server configuration as you develop your cloud-native Java application.

Dev mode monitors the class files in your application and detects when they use new Open Liberty feature APIs. If dev mode detects features that are not already specified in your server configuration, a new generated-features.xml file is created in your configDropins/overrides directory. This function simplifies your developer workflow because you no longer need to manually specify these features. We recommend that you commit this new generated-features.xml file to your version control. User-specified features always take precedence over automatically generated features. Automatically generated features will not replace or conflict with features that you have already manually specified in your server.xml.

This function is enabled by default in the 3.6 of the Liberty Maven Plugin and the 3.4 of the Liberty Gradle Plugin. We invite you to try it out and let us know your thoughts so that we can make it even more useful for your development scenarios.

Dev mode with automatic generation of features

For Maven projects, specify the Liberty Maven Plugin with version 3.6 and its Sonatype repository in your pom.xml file:

    <repositories>
        <repository>
            <id>sonatype</id>
            <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        </repository>
    </repositories>

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

For Gradle projects, specify the Liberty Gradle Plugin with version 3.4 and its Sonatype repository in your build.gradle file:

apply plugin: 'liberty'

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
        maven {
            url "https://plugins.gradle.org/m2"
            url "https://oss.sonatype.org/content/repositories/snapshots"
        }
    }
    dependencies {
        classpath 'io.openliberty.tools:liberty-gradle-plugin:3.4'
    }
}

Start dev mode by running the following Maven goal or Gradle task. Automatic feature generation is enabled by default in the previously mentioned Maven and Gradle snapshot releases for this technology preview.

  • Maven: mvn liberty:dev

  • Gradle: gradle libertyDev

Open Liberty dev mode startup with automatic generation of features on

As you make code changes to your application, dev mode monitors API use in your class files and automatically generates and installs new Open Liberty features as needed.

Open Liberty dev mode generating features after code change

The feature list is generated in a new configDropins/overrides/generated-features.xml file in your project server configuration directory.

Example generated-features.xml
<?xml version="1.0" encoding="UTF-8"?>
<server>
    <!--This file was generated by the Liberty Maven Plugin and will be overwritten on subsequent runs of the liberty:generate-features goal.
 It is recommended that you do not edit this file and that you commit this file to your version control.-->
    <featureManager>
        <!--The following features were generated based on API usage detected in your application-->
        <feature>cdi-2.0</feature>
        <feature>mpHealth-3.0</feature>
        <feature>mpMetrics-3.0</feature>
        <feature>jaxrs-2.1</feature>
    </featureManager>
</server>

As you make changes to your application, dev mode scans for API use only in class files that you have just modified. However, you might run into a scenario where a feature has been generated that is no longer used in any of your classes. You can have dev mode re-scan all class files in your application and generate an optimized list of features by typing 'o' and pressing Enter in the dev mode terminal.

Open Liberty dev mode optimizing features generated

Disabling automatic generation of features in dev mode

To disable automatic feature generation, you can set the generateFeatures parameter to false by running the following Maven goal or Gradle task.

  • Maven: mvn liberty:dev -DgenerateFeatures=false

  • Gradle: gradle libertyDev --generateFeatures=false

You can toggle automatic feature generation on and off by typing 'g' and pressing Enter in the dev mode terminal.

Open Liberty dev mode toggling features generation

Dev mode help menu

This latest snapshot release also includes a new help menu view in dev mode. By typing 'h' and pressing Enter in the dev mode terminal, you can see a list of hotkeys and actions that you can perform in dev mode.

Open Liberty dev mode help menu

Automatically generating features outside of dev mode

To generate a server configuration feature list for your application outside of dev mode, compile your project and run the new generate features stand-alone goal or task. You must run the "installFeatures" goal or task afterwards to install the features that are generated.

  • Maven: mvn compile liberty:generate-features

  • Gradle: mvn compileJava generateFeatures

Open Liberty Maven Plugin generate-features standalone goal

Additional resources

To try out automatic generation of features on a demo project, see the generate-features branch of the demo-devmode project.

For more information, see the official documentation for the liberty:generate-features goal of the Liberty Maven Plugin or the generateFeatures task of the Liberty Gradle Plugin.

We welcome your feedback on this technology preview. You can post in the Open Liberty developer experience channel on Gitter or open issues directly in the Liberty Maven Plugin or Liberty Gradle Plugin GitHub repositories.

See the technology preview in action

Please join us for a WebSphere & Liberty Customer Advisory Board (CAB) session Thursday, February 10th where the development team will demonstrate the new capabilities of this technology preview, answer your questions, and listen to your feedback. If you are interested in this CAB session, or any future CAB sessions, it is best to join the WebSphere & Liberty CAB to ensure that you receive reminders for sessions you are interested in plus an email with the session recording and charts.

Tags