Tuesday, August 5, 2008

Customizing Builds for Your Eclipse Projects

I was answering a question on the Eclipse newsgroups today, a question that I must have answered at least 20 times in the past couple of years. The question was, basically, "How can I add additional steps to a project's build?" In my continuing (seemingly futile) efforts to make more use of blogging, I decided to put my explanation here.

The Java Project build really only does two things: it compiles source .java files into .class files into your project's Build Output location, and it copies any non-.java files it finds in your Source Locations to the corresponding place in your Build Output location (Source Locations and Build Output Locations are configured in the Java Build Path section of the project properties). If you want or need to do more during the builds, you can augment it with your own Ant script.
Open the Properties dialog for your project and select the Builders section. There you can add your own builder to run after the Java Builder.

You can specify your builder as Ant or any arbitrary program. For many typical tasks (such as copying/moving files, creating a JAR, signing it, etc.) I usually recommend Ant because it makes such tasks very easy to implement. There are more details available in the Ant Tutorial section of the Eclipse Help.

Be aware that all custom Builders will be executed for every build, which by default happens every time you save a source file (or something that the project depends on gets built). So having a Builder that takes more than a few seconds to start up and execute can be a real drag your the coding flow. If you find that you don't really want it to run on every build, I recommend opening the Ant View (I like to make it a Fast View to tuck it out of the way but still easily accessible) and adding your Ant script there. That way it is only a couple of clicks away at all times, easy to manually invoke at the times when you need it.