tags:

views:

40

answers:

1

I would like to know how to do something in ant(1) that's equivalent to a particular makefile(4) rule. The makefile(4) rule does the following: 1) starts a process that doesn't terminate and that writes one line to its standard output stream; 2) reads the line from the process; 3) constructs a file using the line; and 4) starts a second process that doesn't terminate using the file as an argument. Schematically, the makefile(4) rule is

program1 | while read arg; do \
    echo $$arg >file; \
    program2 file; \
done

NOTE: "program1" writes one line; neither "program1" nor "program2" terminates.

How can this be done in ant(1)?

+1  A: 

You should be able to use ProcessBuilder as outlined below:

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class PBTest {

    public static void main(String[] args) {
        ProcessBuilder pb = new ProcessBuilder("process1");
        pb.redirectErrorStream(true);
        try {
            Process p = pb.start();
            String s;
            // read from the process's combined stdout & stderr
            BufferedReader stdout = new BufferedReader (
                new InputStreamReader(p.getInputStream()));
            if ((s = stdout.readLine()) != null) {
                ProcessBuilder pb2 = new ProcessBuilder("process2", s);
                pb2.start();
                ...
            }
            System.out.println("Exit value: " + p.waitFor());
            p.getInputStream().close();
            p.getOutputStream().close();
            p.getErrorStream().close();
         } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

Then your java task is fairly straightforward:

<!-- Run the program -->
<target name="run">
    <java classname="PBTest" fork="true"></java> 
</target>

Addendum:

I'm looking for a solution in ant(1) rather than Java.

You can use any Apache BSF or JSR 223 supported language in the script task. I don't see a way to use standard input and output directly, but you can use the loadfile task to load a property from a file. Here's an example that obtains a version number from a source file.

trashgod
@Pourquoi Litytestdata: Thanks!
trashgod
@Pourquoi Litytestdata Thank you for your answer, but, as my question indicated, I'm looking for a solution in ant(1) rather than Java.
Steve Emmerson
@Steve Emmerson: I've suggested some other possibilities above.
trashgod