How to use conditional statements in WildFly CLI

WildFly Command Line Interface is not a real programming language with complex structures. Nevertheless it’s possible to execute conditional logic in it and this short tutorial will show how to do it.

WildFly Command Line Interface contains the “if” keywords which can be used to build conditional logic.

here is the synopsys of the command:

if (condition_expression) of (command_line)

The if statement includes a command that is executed at the beginning of the if-else control flow. After the response of the command_line is received from the controller, it is evaluated using the condition_expression.
The condition_expression consists of a node path, equals sign (‘==’) and a specific value. If the target value designated with the node path equals to the specified value, then the condition is met and the if block (which is a batch) is executed.

If the condition wasn’t met then the else block, if present, would be executed.

For example, here is how you can execute a conditional deployment- e.g. to check if the application myproject.war is not already deployed:

if (outcome != success) of /deployment=myproject.war:read-resource   
     deploy myproject.war   
end-if

Another example, slightly more complex, will check if the com.mysql module has been already installed. The conditional execution will either install it (if not installed already) or just print a message (saying that it’s already installed):

if (outcome != success) of /core-service=module-loading/:list-resource-loader-paths(module=com.mysql)   
    module add --name=com.mysql --resources=mysql-connector-java-5.1.31-bin.jar --dependencies=javax.api,javax.transaction.api
else
    echo module com.mysql already installed
end-if

Finally, you can check also the return value of a particular node and execute conditional logic based on the value of it:

if (result.value==true) of /system-property=test:read-resource
        /system-property=test:write-attribute(name=value,value=false)
else
        /system-property=test:write-attribute(name=value,value=true)
Found the article helpful? if so please follow us on Socials