0

我正在尝试使用 ant(类似于“which”命令)查找可执行文件的路径(在 Linux 上)。例如:

which ls

输出:

/bin/ls

它不能搜索文件系统,它必须搜索 $PATH。

到目前为止,我所看到的只是使用 jython 编写脚本可以工作,但我想知道替代方案,因为 jython 似乎需要安装(我宁愿避免安装)。有什么建议么?

4

1 回答 1

3

您可以在构建脚本中嵌入脚本语言。

以下示例使用 ivy 下载所需的依赖项,并且应该也可以在 Windows 上运行:

<project name="ANT which" default="which" xmlns:ivy="antlib:org.apache.ivy.ant">

    <description>
    ANT example that simulates the unix "which" command

        $ ant -Dwhich.cmd=ls

        which:
        Found /bin/ls
    </description>

    <!--
    Properties
    -->
    <property environment="env"/>
    <property name="which.cmd" value="ls"/>

    <!--
    Bootstrap the build for ANT installations without ivy pre-installed
    -->
    <target name="bootstrap" description="Install ivy">
        <mkdir dir="${user.home}/.ant/lib"/>
        <get src="http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.3.0-rc1/ivy-2.3.0-rc1.jar" dest="${user.home}/.ant/lib/ivy.jar"/>
    </target>

    <!--
    Download groovy
    -->
    <target name="resolve" description="Resolve build dependencies">
        <ivy:cachepath pathid="build.path">
            <dependency org="org.codehaus.groovy" name="groovy-all" rev="2.0.1" conf="master"/>
        </ivy:cachepath>

        <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>
    </target>

    <!--
    Parse the PATH variable and determine if the command is available
    -->
    <target name="which" depends="resolve" description="ANT which command">
        <groovy>
            <arg value="${which.cmd}"/>

            def sepchar = properties["path.separator.ivy.instance"]

            properties["env.PATH"].split(sepchar).each {
                def dir = new File(it)

                if (dir.exists()) {
                    dir.eachFileMatch(~/^${args[0]}(.bat|.cmd)?$/) {
                        project.log "Found ${it}"
                    }
                }
            }
        </groovy>
    </target>

    <!--
    Cleanup
    -->
    <target name="clean" description="Purge the ivy cache">
        <ivy:cleancache/>
    </target>

</project>
于 2012-08-27T22:12:42.247 回答