Copyright © 2010 Skyway Software
Abstract
This tutorial details the steps required to customize the scaffolding capabilities of MyEclipse for Spring by customizing the templates that are used for code generation.
Table of Contents
Scaffolding can produce a lot of beautiful source code and a working application very quickly, and it's a very 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, 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.
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:
the name of the service
the date the service was created/generated
a copyright notice
the developer name
an Eclipse task for the developer to enter their name
The prerequisites needed to complete this tutorial are:
Download MyEclipse for Spring 8.6
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.
From the Eclipse Wizard selection menu (File-->New-->Other), select the MyEclipse for Spring Customization Project wizard (under MyEclipse customization folder). Click Next.
From the MyEclipse for Spring Customization Project wizard, enter AcmeCustomizations as the project name. Click Finish.
The customization project is now ready to use. In the next section we will customize the templates in the project.
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.
Expand the customization project in the Package Explorer. The code generation templates are grouped into three folders: staticSourceContent, staticWebContent, and templates.
The bulk of the templates are in the templates folder. Expand the templates folder, and open the template called Service.jet.
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.
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 //*********************************************************
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.
That is all that's required to customize a template. The next step is to specify where the new customization project should be used.
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.
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).
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.
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.
Thank you for you interest in MyEclipse for Spring. If you are interested in learning more, the following developer resources are available:
Developer Resources
Reference - Eclipse Help (MyEclipse for Spring 8.6 --> MyEclipse for Spring Reference) or Education Materials (online)
JET Tag Reference - JET tag library reference and Skyway JET tag library reference
Tutorials - Eclipse Help (MyEclipse for Spring 8.6 --> Tutorials) or Education Materials (online)
Screencasts - MyEclipse for Spring YouTube Channel (online)
Support Forums - MyEclipse for Spring Forums (online)
Blog - MyEclipse for Spring Posts (online)