command-line-interface - Wildfly CLI 添加/更新系统属性批处理和 if-else 问题

标签 command-line-interface jboss7.x wildfly wildfly-8

我测试了通过 Wildfly Maven 插件在 Wildfly(版本 8.2.1)standalone.xml 中添加系统属性的几种变体。基本上,如果系统属性不存在,它会添加一个系统属性,如果存在则更改其值。理想情况下,我想要一个批处理模式的 CLI 脚本,带有嵌套的 if-else。然而,问题是:

  • 它在批处理模式下不起作用
  • 即使在非批处理模式下,嵌套的 if-else 也不起作用
  • 如果在 CLI 脚本中执行类似的结果 - 批处理和嵌套的 if-else 不起作用

  • 这是我的 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>   
    

    如果在批处理模式下,这是异常(exception):
    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
    

    如果它具有嵌套的 if-else,非批处理,则这是异常(exception):
    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
    

    为了完整起见,这是我要运行的脚本
    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
    

    以及实际运行的内容:
    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
    

    最佳答案

    问题是在批处理模式下不允许 if-else 流,就像语句已经作为批处理执行一样。这也意味着 nested if statements aren't allowed .

    虽然像下面这样的东西会起作用

    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
    

    如果该属性丢失,这将添加它,删除它然后重新添加它。这是让它工作的唯一方法,而且不是一项昂贵的操作。

    请注意,您发布的脚本有点过时。添加系统属性时 value属性用于 add手术。还有 test 的混合和 app.env对于属性名称。

    关于command-line-interface - Wildfly CLI 添加/更新系统属性批处理和 if-else 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35611343/

    相关文章:

    windows - 有更好的 Windows 控制台窗口吗?

    java - 名称 [ejb :. .] 未绑定(bind)在此上下文中。无法在 Wildfly 中找到具有根本原因 javax.naming.NameNotFoundException 的 [ejb : ].]

    Wildfly 11 连接到远程 Artemis ActiveMQ 服务器配置

    server - 如何在 Wildfly 配置文件中使用变量?

    javascript - 如何自动将 CSS 应用于 console.log 着色

    linux - 我可以获得已执行的 Linux shell 命令的开始和停止时间戳吗?

    jsf-2 - 如何在 JSF 2/Primefaces/JBoss 应用程序中监控长进程

    java - EJB 异常 : Channel Channel ID fc7b3f27 (outbound) of Remoting connection 31ad98ef to localhost/127. 0.0.1:4447 无法再处理消息

    java - 在基于命令行的计算器程序中处理输入的最佳做法是什么?

    java - 我应该使用专用服务器来托管 EJB 应用程序吗?