I tested several variations of adding a system property in Wildfly (version 8.2.1) standalone.xml via the Wildfly Maven Plugin. Basically, it adds a system property if it does not exists, and changes its value if it does. Ideally, I want a CLI script in batch mode, with nested if-else. However, the issues are:
Here's the plugin section in my pom.xml
<plugin>
    <groupId>org.wildfly.plugins</groupId>
    <artifactId>wildfly-maven-plugin</artifactId>
    <version>1.0.2.Final</version>
    <executions>
        <execution>
            <id>addConfig</id>
            <phase>install</phase>
            <goals><goal>execute-commands</goal></goals>
            <configuration>
                <execute-commands>
                    <!-- <batch>true</batch>  Issue #1. Not working in batch mode -->
                    <batch>false</batch> <!-- This works -->
                    <commands>
                        <command>if (outcome != success) of /system-property=app.env:read-resource</command>
                        <command>/system-property=app.env:add(value=local)</command>
                        <command>else</command>
                        <command>/system-property=app.env:remove</command>
                        <command>/system-property=app.env:add(value=local)</command>
                        <command>end-if</command>
                    </commands>
                    <!--  Issue #2. Nested if-else not working, even in non-batch mode -->
                    <!--
                    <batch>false</batch>
                    <commands>
                        <command>if (outcome != success) of /system-property=app.env:read-resource</command>
                            <command>/system-property=app.env:add(value=local)</command>
                        <command>else</command>
                        <command>if (result.value == qa) of /system-property=app.env:read-resource</command>
                            <command>/system-property=app.env:remove</command>
                            <command>/system-property=app.env:add(value=local)</command>
                        <command>else</command>
                            <command>/system-property=app.env:remove</command>
                            <command>/system-property=app.env:add(value=qa)</command>
                        <command>end-if</command>
                        <command>end-if</command>
                    </commands>
                    -->
                    <!--  Issue #3. Batch and nested if-else not working in CLI script. -->
                    <!--
                    <scripts>
                        <script>target/classes/scripts/add-config.cli</script>
                    </scripts>
                    -->
                </execute-commands>
            </configuration>    
        </execution>            
    </executions>
</plugin>   
Here's the exception if in batch mode:
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.wildfly.plugins:wildfly-maven-plugin:1.0.2.Final:execute-commands (addConfig)
 on project jboss-config: Execution addConfig of goal org.wildfly.plugins:wildfly-maven-plugin:1.0.2.Final:execute-commands failed: Command 'if (outcome != success) of /system-property=app.env:read-resource' is invalid. The command is not allowed in a batch.
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.PluginExecutionException: Execution addConfig of goal org.wildfly.plugins:wildfly-maven-plugin:1.0.2.Final:execute-commands failed: Command 'if (outcome != success) of /system-property=app.env:read-resource' is invalid. The command is not allowed in a batch.
        ... 20 more
Caused by: java.lang.IllegalArgumentException: Command 'if (outcome != success) of /system-property=app.env:read-resource' is invalid. The command is not allowed in a batch.
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
        ... 21 more
Caused by: org.jboss.as.cli.operation.OperationFormatException: The command is not allowed in a batch.
        ... 24 more
Here's the exception if it has a nested if-else, non-batched:
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.wildfly.plugins:wildfly-maven-plugin:1.0.2.Final:execute-commands (addConfig)
 on project jboss-config: Execution addConfig of goal org.wildfly.plugins:wildfly-maven-plugin:1.0.2.Final:execute-commands failed: Command 'if (result.value == qa) of /system-property=app.env:read-resource' is invalid. if is not allowed while in batch mode.
        ...
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.PluginExecutionException: Execution addConfig of goal org.wildfly.plugins:wildfly-maven-plugin:1.0.2.Final:execute-commands failed: Command 'if (result.value == qa) of /system-property=app.env:read-resource' is invalid. if is not allowed while in batch mode.
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:145)
        ... 20 more
Caused by: java.lang.IllegalArgumentException: Command 'if (result.value == qa) of /system-property=app.env:read-resource' is invalid. if is not allowed while in batch mode.
        at org.wildfly.plugin.cli.Commands.executeCommands(Commands.java:178)
        ... 21 more
Caused by: org.jboss.as.cli.CommandFormatException: if is not allowed while in batch mode.
        at org.jboss.as.cli.handlers.ifelse.IfHandler.doHandle(IfHandler.java:130)
        ... 24 more
For completeness, here's the script that I want to run
batch
if (outcome != success) of /system-property=app.env:read-resource
    /system-property=app.env:add(app.env=local)
else
if (result.value == qa) of /system-property=app.env:read-resource
    /system-property=app.env:remove
    /system-property=app.env:add(app.env=local)
else
    /system-property=app.env:remove
    /system-property=app.env:add(app.env=qa)
end-if
end-if
run-batch
and what actually runs:
if (outcome != success) of /system-property=app.env:read-resource
    /system-property=app.env:add(value=local)
else
    /system-property=app.env:remove
    /system-property=app.env:add(value=local)
end-if
                The issue is that if-else flow isn't allowed in batch mode as if statements are already executed as a batch. This also means that nested if statements aren't allowed.
Something like the following would work though
if (outcome != success) of /system-property=app.env:read-resource
    /system-property=app.env:add(value=local)
end-if
if (result.value == qa) of /system-property=app.env:read-resource
    /system-property=app.env:remove
    /system-property=app.env:add(value=local)
else
    /system-property=app.env:remove
    /system-property=app.env:add(value=qa)
end-if
If the property is missing this will add it, remove it then re-add it. It's the only way to get it to work and isn't an expensive operation.
Note the script you posted was a little off. When adding a system property the value attribute is used in the add operation. Also there was a mix of test and app.env for the property name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With