首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >野生苍蝇CLI添加/更新系统属性批处理和if- and问题

野生苍蝇CLI添加/更新系统属性批处理和if- and问题
EN

Stack Overflow用户
提问于 2016-02-24 19:32:36
回答 1查看 4.2K关注 0票数 4

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

  1. 它不能在批处理模式下工作
  2. 嵌套if-else即使在非批处理模式下也不能工作。
  3. 类似的结果,如果在CLI脚本中执行-批处理和嵌套if -否则不工作。

这是我pom.xml中的插件部分

代码语言:javascript
复制
<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>   

以下是批处理模式下的例外情况:

代码语言:javascript
复制
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 -否则,非批处理,则为例外情况:

代码语言:javascript
复制
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

为了完整起见,下面是我想运行的脚本

代码语言:javascript
复制
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

而实际运行的是:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-25 16:48:35

问题是在批处理模式下不允许if-else流,就好像语句已经作为批处理执行一样。这也意味着不允许嵌套if语句

不过,下面这样的方法是可行的

代码语言:javascript
复制
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操作。此外,属性名还混合了testapp.env

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35611343

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档