Java Ant:如何通过允许文件覆盖来设置“应用”任务?(Java Ant: How to setup “apply” task with allowing file overwriting?)

编程入门 行业动态 更新时间:2024-10-24 23:26:25
Java Ant:如何通过允许文件覆盖来设置“应用”任务?(Java Ant: How to setup “apply” task with allowing file overwriting?)

我的命令行应用程序调用如下所示:

java -jar myapp.jar --output c:\test.txt c:\test.txt

其中读取test.txt,处理它并将结果保存到同一个文件。

我试图让ant任务离开它,但无法弄清楚如何使它使用相同的路径进行输入和输出。

<target name="compress"> <apply executable="java" parallel="false"> <fileset dir="c:/test/" includes="*.txt"> </fileset> <arg line="-jar"/> <arg path="myapp.jar"/> <srcfile/> <arg line="--output"/> <mapper type="glob" from="*" to="c:/test/*"/> <targetfile/> </apply> </target>

哪个不行。 对于应用任务使用<mapper type="identity"/>和设置dest="c:/test/"也不起作用。 看起来它只是不想重写现有的文件。 有没有办法让它在没有将输出写入分离文件夹的情况下工作,然后从原始文件夹中删除所有文件并将文件复制回原始文件夹?

谢谢。

My command line app call looks like this:

java -jar myapp.jar --output c:\test.txt c:\test.txt

Which reads test.txt, processes it and saves result to the same file.

I am trying to make ant task out of it but can't figure out how to make it use same path for input and output.

<target name="compress"> <apply executable="java" parallel="false"> <fileset dir="c:/test/" includes="*.txt"> </fileset> <arg line="-jar"/> <arg path="myapp.jar"/> <srcfile/> <arg line="--output"/> <mapper type="glob" from="*" to="c:/test/*"/> <targetfile/> </apply> </target>

Which doesn't work. Using <mapper type="identity"/> and setting dest="c:/test/" for apply task doesn't work either. Looks like it just doesn't want to rewrite existing files. Is there a way to make it work without writing output to a separated folder, then deleting all files from the original folder and copying files back to original folder?

Thanks.

最满意答案

首先,你应该使用<arg value="..."/>而不是<arg line="..."/> 。 后者不适用于多个论点, 应该避免一般情况 。

其次, 应用任务将目标文件与源文件进行比较,如果两者相同(或者如果目标文件比源代码更新,那么显然不适用于您的情况)将不会被调用。 你可以使用force="true"属性来避免这种情况。

以下适用于我:

<target name="compress"> <apply executable="java" parallel="false" dest="c:/test/" force="true"> <fileset dir="c:/test/" includes="*.txt" /> <arg value="-jar"/> <arg path="myapp.jar"/> <srcfile/> <arg value="--output"/> <mapper type="identity"/> <targetfile/> </apply> </target>

您可以在详细模式下运行Ant(使用“-v”开关)以查看此任务正在生成的实际命令行。

First of, you should be using <arg value="..."/> rather than <arg line="..."/>. The latter is not going to work for multiple arguments and should be avoided in general.

Secondly, apply task compares target files with source files and will not be invoked if both are the same (or if the target file is newer than source which is obviously not applicable in your case). You can use force="true" attribute to avoid this.

The following works for me:

<target name="compress"> <apply executable="java" parallel="false" dest="c:/test/" force="true"> <fileset dir="c:/test/" includes="*.txt" /> <arg value="-jar"/> <arg path="myapp.jar"/> <srcfile/> <arg value="--output"/> <mapper type="identity"/> <targetfile/> </apply> </target>

You can run Ant in verbose mode (using "-v" switch) to see the actual command lines this task is generating.

更多推荐

test,文件,work,电脑培训,计算机培训,IT培训"/> <meta name="description"

本文发布于:2023-07-30 08:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336723.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   setup   Java   Ant   apply

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!