Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you make javac recompile source files when their dependencies change?

I seem to be getting run-time errors in my project when using javac for incremental builds. Is this type of workflow supported? For example, if A.java depends on B.java, and B.java is modified; will javac recompile A.java because its dependency changed?

Right now I'm using a javac ant build-task for compiling:

    <javac destdir="${classes.dir}"
            srcdir="${src.dir}"
            source="${javac.version}"
            debug="${javac.debug}"
            deprecation="${javac.deprecation}"
            includeantruntime="build.sysclasspath=last">
        <classpath refid="compile.classpath" />
        <classpath refid="junit.classpath" />
    </javac>
like image 432
cmcginty Avatar asked Dec 12 '22 15:12

cmcginty


2 Answers

Since you're using ant, check out the depend task.

like image 61
vanza Avatar answered Apr 09 '23 04:04

vanza


The javac command line compiler will compile every source file given on the command line, and additionally everything on which these depend, if they don't have newer class files.

The ant javac task tries to be a bit smarter, to avoid compiling always everything - it recompiles only those files which have changed (i.e. are newer than their respective class files). This does not pay attention to the case that maybe the dependency of some class changed, and thus other classes need to be recompiled too.

In my current project, I simply do ant clean whenever I have problems on tests (and of course before any production deployment), which deletes all class files. But as vanza said, there is the depend task whose task is to find and delete all classes who depend on your changed classes - run this before your javac task and you should be good.

like image 24
Paŭlo Ebermann Avatar answered Apr 09 '23 02:04

Paŭlo Ebermann