是否可以从 Java 代码调用 Ant 或 NSIS 脚本?
是否可以在运行时从 Java 代码以编程方式调用 Ant 或 NSIS 脚本?如果是这样,怎么办?
Is it possible to call Ant or NSIS scripts programmatically from Java code at runtime? If so, how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以从Java代码调用ant脚本。
请参阅本文(向下滚动到“通过 Java 运行 Ant”部分)和这篇文章:
更新
我尝试使用以下ant文件,它没有“告诉”任何东西(没有控制台输出),但它工作:文件确实被移动了
当我尝试时再次(当没有
test.txt
可以移动时(它已经被移动)),我得到了java.io.FileNotFoundException
。我认为当您从 Java 运行某些东西时,这就是您所期望的。
如果您想要 ant 任务的控制台输出,您可能需要添加 Logger 作为构建侦听器。
来自@Perception的下面的回答。
You can call ant scripts from Java code.
See this article (scroll down to the "Running Ant via Java" section) and this article:
Update
I tried with the following ant file , it did not "tell" anything (no console output), but it worked: the file was indeed moved
And when I try it again (when there is no
test.txt
to move(it is already moved)), I got anjava.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.
过于扩展 Nivas 的答案 - 他的解决方案是正确的,您没有看到程序的输出,因为您没有将任何记录器附加到项目中。
这只是基本设置,您可以使用 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.
This is just basic setup, theres alot more you can do with the Ant Java API.
是的,Nivas 给出了最具体的答案,也可能是最好的答案,但请叫我老式的,我仍然更喜欢这样做:
其中 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:
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
user804965的解决方案是我之前实现过的。我必须从 Java 运行一些终端命令,并使用 Process 和 Runtime Java 对象。同样的想法可以应用于运行 ant 命令。
例如:
这将从 Java 运行脚本文件并将输出打印到控制台。在 script.sh 中,您可以执行以下操作:
或者调用您需要调用的任何 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:
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...
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.