Compiling Java in Gulp – Fun Experience Chasing Plugin Issues

Working on the Qortoba project, I needed to add a Java tool for generating JavaScript classes from a Java interface. The need to compile a couple of java source files did not warrant  incorporating Maven or Ant into the project, and a shell script sounded too archaic.

Since I was already using gulp, it made sense to search for a plugin that does the job. Indeed, I found ‘gulp-javac’ which seemed simple enough to adopt. However, after installing with npm and trying to run it I found that it uses some ES6 features, in particular the dot dot dot ‘spread’ operator. I looked up the version of node vs. ES6 support and decided to install the latest (why not!).

Installing the new version with nvm with a breeze. However, after I switched the node version gulp was not found, even after I reinstalled it globally. I figured maybe there is some difference in how the new npm organizes packages, so I deleted the npm_modules folder and reinstalled.

Now, I was getting an error where the ‘addLibraries’ method was not found on the javac gulp task. It was not clear why, so I tried a few things. I replaced the short version of the command (which combines ‘javac’ and ‘jar’) with the long form, and this solved the ‘addLibraries’ problem.

But then I started getting a “file not found” error which seems to have resulted from failure to create a tmp directory. Looked inside the code and found that it uses a some package called ‘tmp’ for managing temporary files and folders. The ‘dirSync’ method that was used was being fed a parameter that allows it to delete the tmp folder on exit even if it was not empty. I removed that parameter in the source and got the same error. I created a two-liner test for the directory creation and I got the same result. It turns out that the ‘tmp’ package deletes folders upon exist anyways, unless you pass it a ‘keep’ parameter. Setting this parameter to true made the two-liner work but not the javac task!

At this point I decided to file an issue on github, and to my delight I got a quick reply indicating that the author found a bug with having the jar file created under any path that contains slashes! The bug fix solved my issue, and I then filed another issue for the ‘addLibraries’ mis-documentation and that too was fixed within hours.