How to get javac to recompile source files when their dependencies change?

It seems that my project has run-time errors when using javac for incremental builds. Is this type of workflow supported? For example, if A.java depends on B.java , and B.java changes; will javac recompile A.java because its dependency has changed?

Now I am using javac ant build-task to compile:

  <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> 
+4
source share
3 answers

Since you are using ant, check the depend task.

+8
source

The javac command-line javac compile every source file specified on the command line, plus everything else they depend on if they don't have newer class files.

The ant javac task tries to be smarter in order to avoid always compiling everything - it recompiles only those files that have changed (i.e., newer than their corresponding class files). This does not pay attention to the fact that the dependency of a class may have changed, and therefore other classes must also be recompiled.

In my current project, I just do ant clean whenever I encounter problems during testing (and, of course, before any production deployment) that deletes all class files. But, as vanza said, there is a depend task, the task of which is to find and remove all classes that depend on your changed classes - run this before your javac task, and you should be good.

+5
source

It depends on what has changed in B.java. If nothing has changed, which affects how the class is represented by A, then javac does not need to recompile A.java for the changes to take effect.

However, if you see a behavior in which you believe that the old code loads and runs, I would be more suspicious of the deployment / packaging process than the compilation process. YMMV.

0
source

Source: https://habr.com/ru/post/1340904/


All Articles