如何在某些路径下检查蚂蚁是否是执行的



我如何使用ant验证当前工作目录是否位于某个路径下(任意深度嵌套(?例如,我只想在当前目录是/some/dir/的一部分时才执行目标,例如,如果在目录/some/dir/to/my/project/中执行ANT。

我能想到的最好的是字符串包含条件:

<if>
    <contains string="${basedir}" substring="/some/dir/"/>
    <then>
        <echo>Execute!</echo>
    </then>
    <else>
        <echo>Skip.</echo>
    </else>
</if>

这适用于我当前的目的,但恐怕将来可能会稍微打破时间...例如,当构建在路径/not/some/dir/中执行时,该构建也包含指定的目录字符串。

还有其他强大的解决方案,例如startsWith比较,甚至更好的基于文件系统的检查...?

本机蚂蚁中没有特定的startswith条件,但是有一个matches条件采用正则表达式。

作为旁注,对于大多数构建脚本而言,很少需要ANT-CONTRIB,并且通常会导致不可靠的代码。我强烈建议避免它。

这是一个示例脚本,可以说明如何将matches条件与本机蚂蚁使用。test目标当然只是用于演示。

<property name="pattern" value="^/some/dir" />
<target name="init">
    <condition property="basedir.starts.with">
        <matches pattern="${pattern}" string="${basedir}" />
    </condition>
</target>
<target name="execute" depends="init" if="basedir.starts.with">
    <echo message="Executing" />
</target>
<target name="test">
    <condition property="dir1.starts.with">
        <matches pattern="${pattern}" string="/some/dir/" />
    </condition>
    <condition property="dir2.starts.with">
        <matches pattern="${pattern}" string="/some/dir/to/my/project/" />
    </condition>
    <condition property="dir3.starts.with">
        <matches pattern="${pattern}" string="/not/some/dir/" />
    </condition>
    <echo message="${dir1.starts.with}" />
    <echo message="${dir2.starts.with}" />
    <echo message="${dir3.starts.with}" />
</target>

相关内容

最新更新