MyEclipse Logo

MyEclipse for Spring 10.0: Customize Code Generation using JET Templates

Customize code generation using JET Templates

Niel Eyde

Skyway Software, Inc.

Table of Contents

1. Introduction
2. Goal
3. Prerequisites
4. Create MyEclipse for Spring Customization Project
5. Customize Templates
6. Configure projects to use customization project
7. Scaffold with customized templates
8. Conclusion
9. Additional Developer Resources

1. Introduction

Scaffolding can produce a lot of source code and a working application very quickly, and it's an extremely powerful tool in a developer's toolbox. While the generated source code may be based on industry best practices and certified by experts, developers understand that there are still many reasons why the generated code may need to be modified after scaffolding. The reason why so much emphasis is placed on generating high-quality well-formed source code in the first place is based on the recognition that developers will need to maintain the source code afterwards.

A developer that frequently uses scaffolding will find themselves regularly making the same modifications to the generated code. The modifications are likely related to personal style/preference, project-specific requirements, or company standards. Ideally there would be a way to customize the templates that are used by code generation to reduce the manual modifications required after scaffolding.

MyEclipse for Spring has a mechanism for customizing all the templates that are used for code generation. This gives MyEclipse for Spring users a lot of flexibility in adapting the code generation capabilities according to their project requirements and standards. Almost all of the Spring MVC, Spring Web Flow, JavaServer Pages (JSP), Google Web Toolkit (GWT), and Adobe Flex artifacts generated by MyEclipse for Spring originate from code generation templates. These templates are implemented using JET, a template engine from the Eclipse M2T project for transforming meta-data/models into concrete code using an approach similar to JavaServer Pages (JSP). By using an open standard and easy-to-use template technology, the templates are easy to customize and adapt to different implementation requirements.

This tutorial will walk you through customizing the code generation templates of MyEclipse for Spring.

2. Goal

While the opportunities provided by MyEclipse for Spring to customize code generation templates are endless, the goal of this tutorial is to introduce you to the customization process and walk you through a customization example, including:

  • creating a customization project

  • modifying a pre-existing template

  • using a tag from the JET tag library

  • using a tag from the Skyway JET tag library

The specific customization that's going to be implemented in this tutorial is to add a custom comment block to all generated Service implementations. The comment block will include:

  1. the name of the service

  2. the date the service was created/generated

  3. a copyright notice

  4. the developer name

  5. an Eclipse task for the developer to enter their name

3. Prerequisites

The prerequisites needed to complete this tutorial are:

4. Create MyEclipse for Spring Customization Project

In MyEclipse for Spring you can create a Customization Project for customizing a full set of code generation templates. The Customization Project is a standard Eclipse project that contains all of the available JET templates. By default the scaffolding functionality will use the standard templates that reside in the installed MyEclipse for Spring plugins, but the project can be easily configured to use one or more Customization Projects as the source for the templates.

  1. From the Eclipse Wizard selection menu (File-->New-->Other), select the MyEclipse for Spring Customization Project wizard (under MyEclipse customization folder). Click Next.

    Figure 1. Select Wizard: MyEclipse for Spring Customization Project


  2. From the MyEclipse for Spring Customization Project wizard, enter AcmeCustomizations as the project name. Click Finish.

    Figure 2. MyEclipse for Spring Customization Project Wizard


The customization project is now ready to use. In the next section we will customize the templates in the project.

5. Customize Templates

When a customization project is created, a copy of all the MyEclipse for Spring templates are included in the project. These templates can be edited with any text editor.

  1. Expand the customization project in the Package Explorer. The code generation templates are grouped into several folders.

    Figure 3. Template Folders


  2. The template for Spring services is located in the com.skyway.integration.service.webservice/templates folder. Open the template called Service.jet.

    Figure 4. JET template for generating Spring Services: Service.jet


  3. Before we start modifying the template, let's briefly review the template. A template has three major sections.

    • JET directive for compiling the template - A JET template will be compiled into Java code. The first JET directive specifies the compile parameters (i.e. package name and class name).

    • JET directives for importing JET tag libraries - JET tag libraries are very similar to JSP tag libraries, and each template must specify which tag libraries are going to be used. This particular template uses tag libraries that are provided by JET (org.eclipse.jet.*) and Skyway (org.skyway.integration.*).

    • Template body - This is the actual body of the template, and it's used to specify the generated output.

    Figure 5. JET Template Layout


  4. Add the following block to the template. It should be added immediately after the line 10 (<%taglib prefix="jaxws".... %>)

    Example 1. Comment block

    <%@taglib prefix="f" id="org.eclipse.jet.formatTags"%>
    //**********************************************************
    // <sw:javaType select="$model" />
    // 
    // Author: TODO: update developer's name in comment header
    // Creation date: <f:formatNow pattern="MM/dd/yyyy" />
    //
    // Copyright (C) 2010  Acme Corporation
    //*********************************************************
    
    

  5. Review the template customizations. The updated template is going to add a comment block to all generated Service implementation classes. The comment block includes static content (i.e. labels, copyright notice) and dynamic content (name, date). The goal was to also include the developer's name in the comment block. Since the developer's name can't be derived dynamically, an Eclipse TODO task is going to be emitted into the source code to remind the developer that it needs to be set. Here's a few things worth noting:

    • To include the current date in the header, the org.eclipse.jet.formatTags tag was included using a JET declaration.

    • The class name will be emitted into comment block using the org.skyway.integration.java.spring.skywaySpringCodeGenTags tag.

    Figure 6. Review updated JET template


That is all that's required to customize a template. The next step is to specify where the new customization project should be used.

6. Configure projects to use customization project

Now that a new customization project has been created and one of the existing templates has been customized, the next step is to scaffold an application using the new customization project. This tutorial isn't going to assume that you are already familiar with scaffolding. (If you need help scaffolding, please eee Additional Developer Resources). To use the customization project, a reference to the project must be added to the customization section of the project you're going to scaffold into.

  1. Under Project Properties, add a reference to the new customization project under MyEclipse-->Customizations.

    Figure 7. Add reference to customization project


7. Scaffold with customized templates

Finally it's time to run CRUD scaffolding to see the effect of the custom templates. The steps for scaffolding is beyond the scope of this tutorial. (If you need help scaffolding, please eee Additional Developer Resources).

  1. After running CRUD scaffolding, open one of the generated service implementations classes. As you can see from the following diagram, the comment block was generated into the class. The comment block includes the class name, creation date, copyright, and an Eclipse TODO task for reminding the developer to enter their name.

    Figure 8. Review generated Spring implemenation class


8. Conclusion

This was a very brief example on how to customize the CRUD scaffolding and code generation of MyEclipse for Spring. You are encouraged to explore the other templates that are available in a customization project. If you have any questions, please contact us using the MyEclipse for Spring forums (see Additional Developer Resources).

You may also want to try other tutorials which are available in the Eclipse help system and online (see Additional Developer Resources).

What was your experience with this tutorial? Share your experience with us by completing a very brief survey.

9. Additional Developer Resources

Thank you for you interest in MyEclipse for Spring. If you are interested in learning more, the following developer resources are available: