Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple source folders: Avoid implicit compilation with Ant

Tags:

Consider the following project layout (assuming A and B depend on each other):

.
|-- bin1
|-- bin2
|-- src1
|   `-- A.java
`-- src2
    `-- B.java

After compilation, I want the classes to reside in their respective folders liike this:

.
|-- bin1
|   `-- A.class
|-- bin2
|   `-- B.class
|-- src1
|   `-- A.java
`-- src2
    `-- B.java

This is quite simple from the command line:

 $ javac -implicit:none -sourcepath src1:src2 -d bin1 src1/*
 $ javac -implicit:none -sourcepath src1:src2 -d bin2 src2/*

Eclipse also does it that way if so configured. But I cannot figure out how to do it with Ant.

Appendix: My current javac tasks:

    <javac destdir="${classes.1.dir}">
        <src path="${src.1.dir}" />
        <src path="${src.2.dir}" />
    </javac>
    <javac destdir="${classes.2.dir}">
        <classpath path="${classes.1.dir}" />
        <src path="${src.2.dir}" />
    </javac>

Note the circular dependency. The second task works well, it only compiles what’s in src2 as it has a classpath dependency on the other build. The first task, however, cannot take a classpath, since nothing is yet compiled, and with src it of course compiles too much.

like image 288
Michael Piefel Avatar asked Jan 07 '11 16:01

Michael Piefel


1 Answers

I had the same problem. And i found some pretty easy solution.

You just need to specify several source foulders in srcdir attribute in javac task. And you should not specify destdir attribute.

Something like this:

    <javac srcdir="src1:src2" />

All the binaries (.class files) will be placed in the same places as sources. So the structure of the class files will be exactly the same. Then you can move all *.class to the separate place, so they won't be stored in the source foulders.

And no double compilation as in Kurt Kaylor's example.

like image 168
olshevski Avatar answered Sep 22 '22 02:09

olshevski