2012-08-27 86 views
0

我试图找到一个可执行文件(在Linux上)使用ant(类似于“which”命令)的路径。例如:蚂蚁'哪个'命令

which ls 

输出:

/bin/ls 

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

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

+1

是否有一个原因,你不能只使用任务,并呼吁“的”直接? –

回答

3

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

下面的示例使用常春藤下载所需要的依赖,并应工作在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>