Creating Java code for use inside and outside Eclipse plugins

Eclipse is very good at creating Eclipse plugins. No surprise there. It’s also very good at creating JAR files for use outside Eclipse. But if I want to write some Java code for use inside Eclipse and outside Eclipse, it’s not obvious what to do. So far I’ve found various different methods, all of which have pros and cons.

Method Pros Cons
Create single PDE project. Use in both a PDE and a JDT context, exporting as a JAR when necessary Works, mostly Slightly odd behaviour relating to the cleanliness of projects – sometimes it doesn’t seem to build the project properly when it’s used from another non-Eclipse project. I don’t know if that’s because I’ve added a PDE nature. Secondly, I need to make sure that I don’t use any Eclipse classes within the code
Create entirely independent Java projects. Use my source-code-management system to keep the source code synchronised, roughly, between the two. I think this is what the Eclipse designers probably expected. It’s always wise to go along with what the software designers expected, or you’ll run up against their invalid assumptions later on. Especially with Eclipse. Each time I change the code for the Java project, I have to go and mess about in Subversion to copy the code across to the plug-in project (or vice versa). Not practical at all.
Create a standalone Java project. Use PDE to “convert to plug-in project”. Irrelevant. Just converts one type into another – doesn’t allow the two to exist together.
Create Java project. Create JAR exporter. Export as JAR. Create “Plug-in project from existing JAR archive”. Resulting plug-in project works well. There is no way to automate the export as a JAR. I can make it a bit easier by saving the JAR’s manifest and its .jardesc file somewhere in subversion. But each time I change the original code, I’d have to right-click on the jardesc and select ‘build JAR’. This method might be viable if there were a way to export the JAR-production process as an Ant buildfile, so that I could build it each time I build the original Java project. Unfortunately, although that seems possible with some of Eclipse’s exporters, the JAR exporter doesn’t seem to be one of them.
Create Java project. Create plug-in project. Delete ‘bin’ folder. Create new linked folder pointing to the ‘bin’ directory of the original project. Doesn’t work. Eclipse gets confused that it doesn’t have to build any source code, and some features of the PDE seem to depend upon the presence of the source code rather than the binaries (such as the nice selection of classes within the plugin.xml and MANIFEST.MF editors).
Create Java project. Create plug-in project. Delete ’src’ folder. Create new linked folder pointing to the ’src’ directory of the original project. It works! Eclipse builds two copies of binaries. I don’t fully trust it to observe changes in the original source code and rebuild the binaries properly (I guess it ought to be OK – there’s no real reason why dependency tracking should work less well over linked folders). When debugging, I don’t trust it necessarily to open the source code properly. If it does, I seriously doubt that Subversion will allow me to work with the linked folder. Makes my projects non-mobile, since I have to include absolute file locations in them.

I’m a little suspicious of the first, most obvious, method but that’s what I’m going to stick with for now.

Leave a Reply