Sunday 19 June 2011

Building for Java 5 on Java 6


During some recent work in Apache Aries, I had a need to ship some of the modules to a client who is using Java 5. No problem on the surface since the modulers were already compiled to 1.5 binaries, were it not for the fact that compiling to 1.5 class files includes a full zero percent guarantee that the class files actually do not use JDK API introduced in Java 6. And lo and behold there were a couple of calls to String#isEmpty and the IOException constructors that take an Exception argument - plus ubiquitous use of @Override on methods inherited from interfaces (a feature allowed by the Java 6 compiler but not a Java 5 compiler).



Ordinarily, the solution in this kind of situation - after fixing all the JDK method calls and annotation usages - would be to build on Java 5. However, there are unfortunately modules in Apache Aries that genuinely require a Java 6 compiler and runtime. So this was not an option. Leaving things as they are on the other hand would mean that Java 5 compatibility would likely rot very quickly as JDK 6 calls get re-introduced.



Enter Animal Sniffer, a very nifty Maven plugin that performs an API usage check on compiled binaries against a library signature. With just a bit of Maven plugin setup as shown below this plugin can enforce usage of the Java 5 class library in the projects that are meant to be Java 5 compatible. Even better since we continue to compile on Java 6, we can also continue to use @Override in the Java 6 way since it's a compile time annotation. Lastly, Animal Sniffer is actually very fast, so the overhead for doing the check is minimal.


No comments:

Post a Comment