facebook

Customizing Spring Code Generation

Users of scaffolding find themselves frequently making the same modifications to generated code. The modifications are related to personal style/preference, project-specific requirements, or company standards. This tutorial demonstrates customizing code-generation templates. You will learn how to:

  • Create a customization project
  • Modify an existing template to include a custom comment
  • Use tags from the JET and Skyway tag library
  • Configure Projects to Use the Customization
Customizing Spring code generation requires a MyEclipse Spring or Bling subscription.


1. Create a Customization Project

In MyEclipse with Spring support you can create a Customization project for customizing a full set of code generation templates.  This standard Eclipse project contains all available JET templates. By default the scaffolding functionality uses the standard templates residing 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 templates.

  1. Select File>New>Other, expand MyEclipse Customization, and select MyEclipse for Spring Customization Project. Click Next.

    Creating a new Customization project
  2. Type AcmeCustomizations as the project name, and click Finish.

    Naming the project


2. Customize Templates

Almost all generated Spring scaffolding artifacts originate from templates 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).

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


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


    JET template for generating Spring services
    A template has three major sections:

    JET directive for compiling the template
    – A JET template is compiled into Java code. The first JET directive specifies the compile parameters(i.e., package name and class name).

    JET directives for importing JET taglibraries
    – 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 usestag libraries 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.

    JET template layout
  3.  Add the following block to the template. It should be added immediately after line 10 (<%taglibprefix=”jaxws”…. %>).
    <%@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) 2013  Acme Corporation
    //*********************************************************

    Review the template customizations. The updated template will 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 is to include the developer’s name in the comment block. Since the  developer’s name can’t be derived dynamically, an Eclipse TODO task is emitted into the source code to  remind the developer that it needs to be set. Here are 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 the comment block using the org.skyway.integration.java.spring.skywaySpringCodeGenTags tag.

    Updated template


    3. Configure Projects to Use the Customization

    With a customization project and a customized template, the next step is to scaffold an  application using the new customization project. To use the customization project, a reference  to the project must be added to the customization property of the project you are scaffolding.

    1. Right-click the project for which you want to use the template, and select Properties.
    2. Expand MyEclipse, and select Customization.
    3. Click Add, and select the AcmeCustomizations project.

      Adding a reference to the Customization project


    4. Scaffold with Customized Templates

    After running CRUD scaffolding, open one of the generated service implementation classes. As you can see from the following image, 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.

    Reviewing generated Spring implementation class