是否可以从 Java 代码调用 Ant 或 NSIS 脚本?

发布于 2024-11-16 15:45:41 字数 56 浏览 2 评论 0原文

是否可以在运行时从 Java 代码以编程方式调用 Ant 或 NSIS 脚本?如果是这样,怎么办?

Is it possible to call Ant or NSIS scripts programmatically from Java code at runtime? If so, how?

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

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

发布评论

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

评论(4

━╋う一瞬間旳綻放 2024-11-23 15:45:41

可以从Java代码调用ant脚本。

请参阅本文(向下滚动到“通过 Java 运行 Ant”部分)和这篇文章

   File buildFile = new File("build.xml");
   Project p = new Project();
   p.setUserProperty("ant.file", buildFile.getAbsolutePath());
   p.init();
   ProjectHelper helper = ProjectHelper.getProjectHelper();
   p.addReference("ant.projectHelper", helper);
   helper.parse(p, buildFile);
   p.executeTarget(p.getDefaultTarget());

更新

我尝试使用以下ant文件,它没有“告诉”任何东西(没有控制台输出),但它工作:文件确实被移动了

   <project name="testproject" default="test" basedir=".">
      <target name="test">
        <move file="test.txt" tofile="test2.txt" />
      </target>
   </project>

当我尝试时再次(当没有 test.txt 可以移动时(它已经被移动)),我得到了 java.io.FileNotFoundException

我认为当您从 Java 运行某些东西时,这就是您所期望的。

如果您想要 ant 任务的控制台输出,您可能需要添加 Logger 作为构建侦听器。

来自@Perception的下面的回答。

   DefaultLogger consoleLogger = new DefaultLogger();
   consoleLogger.setErrorPrintStream(System.err);
   consoleLogger.setOutputPrintStream(System.out);
   consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
   p.addBuildListener(consoleLogger);

You can call ant scripts from Java code.

See this article (scroll down to the "Running Ant via Java" section) and this article:

   File buildFile = new File("build.xml");
   Project p = new Project();
   p.setUserProperty("ant.file", buildFile.getAbsolutePath());
   p.init();
   ProjectHelper helper = ProjectHelper.getProjectHelper();
   p.addReference("ant.projectHelper", helper);
   helper.parse(p, buildFile);
   p.executeTarget(p.getDefaultTarget());

Update

I tried with the following ant file , it did not "tell" anything (no console output), but it worked: the file was indeed moved

   <project name="testproject" default="test" basedir=".">
      <target name="test">
        <move file="test.txt" tofile="test2.txt" />
      </target>
   </project>

And when I try it again (when there is no test.txt to move(it is already moved)), I got an java.io.FileNotFoundException.

I think this is what you would expect when you run something from Java.

If you want the console output of the ant tasks, you might want to add a Logger as a build listener.

From @Perception's answer below.

   DefaultLogger consoleLogger = new DefaultLogger();
   consoleLogger.setErrorPrintStream(System.err);
   consoleLogger.setOutputPrintStream(System.out);
   consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
   p.addBuildListener(consoleLogger);
泪冰清 2024-11-23 15:45:41

过于扩展 Nivas 的答案 - 他的解决方案是正确的,您没有看到程序的输出,因为您没有将任何记录器附加到项目中。

DefaultLogger consoleLogger = new DefaultLogger();
consoleLogger.setErrorPrintStream(System.err);
consoleLogger.setOutputPrintStream(System.out);
consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
p.addBuildListener(consoleLogger);

这只是基本设置,您可以使用 Ant Java API 执行更多操作

Too expand on Nivas' answer - his solution is correct, you are not seeing output from your program because you haven't attached any loggers to your project.

DefaultLogger consoleLogger = new DefaultLogger();
consoleLogger.setErrorPrintStream(System.err);
consoleLogger.setOutputPrintStream(System.out);
consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
p.addBuildListener(consoleLogger);

This is just basic setup, theres alot more you can do with the Ant Java API.

复古式 2024-11-23 15:45:41

是的,Nivas 给出了最具体的答案,也可能是最好的答案,但请叫我老式的,我仍然更喜欢这样做:

Process child = Runtime.getRuntime().exec(command);

其中 command 是带有 ant 命令的字符串或 ant 命令的 shell 脚本;

如果您不需要所有“花里胡哨”(我承认这很酷),那么以后需要管理的代码就会更少。按照我的方式,你不需要对 ant 调用进行单步调试。代价是您的文件需要位于具有特殊名称的特殊位置,但无论如何 Java 在这方面已经有点强迫症了。 (不要激怒我。;)Java 也是我最喜欢的语言,但是来吧,我们在这里讨论部署选项,您知道它在部署选项区域有点强迫症,对吧?) 我想都是我真正建议的是“用突击队刀给吐司涂黄油可能有点过头了。”并考虑这一点。 ;)

* 为了说教而自行编辑

Yes, and Nivas gave the most specific answer and probably the best answer too, but call me old fashioned, I still prefer just doing:

Process child = Runtime.getRuntime().exec(command);

where command is a String with your ant commands or a shell script to your ant commands;

If you don't need all the "bells and whistles", which are pretty cool I admit, then it is less code to manage later. You don't get to step debug the ant calls, my way. The trade off is that your files need to be in special places with special names, but Java is already a little OCD in that regard anyway. (Don't flame me. ;) Java is my favorite language too, but come on, we're talking deployment options here and you know it's a little OCD in the deployment options area, right?) I guess all I'm really suggesting is "It may be over kill to use a commando knife to butter toast." and to consider that as well. ;)

* self edited for preachiness

不甘平庸 2024-11-23 15:45:41

user804965的解决方案是我之前实现过的。我必须从 Java 运行一些终端命令,并使用 Process 和 Runtime Java 对象。同样的想法可以应用于运行 ant 命令。

例如:

  ProcessBuilder pb = new ProcessBuilder("path/to/my/script/script.sh");
  Map<String, String> env = pb.environment();
  Process p = pb.start();
  p.waitFor();
  BufferedReader buf = new BufferedReader(new InputStreamReader(
          p.getInputStream()));
  String line = "";
  while ((line = buf.readLine()) != null) {
    System.out.println(line);
  }

这将从 Java 运行脚本文件并将输出打印到控制台。在 script.sh 中,您可以执行以下操作:

cd /path/to/my/build.xml/file/; ant -p

或者调用您需要调用的任何 ant 脚本(例如 ant clean)。

您也可以在没有附加 sh 文件的情况下执行此操作,只需从 Java 调用终端命令即可。不过我还没有尝试过这种方法。

user804965's solution is one I've implemented before. I had to run some terminal commands from Java, and used the Process and Runtime Java objects. The same idea can be applied to running ant commands.

For example:

  ProcessBuilder pb = new ProcessBuilder("path/to/my/script/script.sh");
  Map<String, String> env = pb.environment();
  Process p = pb.start();
  p.waitFor();
  BufferedReader buf = new BufferedReader(new InputStreamReader(
          p.getInputStream()));
  String line = "";
  while ((line = buf.readLine()) != null) {
    System.out.println(line);
  }

This will run a script file from Java and print the output to the console. Inside script.sh you can do something along the lines of...

cd /path/to/my/build.xml/file/; ant -p

Or call whatever ant script you need to call (ant clean, for example).

You can also likely do this without having the additional sh file, and just call the terminal command from Java. I have not tried this approach though.

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