本文介绍了是否可以从 Java 代码调用 Ant 或 NSIS 脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

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

You can call ant scripts from Java code.

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

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

更新

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

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>

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

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

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

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

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

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

来自下面@Perception 的回答.

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

这篇关于是否可以从 Java 代码调用 Ant 或 NSIS 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:38