How to customize WildFly applications on Openshift

In this tutorial Java EE example application on Openshift we have deployed a sample Java EE application using a database on Openshift. We will explore now other possibilities such as running CLI commands as part of the Vuild process and include a set of custom modules and finally we will learn how to add artifacts directly into the deployments folder of the application server.

Injecting CLI commands when Building WildFly Applications

If you need a fine grained control over WildFly configuration on OpenShift we recommend using the S2I WildFly server customization hooks (https://github.com/wildfly/wildfly-s2i) which includes:

  • Wildfly configuration files from the `<application source>/<cfg|configuration>` are copied into the wildfly configuration directory.
  • Pre-built war files from the `<application source>/deployments` are moved into the wildfly deployment directory.
  • Wildfly modules from the `<application source>/modules` are copied into the wildfly modules directory.
  • Execute WildFly CLI scripts by using the `install.sh` script.

In the first example, we will show how to execute a CLI script as part of the build process. This example (available here https://github.com/fmarchioni/mastertheboss/tree/master/openshift/config) uses the following project structure:

$ tree -a

├── extensions
│   ├── configuration.cli
│   └── install.sh
├── pom.xml
├── README.md
├── .s2i
│   └── environment
└── src
    └── main
        └── webapp
            └── index.jsp
  • The `extensions/configuration.cli` contains the CLI commands to be executed.
  • The `extensions/install.sh` launches the CLI using the utility function `run_cli_script` available in the `/usr/local/s2i/install-common.sh` script of WildFly S2I Builder Image:
#!/usr/bin/env bash
injected_dir=$1
source /usr/local/s2i/install-common.sh

S2I_CLI_SCRIPT="${injected_dir}/configuration.cli"

run_cli_script "${S2I_CLI_SCRIPT}"

Finally, within the `.s2i/environment` folder we can set environment variables such as the location of extensions dir.

You can build the application and expose the service as follow:

$ oc new-app --as-deployment-config wildfly~https://github.com/fmarchioni/mastertheboss.git --context-dir=openshift/config --name demo-config

$ oc expose service/wildfly-config

By requesting the Route Address, you will see the System Property which has been injected with the CLI:

$ curl wildfly-config-wildfly.apps-crc.testing

 <html>
  <body bgcolor=white>

  <table border="0" cellpadding="10">
    <tr>
      <td>
         <h1>WildFly - sample OpenShift configuration</h1>
      </td>
    </tr>
  </table>
  <br />
  <p>Prints System Property from configuration: HelloWorld</p>
  </body>
</html> 

Adding custom modules and deployments to WildFly on Openshift

Our second application, available on github at https://github.com/fmarchioni/mastertheboss/tree/master/openshift/module, will show how to add external modules and deployments during the Source to Image process.

How to customize WildFly applications on Openshift

As you can see from the above picture, you can use the following folders:

  • modules: Place here modules just like you would do in a bare-metal installation of WildFly. They will be automatically uploaded on WildFly
  • deployments: Place here artifacts that are to be deployed on the application server.
├── modules
│   └── com
│       └── itext
│           └── main
│               ├── itext-5.0.5.jar
│               └── module.xml
├── pom.xml
├── README.md
└── src
    └── main
        ├── java
        │   └── com
        │       └── mastertheboss
        │           └── CreatePDFExample.java
        └── webapp
            ├── index.jsp
            └── WEB-INF
                ├── jboss-deployment-structure.xml
                └── web.xml

The module.xml loads the itext JAR file and uploads it into the application server.

<module xmlns="urn:jboss:module:1.1" name="com.itext">

    <properties>
        <property name="jboss.api" value="private"/>
    </properties>

      <resources>
        <resource-root path="itext-5.0.5.jar"/> 
    </resources>

</module>

In order to link the library with the application server add a jboss-deployment-structure.xml file:

<jboss-deployment-structure>
  <deployment>
    <dependencies>
      <module name="com.itext" />
    </dependencies>
    </deployment>
</jboss-deployment-structure>

To build the application on Openshift specify the root folder as Git Repository URL: https://github.com/fmarchioni/mastertheboss

Next, specify the subfolder “openshift/modules” as path:

How to customize WildFly applications on Openshift

Here is the application in action:

How to customize WildFly applications on Openshift

That’s all. You can check the rest of the source code on Github: https://github.com/fmarchioni/mastertheboss/tree/master/openshift/module