As much as Forge is a great tool - the code it generates is usually not in your preferred code styling. This plugin is intended to help you getting source code nice and readable after generation, in the way you want it.
The formatter plugin currently supports only Java source formatting. Other file formats might be added in a later version.
The formatter plugin is currently installable from this github repository. Start Forge and run
forge git-plugin git://github.com/thomashug/forge-formatter-plugin.git
Once you have the plugin installed you can simply execute
formatter ${path_to_file}.java
This will format your Java file based on the Sun coding conventions. You can also format a complete folder with
formatter ${path_to_parent_folder}
Or even more, format a folder recursively with
formatter ${path_to_parent_folder} --recursive
The Forge formatter plugin supports several predefined formatter settings. To use one of them, execute the formatter command with the following option:
formatter ${path_to_file}.java --configName [Sun|Eclipse|JBoss]
Using this option each time you want to format a file is quite inconvenient. You can configure your project to use a specific formatter profile every time you format a file with the following setup command:
formatter setup --configName [Sun|Eclipse|JBoss]
You can also use customer formatter settings by exporting a configuration from Eclipse. Save the XML under your project and use the following command to set it as default for your project:
formatter setup ${path_to_config}.xml
All following formattings will use the custom formatter config file.
Sometimes you spend a while formatting JavaDoc to reflect the HTML output. Then the formatter comes and screws it all up - something we want to spare you from. Disable comment formatting with the following flag:
formatter setup ... --skipComments # persist the setting
formatter --skipComments # one time skip on the formatter command
Create a new project:
[no project] projects $ new-project --named test
Create an Eclipse formatting file with your preferred coding style and copy it into your project root (or similar location):
[test] test $ ls
eclipse-formatter.xml pom.xml src/
[test] test $ formatter setup eclipse-formatter.xml
Wrote test/src/main/resources/META-INF/forge.xml
Your formatter settings get stored in the forge.xml config file. Create now e.g. a JPA entity and use the formatter:
[test] test $ persistence setup --provider HIBERNATE --container JBOSS_AS7
...
[test] test $ entity --named Test --package org.jboss.forge.model
Check the generated code:
[test] Test.java $ less .
And now format it:
[test] Test.java $ formatter .
This should match now with the formatting conditions you've put into the Eclipse formatter file.