首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Ant 'which‘命令

Ant 'which‘命令
EN

Stack Overflow用户
提问于 2012-08-28 01:26:26
回答 1查看 296关注 0票数 0

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

代码语言:javascript
运行
复制
which ls

输出:

代码语言:javascript
运行
复制
/bin/ls

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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-28 06:12:42

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

下面的示例使用ivy下载所需的依赖项,并且应该也适用于windows:

代码语言:javascript
运行
复制
<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>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12146591

复制
相关文章

相似问题

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