Java Ant: How to setup "apply" task with allowing file overwriting?
My command line app call looks like this:
java -jar myapp.jar --output c:\test开发者_如何学JAVA.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.
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.
If the file exists I recommend deleting the file first using ant Then create the NEW file.
精彩评论