如何在 ant(1) 中执行 makefile(4) 规则?

发布于 2024-09-08 08:05:02 字数 368 浏览 0 评论 0原文

我想知道如何在 ant(1) 中执行相当于特定 makefile(4) 规则的操作。 makefile(4) 规则执行以下操作: 1) 启动一个不会终止的进程,并将一行写入其标准输出流; 2)从进程中读取行; 3) 使用该行构造一个文件; 4) 使用该文件作为参数启动第二个进程,该进程不会终止。概括地说,makefile(4) 规则是

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

注意:“program1”写入一行; “program1”和“program2”都不会终止。

在 ant(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)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魔法唧唧 2024-09-15 08:05:03

您应该能够使用 ProcessBuilder 如下所述:

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();
        }
    }
}

然后您的 java任务相当简单:

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

附录:

我正在 ant(1) 而不是 Java 中寻找解决方案。

您可以在 脚本任务。我没有看到直接使用标准输入和输出的方法,但您可以使用 loadfile 任务 从文件加载属性。下面是一个从源文件获取版本号的示例

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文