passing command line arguments to ant

2019-05-02 01:32发布

问题:

I am relatively new in ant, at school I have an assignment to do a build file. One of my questions is to copy to "/foldercopy" the file whose name(or path) is taken as argument for ant. I need to do something like:

ant cpfile file.txt

So ant will copy the file.txt to /foldercopy. I searched a lot on google but all I could find was something with "-Darg", but my teacher said that it's not correct. Is there any way to do it?

回答1:

Plain command line arguments to ant are considered to be target names, so if you want to pass arguments to your target you need to use properties, via -D:

ant -Dfile=file.txt cpfile

and access the value as ${file} inside build.xml



回答2:

This will help you:

<target name="copytask" >
  <copy file="file.txt" todir="path-od-dir" failonerror="false" />

</target>