Ant命令行中传递的参数数量

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ant命令行中传递的参数数量相关的知识,希望对你有一定的参考价值。

我知道是否可以使用Ant任务来了解传递给Ant目标的参数数量?例如,使用以下命令:

ant myTarget -Darg1="arg1" -Darg2="arg2"

我希望能够在“myTarget”目标中获得用户正好传递2个参数。

我已经建立了这个条件:

<condition property="params.set">
    <and>
        <isset property="arg1"/>
        <isset property="arg2"/>
    </and>
</condition>

但我想在其中添加一个检查传递参数的数量。

或者也许可以使用“myTarget”目标中的常规任务获取相同的信息?我认为获取整个命令行就足够了(但是怎么做?)因为我可以计算“-D”标记的数量。

提前致谢。

答案

就个人而言,我建议检查用户不应该设置的特定属性,而不是简单地计算它们并假设任何额外的内容是不需要的。如果有人正在修改或稍后调试它,那么这可能会使脚本很烦人。

这将是我的方法:

<fail>
    <condition>
        <or>
            <not>
                <or>
                    <isset property="arg1" />
                    <isset property="arg2" />
                </or>
            </not>
            <isset property="doNotSet1" />
            <isset property="doNotSet2" />
            <isset property="doNotSet3" />
            </or>
    </condition>
</fail>

但是,如果你按照你所描述的方式去做,那么技术上应该是可行的。 Ant将名为“命令行”的propertyset中的初始命令定义的任何属性存储起来。但是,此集合不仅包括使用-D...定义的用户属性。它还将包含自动设置的生成属性,例如ant.file。这些属性的数量可能会有所不同,具体取决于脚本的配置方式(例如,在根元素中指定默认目标),因此需要过滤掉这些属性。幸运的是,这些生成的属性都以ant.fileant.project开头,因此它们的识别相对简单。

<fail>
    <condition>
        <or>
            <not>
                <or>
                    <isset property="arg1" />
                    <isset property="arg2" />
                </or>
            </not>
            <resourcecount when="ne" count="2">
                <intersect>
                    <propertyset>
                        <propertyref builtin="commandline" />
                    </propertyset>
                    <propertyset>
                        <propertyref regex="^(?!ant.(?:file|project))" />
                    </propertyset>
                </intersect>
            </resourcecount>
        </or>
    </condition>
</fail>
另一答案

我终于使用了:

<resourcecount when="ne" count="2">
    <difference>
        <propertyset>
            <propertyref builtin="commandline"/>
        </propertyset>
        <union>
            <propertyset>
                <propertyref prefix="ant.file"/>
            </propertyset>
            <propertyset>
                <propertyref prefix="ant.project"/>
            </propertyset>
        </union>
    </difference>
</resourcecount>

同时:

<condition>
    <or>
        <equals arg1="${library.name}" arg2=""/>
        <equals arg1="${suffix}" arg2=""/>
    </or>
</condition>

而且运作良好。再次感谢您的回答包含非常有趣的路径。

以上是关于Ant命令行中传递的参数数量的主要内容,如果未能解决你的问题,请参考以下文章

如何在命令行中使用 & & & 符号在后台执行批处理文件,但传递参数?

在 Apache Ant 中,有没有办法从 ant 命令中获取参数并将其用作属性文件的键?

将命令行参数和文本文件传递给程序

Python成长型笔记持续更新

Python成长型笔记持续更新

Linux 上的 Open***:在命令行中传递用户名和密码