2. Customizing Code Generation Templates

PROBLEM

While Skyway Builder provides you with a fully-implemented set of SpringSource-certified Spring MVC templates, there may be occasions where you want to customize the code generation templates to match custom requirements.

SOLUTION

The standard Spring templates included with Skyway Builder can be used as-is, or they can be adapted to meet custom generation requirements. Application development projects will by default use the standard templates from the Skyway plugins for Eclipse. All versions of Skyway Builder support the customization of code generation templates by extending the code generation framework using custom Eclipse plugins. Skyway Builder Professional Edition also supports the customization of code generation templates using a Skyway Template Project which doesn't require Eclipse plugin development. A Spring DSL project can be easily configured to use a Skyway Template Project (in the Eclipse workspace) as the source for the templates.

The benefits of using the Skyway Template Project are:

  1. Any developer can customize application templates – Eclipse plugin development experience is not required. The only skillset required to customize templates is is JET, which is very similar to JSP.

  2. Customized templates can be used on a project by project basis – If needed, each project can have a distinct set of templates.

  3. Customized templates can be shared across projects – As templates are adapted to conform to company standards and desired implementations, they can be shared across development projects.

  4. Customizations can be version controlled like standard Eclipse projects – The templates that were used to generate the code and configurations can be version controlled with the application.

  5. Customization of code generation for all Skyway Builder editions – Each edition of Skyway Builder adds additional code generation functionality, and all templates for all versions can be customized.

HOW IT WORKS

The first step is to create a new Skyway Template Project. You will be prompted for a name (i.e. MyCustomSpringGenTemplates) and a template source project. The name is self explanatory, but the template source project needs a little bit of explanation. When you create a template project, it will be seeded with the code generation templates (JET) from whichever project you specify as the template source project. You can specify any pre-existing Spring DSL project as the template source project, and all the templates that were used for generation of the Spring MVC application will be copied into the new Skyway Template Project. Now that you have a template project with a copy of the templates, you can customize the templates.

The templates are implemented using JET, a template engine from the Eclipse M2T project for transforming meta-data/models to concrete code using an approach similar to JavaServer Pages (JSP). By using a standard and easy-to-use template technology, the Spring templates are easy to customize and adapt to different implementation requirements. If you open the MyTemplates project, you will see a bunch of JET templates. Just about anything that is generated by Skyway Builder has a JET template or set of JET templates that define the outputs from the Spring DSL. JET templates resemble JSP in many ways, making it very easy to follow the implementation of the templates. JET supports the concept of tags and tag libraries (just like JSP), and the templates use many of the standard JET tags. The JET tag library is very rich in generation functionality, but you can also use custom tag libraries. Skyway Builder contributes a set of JET tag libraries for simplifying many of the recurring generation requirements of Spring applications.

Once you've customized ther templates, the next step is to configure a Spring DSL project to use the templates in the new Template Project instead of the default templates. This is done by opening the Spring DSL editor, switching to the Code Generation tab, and configuring the new template project (i.e. MyCustomSpringGenTemplates) for the Location of Customized Templates. Go ahead and delete all of the folders in the generated folder, and do a clean generation (Project–>Clean). The generated code should reflect the updates to the templates.