views:

109

answers:

1

Hi, I know how to use Ant to copy files and folders but what I'm interested in is if, and how, I can have the javac task copy the same sources it's compiling to the output directory. Basically, it's very similar to the option to include your sources in the jar task.

Thanks in advance, Ittai

+1  A: 

Why not simply use the copy task, along with the javac one ?

You can even use ant's macro to define your own copyingjavac task, that performs the two operations, with the only problem to correctly handle filesets, to copy exactly the set of files being compiled.

If you want to only copy a file when compilation succeeded, you will have to either build a custom ant task (extending the default javac task), or to play with ant_contrib foreach task.

The macrodef could look like:

<macrodef name="copyingjavac">
  <attribute name="srcdir"/>
  <attribute name="destdir""/>
  <element name="arginclude"/>
  <sequential>
    <javac srcdir="@{srcdir}" destdir="@{destdir}" updatedProperty="build.success">
       <arginclude/>
    </javac>
    <copy todir="@{destdir}">
      <fileset dir="@{srcdir}">
        <arginclude/>
      </fileset>
    </copy>
    <fail unless="build.success">
      Build failed. Check the output...
    </fail>
  </sequential>
</macrodef>
tonio
I think he wants to copy only the compiled source files.
aioobe
@tonio, thanks for your input and I'll look at the macro but what I was really interested was if this is possible as part of ant's javac task built-in as a parameter. I'll wait to see if I get any response which nails what I want and if not I'll accept your answer.BTW, what is the arginclude?@aioobe, I don't really care about copying only the compiled source files as I delete all the classes and compile them together each time for different reasons.
Ittai
@Ittai, there is nothing like that in the javac task, however, you could still do that, by using the same `srcdir` and `destdir`: this way, the class files will be in the same directory as the source files. Then build your jar from there.
tonio
@tonio, thanks I guess you are right and there isn't any alternative.Can you tell me what the arginclude is?
Ittai
the `arginclude` is a macro argument, that will be replaced by the sub-elements of the macro when you use it. This enables uses such as `<copyingjavac srcdir="..." destdir="..."><include name="*.java"></copyingjavac>`, and get the `include` passed in the macro expand.
tonio