views:

963

answers:

3

Today I had to add a task to an Apache Ant file. The command line should have been something like

myprogram --param1 --param2 path\somefile 2> path\logfile

The problem with this was that if I used something like the following for this

<exec executable="$(myprogram)"
  <arg value="--param1">
  <arg value="--param2">
  <arg path="$(somefile)">
  <arg value="2>">
  <arg path="$(logfile)">
</exec>

all arguments were quoted, so the command looked like this:

myprogram "--param1" "--param2" "path\somefile" "2>" "path\logfile"

which is not bad and especially nice if you have spaces in your files/path, but destroys the pipe to the logfile (instead, the program thinks there are two additional file arguments "2>" and "path\logfile").

I worked around this by calling a batch script instead that only wants the files as parameters, but I wondered: Is it possible to do this without such a workaround?

+2  A: 

Have you tried <arg line="..." />?

nullptr
I think this should work.
Geo
Thanks, this works! I noticed the "line" variant, but the Apache Ant manual doesn't explain the difference between it and "value" well, so I thought it would just be kind of an alias.
schnaader
From the Ant docs: It is highly recommended to avoid the line version when possible. Ant will try to split the command line in a way similar to what a (Unix) shell would do, but may create something that is very different from what you expect under some circumstances. http://ant.apache.org/manual/using.html#arg
Yishai
+2  A: 

When you run "myprogram --param1 --param2 path\somefile 2> path\logfile", the arguments to your program end at "2>". File redirection is an operation of your shell, which isn't being used from within ant. If you look at the docs for the ant exec task, you'll see that it supports redirection via the output attribute.

David Winslow
Thanks, this seems like the general way to do it (I see there's an error attribute, too, if you want to difference between stdout and stderr).
schnaader
+1  A: 

The ant exec task has an output parameter where you could specify the log file without requiring the command line piping, combined with the parameter append to determine if the output file should be overwritten or appended to.

Yishai