tags:

views:

120

answers:

2

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.

A: 

If the file exixts I recommend deleting the file first using ant Then create the NEW file.

Woot4Moo
Yes, this will work especially well considering OP wants source and target files to be **the same**.
ChssPly76
I apologize in advance, but was that sarcasm? It's hard to tell on the interwebs.
Woot4Moo
If you're not sure whether someone's being sarcastic, they're being sarcastic :-)
ChssPly76
touche my friend, touche :)
Woot4Moo
+2  A: 

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.

ChssPly76
Perfect, thanks.
serg
+1 for -v switch have searched the docs for apply to debug command but didn't think of this :)
antonj