How do you ensure each developer in your team can deliver Java code in the same format (indentation, line length, code blocks….)? Well, you of course know Eclipse preference is the place to go. You can easily import a code template and formatter. But there are still problems:
- For some Preference items, such as Save Actions and Organize Import, etc, you have to manually configure them because import/export is not explicitly supported by Eclipse; manual editing is error prone, especially where you have a big team.
- Even with imported Code Templates and Formatter, how do you ensure a developer will not accidentally modify it? In such case, this developer could produce code with inconsistent format which will pollute the diff and code review.
Here comes the Eclipse Workspace Mechanic to the rescue. This plugin, on Eclipse start up, automatically checks the current preferences setting and notifies you if there is any discrepancy. Then after, it periodically checks the conformation to ensure any preference change during the period does not take effect unnoticeably. Once noticed, you can choose to fix the unwanted change or leave it along.
You can find the installation and usage details of the plugin here. But you may still be not very clear about how to create tasks and what type of task (LASTMOD or RECONCILE) to record. Here are two of my use case for your reference:
- Convert the existing Google Code Style to a Workspace Mechanic task file (epf format): Assume you already use the Google Code Style as your formatter in Eclipse, you can do the following (1) Go to Window->Preferences->java->Code Style->Formatter and change the Active Profile to any other format and apply by click OK; (2) Go to the Eclipse lower right corner and right click the Workspace Mechanic icon->Preference Recorder->Start Recording; (3) Open the Formatter preference again and change it back to the GoogleStyle and click OK; (4) Workspace Mechanic icon->Preference Recorder->Stop Recording; (5) Enter anything meaningful to you into the Title and description, be sure to select Reconcile as the Task Type and save to your home directory/.eclipse/mechanic/;(6)Restart Eclipse and you are done.
- Create a task for customized Save Actions. To make sure the team have consistent save actions, the following steps are recommended. (1)Go to Window->Preferences->java->Editor->Save Actions, Restore Defaults and click OK; (2) Start to record preference (see above); (3)Open the Save Actions again and configure it to meet your requirement and apply; (4)Stop recording; (5)Save the task in Reconcile style; (6)Restart Eclipse.