在ant的<exec> </exec>中引用麻烦

时间:2013-11-08 21:02:02

标签: linux ant ubuntu-12.04

这是在终端上运行正常的命令

egrep Version ./path/fileName.java | cut -d"\"" -f4

我在代码中使用了以下内容

<exec command="egrep Version ./path/fileName.java | cut -d&quot;\&quot; -f4)" outputproperty="VER"/>

但是得到错误

the command attribute is deprecated.
 [exec] Please use the executable attribute and nested arg elements.
 [exec] Result: 1
 [echo] "Version: egrep: invalid argument `\\' for `--directories'
 [echo] Valid arguments are:
 [echo]   - `read'
 [echo]   - `recurse'
 [echo]   - `skip'
 [echo] Usage: egrep [OPTION]... PATTERN [FILE]...
 [echo] Try `egrep --help' for more information."   

少了一个;在命令中因为如果我写了两个小数,它会给我一些不平衡的错误。

2 个答案:

答案 0 :(得分:2)

尝试在xml中使用'

<exec command='egrep Version ./path/fileName.java | cut -d"\"" -f4)' outputproperty="VER"/>

答案 1 :(得分:2)

Ant的<exec>使用Java的执行规则,特别是一个shell,并且不了解管道和重定向。可能你最好的选择是调用shell。如果您希望稍后在构建中使用它,则还需要捕获属性中的输出:

<exec executable="sh" outputproperty="version.number">
  <arg value="-c" />
  <arg value="egrep Version ./path/fileName.java | cut -d'&quot;' -f4" />
</exec>

或者,您可以忘记exec并使用loadfile使用filterchain直接在Ant中实现所需的逻辑,而不是调用外部进程:

<loadfile srcFile="path/fileName.java" property="version.number"
          encoding="UTF-8">
  <filterchain>
    <tokenfilter>
      <!-- equivalent of egrep Version -->
      <containsregex pattern="Version" />
      <!-- equivalent of the cut - extract the bit between the third and
           fourth double quote marks -->
      <containsregex pattern='^[^"]*"[^"]*"[^"]*"([^"]*)".*$$'
                     replace="\1" />
    </tokenfilter>
    <!-- I'm guessing you don't want a trailing newline on your version num -->
    <striplinebreaks />
  </filterchain>
</loadfile>