Using the javaclasses built-in macro

The $(javaclasses) function can be used to generate a list of .class targets, from existing .java files. The function takes one or two path arguments.

If your builds write .class files to the same directory as the .java files, use the single-argument form. For example:

ALL_CLASSES=$(javaclasses .)

If this makefile resides in /vobs/myvob/src, along with A.java and B.java, then the previous command sets ALL_CLASSES to

/vobs/myvob/src/A.class /vobs/myvob/src/B.class

To provide a way for clearmake to evaluate all the .class targets, use this macro in the dependency list of some other target. For example:

ALL_CLASSES=$(javaclasses .)
  all : $(ALL_CLASSES)

If your builds writes .class files to a different directory from the .java files, use the two-argument form. The first argument is the destination root, an absolute path beneath which your package .class files are written. The second argument is the source root, an absolute path beneath which your package sources reside.

If this makefile resides in /vobs/myvob/src/pkg1, along with files A.java and B.java, and you want the compiler to write the .class files into /vobs/deployvob/pkg1, use a function like this one:

ALL_CLASSES=$(javaclasses /vobs/deployvob, /vobs/myvob/src

This sets ALL_CLASSES to

/vobs/deployvob/pkg1/A.class /vobs/deployvob/pkg1/B.class

A Java compiler that is run from the source directory for Package1 can compile .java files stored in the source directory for Package2. clearmake can manage this process if the makefile can always be used to find a rule to build a .class target, regardless of where the target's .java file resides.

To this end, use suffix rules in your makefiles. For example:

.JAVAC:
.java.class:
        rm -f $@
        javac $<

When using .JAVAC, clearmake can use this type of rule to build any .class file and correctly set the value of built-in macros like $@ and $<.

For example, if this makefile and the A.java file reside in /vobs/myvob/src/pkg1, you can instruct the compiler to write the .class files to /vobs/deployvob by using a makefile fragment like this one:

.JAVAC:
DEPLOY_ROOT=/vobs/deployvob
  ALL_CLASSES=$(javaclasses $(DEPLOY_ROOT),/vobs/myvob/src)
  all: $(ALL_CLASSES)
.java.class:
        rm -f $@
        javac -d $(DEPLOY_ROOT) $<