Wiki source code of Custom Project Templates

Last modified by Bastian Triller on 2009/04/11 21:03

Hide last authors
David Avendasora 60.1 1 {{toc/}}
David Avendasora 20.1 2
David Holt 38.1 3 = Introduction =
4
David Avendasora 60.1 5 WOLips 3.3+ supports the definition of custom project templates. These templates support creating new types of WebObjects projects beyond the ones that ship with WOLips.
David Holt 38.1 6
David Avendasora 44.1 7 To use a custom project template, create a new project by going to the "File" menu and selecting "New -> Other..." then select the "WO Project from Template" option under the WOLips section.
David Avendasora 60.1 8 [[image:attach:NewProjectFromTemplate.png]]
David Avendasora 20.1 9
David Avendasora 44.1 10 = Creating a Template =
David Avendasora 20.1 11
David Avendasora 60.1 12 Templates are basically like a regular project folder, but some of the contents are modified to use the variables defined in the template.xml file to change their names or values in their contents. The modified files within the template are processed using the [[Velocity Template Engine>>url:http://velocity.apache.org/engine/devel/vtl-reference-guide.html||shape="rect"]].
Pascal Robert 22.1 13
David Avendasora 44.1 14 The declaration of templates is very simple. The template system looks for folders in the following locations (in the following order):
David Holt 38.1 15
David Avendasora 44.1 16 1. The ProjectTemplates folder inside of the WOLips templateengine plugin jar
17 1. /Library/Application Support/WOLips/Project Templates
David Avendasora 60.1 18 1. YourHomeDir\Documents and Settings\Application Data\WOLips\Project Templates
19 1. YourHomeDir\Documents and Settings\AppData\Local\WOLips\Project Templates
20 1. ~~/Library/Application Support/WOLips/Project Templates
David Holt 38.1 21
David Avendasora 44.1 22 {{note}}
David Avendasora 60.1 23 Project templates found later in the list of locations above will override those of the same name from earlier in the list. For instance, a "Wonder Application" template found in ~~/Library will override a "Wonder Application" template from /Library.
David Avendasora 44.1 24 {{/note}}
David Avendasora 20.1 25
David Avendasora 44.1 26 The name that the template shows up as in the New Project wizard is controlled by one of two things:
David Avendasora 20.1 27
David Avendasora 44.1 28 1. The template's folder name
29 1. The name attribute of the template tag in the template.xml file inside the template folder (if present, this overrides the value set by the template folder name).
David Avendasora 20.1 30
David Avendasora 60.1 31 For instance, if you create a folder named "~~/Library/Application Support/WOLips/Project Templates/Wonder Application," the template system will offer a template named "Wonder Application" in the template selection dialog. If you then set the name attribute to "My Wonder Application", that is what you will see in the dialog.
David Avendasora 20.1 32
Bastian Triller 62.1 33 If you want to look at the current templates that ship with WOLips, the best way to analyse them is by [[importing the source>>doc:WOL.Home.Building WOLips.WebHome]] of WOLips from Subversion. The templates are located in //woproject/wolips/core/plugins/org.objectstyle.wolips.templateengine/ProjectTemplates//.
David Avendasora 44.1 34
David Avendasora 60.1 35 Here is an additional custom template for creating [[Wonder ERD2W Applications>>attach:WOL.Custom Project Templates@Wonder ERD2W Application.zip]] that you can view as an example.
David Avendasora 44.1 36
37 {{note}}
David Avendasora 60.1 38 Remember, if you copy an existing template to test it out you must rename both the folder **and** the name attribute of the template tag in the template.xml file otherwise whichever loads last will replace the first!
David Avendasora 44.1 39 {{/note}}
40
David Holt 38.1 41 After creating a template folder, you can create a hierarchy of files and folders within that folder. When you create a project using this template, a copy of all of the files and folders in your template will be used to create the new project. It is up to you to declare //all// of the files within a project, including Eclipse project metadata files like .classpath. You can refer to the built-in project templates as a starting point for creating your own custom templates.
David Avendasora 20.1 42
David Avendasora 60.1 43 That's it! For creating static boilerplate templates, you're done.
David Holt 38.1 44
45 = Template Metadata and Template Inputs =
46
David Avendasora 20.1 47 For a static template, the simple process above is enough. However, it's a common requirement to have configuration options for project templates. The WOLips project template engine provides an easy way to declare these options.
48
David Avendasora 60.1 49 After creating a template using the directions in the above section, you can additionally create a file named "template.xml" inside your project template folder. For instance, in the example above, you would create the file "~~/Library/Application Support/WOLips/Project Templates/My Application Template/template.xml".
David Avendasora 20.1 50
51 An example template.xml is below:
52
David Avendasora 60.1 53 {{code 0="xml"}}
David Holt 38.1 54
55 <?xml version="1.0" encoding="UTF-8"?>
David Avendasora 60.1 56 <template name = "My Application">
David Avendasora 20.1 57 <inputs>
David Avendasora 60.1 58 <input name = "basePackage" type = "Package">
59 <question>Base Package?</question>
60 <default>your.app</default>
61 </input>
62 <input name = "componentsPackage" type = "Package">
63 <question>Components Package?</question>
64 <default>your.app.components</default>
65 </input>
David Avendasora 58.1 66 <input name = "servletDeployment" type = "Boolean">
David Avendasora 60.1 67 <question>Deploy to Servlet Container?</question>
David Avendasora 54.1 68 <default>false</default>
David Avendasora 44.1 69 </input>
David Avendasora 58.1 70 <input name = "webXML" type = "Boolean">
71 <question>Autogenerate web.xml file?</question>
72 <default>false</default>
73 </input>
David Avendasora 20.1 74 <input name = "YourFavoriteColor" type = "String">
75 <question>Your Favorite Color?</question>
76 <options>
77 <option name = "Red" value = "#FF0000"/>
78 <option name = "Green" value = "#00FF00"/>
79 <option name = "Blue" value = "#0000FF"/>
80 </options>
81 <default>#FF0000</default>
82 </input>
83 </inputs>
84 </template>
85
David Holt 38.1 86 {{/code}}
87
David Avendasora 60.1 88 If you don't set a "name" attribute in the "template" tag, the name of the Template Folder will be used. In this example the Template will show up as "My Application" in the list of available templates. If "name" handn't been defined, the it would show up as the folder name, which was "My Application Template".
David Avendasora 20.1 89
David Avendasora 60.1 90 Within a template, you can declare a single "inputs" node that can contain multiple "input" nodes. Each input node corresponds to a variable that will be presented to the user on the second page of the wizard. Each input specifies a "name" attribute, which will become the variable name of the input for later reference in the Velocity templates; and a "type" attribute which will determine the type of control presented to the user. They are:
David Avendasora 20.1 91
David Avendasora 60.1 92 * Boolean -> Check Box
93 * String -> Text Field
94 * Package -> Text Field
95 * Integer -> Spinner
David Holt 38.1 96
David Avendasora 60.1 97 Each input also contains a "question" node, whose value corresponds to the label of the control when displayed to the user. In the above example, the "servletDeployment" will display a checkbox to the user with the label "Deploy to Servlet Container?". Additionally, you can provide a "default" node that defines the default value of the variable. If a default is not specified, the default value will be null for all input types.
David Avendasora 20.1 98
David Avendasora 60.1 99 The package type is slight extension to the String type. For a variable declared as type Package, in addition to having your variable bound, you will also have a variable named "yourvariablename_folder" with replaces dots for slashes. For instance, if your variable is named "basePackage" you will also get a variable named "$basePackage_folder". If the user left the default of "your.app" in the basePackage field, then the two variables would be:
100
101 * basePackage = "your.app"
102 * basePackage_folder = "your/app"
103
104 Finally, the input system supports the declaration of enumerated types. By declaring an "options" node that contains an ordered set of "option" nodes, you can define the possible values that the user can provide. In the above example, the "YourFavoriteColor" input defines three options: Red, Green, and Blue. Each option node has a "name" attribute, which will be the value displayed to the user, and a "value" attribute, which will be the actual backing value of the selection. The value of the option should be of the type specified in the "type" attribute of the input. For instance, if you declare the input type to be "Integer," your option values should be integer values (in quotes).
105
David Holt 38.1 106 = Using Template Inputs =
David Avendasora 20.1 107
David Avendasora 60.1 108 So now that you have template inputs defined, you need to be able to use them. The name used in the "name" attribute of your input node will be the name of the variable you can use in your template's files. For instance, in the example above, the Velocity variable "servletDeployment" will be bound to the boolean value corresponding to the user's selection, and can be used just like any other velocity variable. The Apache project provides a [[Velocity reference guide>>url:http://velocity.apache.org/engine/devel/vtl-reference-guide.html||shape="rect"]].
David Avendasora 20.1 109
David Avendasora 60.1 110 As an example, the .classpath file can be modified to automatically add a link to the JavaWOJSPServlet.framework if the user selects "Deploy to Servlet Container" in the the wizard.
David Avendasora 20.1 111
David Avendasora 60.1 112 {{code 0="xml"}}
David Holt 38.1 113
114 <?xml version="1.0" encoding="UTF-8"?>
David Avendasora 56.1 115 <classpath>
116 <classpathentry kind="src" path="Sources"/>
117 <classpathentry kind="con" path="WOFramework/JavaEOAccess"/>
118 <classpathentry kind="con" path="WOFramework/JavaEOControl"/>
119 <classpathentry kind="con" path="WOFramework/JavaFoundation"/>
120 <classpathentry kind="con" path="WOFramework/JavaJDBCAdaptor"/>
121 <classpathentry kind="con" path="WOFramework/JavaWebObjects"/>
122 <classpathentry kind="con" path="WOFramework/JavaXML"/>
123 <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
124 #if ($servletDeployment)
125 <classpathentry kind="con" path="WOFramework/JavaWOJSPServlet"/>
126 #end
127 <classpathentry kind="output" path="bin"/>
128 </classpath>
David Avendasora 20.1 129
David Holt 38.1 130 {{/code}}
131
David Avendasora 60.1 132 In addition to variables inside of Velocity templates, you can also use template variables in folder and file names. However, because $ is not allowed on some filesystems, we instead surround the variable names with "~_~_" (for instance $someVariable would be "~_~_someVariable~_~_" in the filename or path). As an example, the template above has an input named "basePackage" (of type Package) which, as we learned above, creates two variables: "basePackage" and "basePackage_folder". Since we are going to be using this variable to name (and define the path) of a folder in the Sources directory, we need to use the alternate version of the variable. Create a new folder in the Sources directory of the project templaet and instead of using "${basePackage_folder}" as the file name, we must use "~_~_basePackage_folder~_~_".
David Holt 38.1 133
David Avendasora 60.1 134 = Special Circumstances =
135
136 ===== Flagging files to be skipped by Velocity =====
137
138 If there are files in your template that should not be processed by Velocity as the project is being setup, you will need to end the file name with "~_~_binary" which will cause Velocity to strip the "~_~_binary" off the file name, but skip processing the contents of the file. Examples of files that you'd want to flag are:
139
140 * EOGenerator Templates
141 * Custom builder .launch files.
142
143 ===== Using keypaths =====
144
145 If you need to call a method on a given variable, you need to include the "()" at the end of the method name. An eample is in the build.properties file. In order to get the lowercase version of the project name, you have to call "${projectName.toLowerCase()}"
146
147 Happy templating!