I'm trying to run the following command using the 'exec' task in Ant:
ls -l /foo/bar | wc -l
Currently, I have my exec looking like this:
<exec executable="ls" outputproperty="noOfFiles"> <arg value="-l" /> <arg value="/foo/bar" /> <arg value="|" /> <arg value="wc" /> <arg value="-l" /> </exec>
The 'ls' command looks to be working but it's having a hard time piping the output to 'wc'. Any suggestions?
To run the ant build file, open up command prompt and navigate to the folder, where the build. xml resides, and then type ant info. You could also type ant instead. Both will work,because info is the default target in the build file.
Ant tasks are the units of your Ant build script that actually execute the build operations for your project. Ant tasks are usually embedded inside Ant targets. Thus, when you tell Ant to run a specific target it runs all Ant tasks nested inside that target.
If you use sh -c
as Aaron suggests, you can pass the whole pipeline as a single arg, effectively doing:
sh -c "ls -l foo/bar | wc -l"
If you use separate args, they are consumed by sh, not passed through to ls (hence you see just the current directory).
Note that on my system, ls -l
includes a total as well as a list of the files found, which means the count shown is one more than the number of files. So suggest:
<exec executable="sh" outputproperty="noOfFiles"> <arg value="-c" /> <arg value="ls foo/bar | wc -l" /> </exec>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With