Wiki source code of Custom Project Templates

Version 43.1 by Pascal Robert on 2007/12/17 13:40

Hide last authors
David Holt 38.1 1 {{toc}}{{/toc}}
David Avendasora 20.1 2
David Holt 38.1 3 = Introduction =
4
5 WOLips 3.3 supports the definition of custom project templates. These templates support the declaration of custom configuration parameters and the files within the template are processed using the [[Velocity Template Engine>>http://velocity.apache.org/engine/devel/vtl-reference-guide.html]].
6
Pascal Robert 22.1 7 To use a custom project template, select the "WO Project from Template" option under the WOLips category of the New Project Wizard.
David Avendasora 20.1 8
David Holt 38.1 9 {{info}}
David Avendasora 20.1 10
David Holt 38.1 11 !NewProjectFromTemplate.png!
Pascal Robert 22.1 12
David Holt 38.1 13 {{/info}}
14
15 = Creating a Template =
16
David Avendasora 20.1 17 The declaration of templates is very simple. The template system looks for folders in the following locations (in the following order):
18
Pascal Robert 22.1 19 * The ProjectTemplates folder inside of the WOLips templateengine plugin jar
20 * /Library/Application Support/WOLips/Project Templates
Mike Schrag 34.1 21 * YourHomeDirocuments and Settingspplication DataOLipsroject Templates
22 * YourHomeDirocuments and SettingsppDataocalOLipsroject Templates
David Holt 38.1 23 * /Library/Application Support/WOLips/Project Templates
David Avendasora 20.1 24
David Holt 38.1 25 If you want to look at the current templates that ships with WOLips, the best way to analyse them is by [[importing the source>>Building WOLips]] of WOLips from Subversion.  The templates are located in //woproject/wolips/core/plugins/org.objectstyle.wolips.templateengine/ProjectTemplates//.
David Avendasora 20.1 26
David Holt 38.1 27 Without any additional metadata, the template system derives the name of the template from the name of the folder in one of the above locations. 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. 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 overrode a "Wonder Application" template from /Library.,,
David Avendasora 20.1 28
David Holt 38.1 29 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 30
David Holt 38.1 31 That's it For creating static boilerplate templates, you're done.
32
33 = Template Metadata and Template Inputs =
34
David Avendasora 20.1 35 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.
36
David Holt 38.1 37 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/Wonder Application/template.xml".,,
David Avendasora 20.1 38
39 An example template.xml is below:
40
David Holt 38.1 41 {{code value="xml"}}
42
43 <?xml version="1.0" encoding="UTF-8"?>
Pascal Robert 22.1 44 <template name = "Wonder Application">
David Avendasora 20.1 45 <inputs>
Pascal Robert 22.1 46 <input name = "linkToWonderProjects" type = "Boolean">
47 <question>Link to Wonder Projects?</question>
48 <default>false</default>
David Avendasora 20.1 49 </input>
Pascal Robert 22.1 50 <input name = "linkToWonderFrameworks" type = "Boolean">
51 <question>Link to Wonder Frameworks?</question>
52 <default>true</default>
David Avendasora 20.1 53 </input>
54 <input name = "YourFavoriteColor" type = "String">
55 <question>Your Favorite Color?</question>
56 <options>
57 <option name = "Red" value = "#FF0000"/>
58 <option name = "Green" value = "#00FF00"/>
59 <option name = "Blue" value = "#0000FF"/>
60 </options>
61 <default>#FF0000</default>
62 </input>
63 </inputs>
64 </template>
65
David Holt 38.1 66 {{/code}}
67
Pascal Robert 22.1 68 The "name" attribute of the template node overrides the name of the folder the templates are in. For instance, you could have the above template.xml inside a folder named "Template 1" and the template system would consider the name of the template to be "Wonder Application."
David Avendasora 20.1 69
David Holt 38.1 70 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 can be one of Boolean, String, Package, or Integer. The type value determines the control that will be used to display the input to the user (String = text field, Boolean = checkbox, Integer = spinner, Package = text field, etc). 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 "linkToWonderFrameworks" will display a checkbox to the user with the label "Link to Wonder Frameworks?". 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 71
David Holt 38.1 72 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." This is useful because you can use template variables in folder names on the filesystem.
73
David Avendasora 20.1 74 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).
75
David Holt 38.1 76 = Using Template Inputs =
David Avendasora 20.1 77
Pascal Robert 22.1 78 So now that you have template input defined, you will want to be able to use them. The name use used in the "name" attribute of your input declaration will be the name of the variable in your Velocity context. For instance, in the example above, the Velocity variable "linkToWonderProjects" 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>>http://velocity.apache.org/engine/devel/vtl-reference-guide.html]].
David Avendasora 20.1 79
Pascal Robert 22.1 80 As an example, the Wonder Application template's .classpath file is defined as:
David Avendasora 20.1 81
David Holt 38.1 82 {{code value="xml"}}
83
84 <?xml version="1.0" encoding="UTF-8"?>
Pascal Robert 22.1 85 <classpath>
86 <classpathentry kind="src" path="Sources"/>
87 #if ($linkToWonderProjects)
88 <classpathentry combineaccessrules="false" kind="src" path="/ERJars"/>
89 <classpathentry combineaccessrules="false" kind="src" path="/ERExtensions"/>
90 <classpathentry combineaccessrules="false" kind="src" path="/ERPrototypes"/>
91 <classpathentry combineaccessrules="false" kind="src" path="/JavaWOExtensions"/>
92 #end
93 #if ($linkToWonderFrameworks)
David Holt 38.1 94 <classpathentry kind="con" path="org.objectstyle.wolips.WO_CLASSPATH/ERExtensions/ERJars/ERPrototypes/JavaWOExtensions/JavaEOAccess/JavaEOControl/JavaFoundation/JavaJDBCAdaptor/JavaWebObjects/JavaXML"/>
Pascal Robert 22.1 95 #else
David Holt 38.1 96 <classpathentry kind="con" path="org.objectstyle.wolips.WO_CLASSPATH/JavaEOAccess/JavaEOControl/JavaFoundation/JavaJDBCAdaptor/JavaWebObjects/JavaXML"/>
Pascal Robert 22.1 97 #end
David Holt 38.1 98 <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
Pascal Robert 22.1 99 <classpathentry kind="output" path="bin"/>
David Holt 38.1 100 </classpath>
David Avendasora 20.1 101
David Holt 38.1 102 {{/code}}
103
104 In addition to variables inside of Velocity templates, you can also use template inputs in folder 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 Wonder Application template has an input named "basePackage" (of type Package), which creates a magic variable named "basePackage//folder" (where the dots are turned into slashes), and the Source folder on the filesystem is named "Wonder Application/Sources///basePackage//folder//".
105
106 Happy templating