jattcoder
jattcoder

Reputation: 13

Trouble with quotes in ant's <exec>

This is the command that runs just fine on terminal

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

I've used the following in my code

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

But getting errors

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."   

there is one less quot; in the command because if I write 2 quots, it will give me number of quots unbalanced error.

Upvotes: 0

Views: 1774

Answers (2)

Ian Roberts
Ian Roberts

Reputation: 122364

Ant's <exec> uses Java's rules for execution, in particular it is not a shell and does not understand pipes and redirections on its own. Probably your best bet will be to invoke a shell. You will also need to capture the output in a property if you want to make use of it later in your build:

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

Alternatively, you could forget the exec and implement the required logic directly in Ant using loadfile with a filterchain rather than calling an external process:

<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>

Upvotes: 2

rzymek
rzymek

Reputation: 9281

Try using ' in your xml

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

Upvotes: 2

Related Questions