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

时间:2018-02-26 18:05:01

标签: 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”标记的数量。

提前致谢。

2 个答案:

答案 0 :(得分:0)

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

这将是我的方法:

<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>

答案 1 :(得分:0)

我终于使用了:

<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>

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

相关问题