Post by Roedy GreenPost by Steven SimpsonI came to the conclusion that Ant is insufficient without an
error-detecting editor (as in an IDE). If class Foo uses class Bar,
class Bar could be changed in a way that causes an error in Foo, yet Ant
will not detect that Foo needs to be recompiled in order to see the
error, which goes unnoticed until runtime, or until Foo is re-compiled.
I don't think ant has anything to do with it. It just hands all the
source files to javac.exe, and javac.exe makes the decision on what
needs to be recompiled.
That only happens if the output directory is included in the classpath.
Is Ant invoking javac this way? 'ant -v' shows this:
[javac] Compilation arguments:
[javac] '-d'
[javac] '/scratch/simpsons/Scratch/Coding/antfail2/classes'
[javac] '-classpath'
[javac] '/scratch/simpsons/Scratch/Coding/antfail2/classes'
[javac] '-sourcepath'
[javac] '/scratch/simpsons/Scratch/Coding/antfail2/src'
[javac] '-g:none'
(Note that source files are not listed here, so it's not the complete
set of arguments. We can't directly tell which source files are passed
from this statement.)
The includeDestClasses attribute of <javac> controls this, and defaults
to "true", though I was also setting it explicitly to its default. I
set it to false, and got:
[javac] Compilation arguments:
[javac] '-d'
[javac] '/scratch/simpsons/Scratch/Coding/antfail2/classes'
[javac] '-classpath'
[javac] ''
[javac] '-sourcepath'
[javac] '/scratch/simpsons/Scratch/Coding/antfail2/src'
[javac] '-g:none'
So that attribute does what it says on the tin. I repeated my test run
(see end of message) to see if it made any difference. However, again
only Bar is recompiled, so Ant is still showing the behaviour I
mentioned - javac is only being offered the source files that have changed.
Post by Roedy GreenJavac/ANT is so fast compared with what we used to do, loading javac
for each source module and fiddling with MAKE.
Certainly, there's no point trying to use Make to pass one file at a
time to javac. If you have a dependency sequence A->B->C, and you make
a change to B and decide to recompile, C is going to get compiled as an
implicit class anyway (unless you add the destination directory to the
classpath, which we've already seen misses things), and you might need A
compiled anyway. You may as well just compile A if any of the source
files for A, B or C have changed. IOW, if it's so fast, just do a clean
build every time.
Here's the corrected test run...
% cat build.xml
<project default="compile">
<target name="compile">
<mkdir dir="classes" />
<javac srcdir="src" destdir="classes"
listfiles="yes" includeDestClasses="false"
includeAntRuntime="false" />
</target>
<target name="clean">
<delete dir="classes" />
</target>
</project>
% cat src/Foo.java
public class Foo {
public static void main(String[] args) throws Exception {
new Bar();
}
}
% cat src/Bar.java
public class Bar { }
% ant clean
Buildfile: /scratch/simpsons/Scratch/Coding/antfail2/build.xml
clean:
[delete] Deleting directory /scratch/simpsons/Scratch/Coding/antfail2/classes
BUILD SUCCESSFUL
Total time: 0 seconds
% ant
Buildfile: /scratch/simpsons/Scratch/Coding/antfail2/build.xml
compile:
[mkdir] Created dir: /scratch/simpsons/Scratch/Coding/antfail2/classes
[javac] Compiling 2 source files to /scratch/simpsons/Scratch/Coding/antfail2/classes
[javac] /scratch/simpsons/Scratch/Coding/antfail2/src/Bar.java
[javac] /scratch/simpsons/Scratch/Coding/antfail2/src/Foo.java
BUILD SUCCESSFUL
Total time: 0 seconds
% java -cp classes Foo
% sed -e 's/class/abstract class/' -i src/Bar.java
% cat src/Bar.java
public abstract class Bar { }
% ant
Buildfile: /scratch/simpsons/Scratch/Coding/antfail2/build.xml
compile:
[javac] Compiling 1 source file to /scratch/simpsons/Scratch/Coding/antfail2/classes
[javac] /scratch/simpsons/Scratch/Coding/antfail2/src/Bar.java
BUILD SUCCESSFUL
Total time: 0 seconds
% java -cp classes Foo
Exception in thread "main" java.lang.InstantiationError: Bar
at Foo.main(Unknown Source)
% touch src/Foo.java
% ant
Buildfile: /scratch/simpsons/Scratch/Coding/antfail2/build.xml
compile:
[javac] Compiling 1 source file to /scratch/simpsons/Scratch/Coding/antfail2/classes
[javac] /scratch/simpsons/Scratch/Coding/antfail2/src/Foo.java
[javac] /scratch/simpsons/Scratch/Coding/antfail2/src/Foo.java:3: error: Bar is abstract; cannot be instantiated
[javac] new Bar();
[javac] ^
[javac] 1 error
BUILD FAILED
/scratch/simpsons/Scratch/Coding/antfail2/build.xml:6: Compile failed; see the compiler error output for details.
Total time: 0 seconds
--
ss at comp dot lancs dot ac dot uk