|
|
Does anyone know if it's possible to define and use custom project templates with the New Template Project wizard in STS? And if so, how?
I've spent a bit of time trying to figure out how to do this (or if it's even possible) and I'm not getting anywhere. Searching isn't turning up much either.
Any help is appreciated.
Hi Kent!
Unfortunately we don't have a good documentation on that at the moment, but we are working on that. I will let you know when there is something for you to try, ok?
Cheers,
-Martin
Thanks, Martin. When you say you're working on it- you're working on adding this feature? Or working on documenting it?
This should already be possible, I am currently working on a small example that describes how to do this. Stay tuned...
Custom Project Templates in STS
STS provides the New Spring Template Project wizard. Uses this wizard, the user can see a number of project templates, choose one and let the wizard create a complete project, based on that template description.
In order to create your own project templates for STS, you need to do the following at the moment:
1. Build a new bundle that contributes a new resources extension to STS
STS creates the list of available project templates by downloading and reading resource XML files. To let STS know where those resource files are located, you need to contribute them to STS via an extension.
So the first step is to create a new and empty plugin project. You don't need Java code in there, so don't create this as a Java project. Just create a new plugin project, deselect the Java options in the wizard and you are done.
Now define the extension within that plugin project. Create a plugin.xml by selecting the extension tab, when you have the manifest editor open (double clicking on the META-INF/MANIFEST.MF). You define the extension my modifying the plugin.xml to look something like this:Code:
lt;?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?gt;
lt;?eclipse version=quot;3.4quot;?gt;
lt;plugingt;
lt;extension point=quot;com..sts.core.resourcesquot;gt; lt;uclid=quot;content.descriptorsquot;value=quot;%content.descriptor.uclsquot;gt; lt;/uclgt; lt;/extensiongt;
lt;/plugingt;
This defines a contribution to the STS extension point quot;com..sts.core.resourcesquot; and overrides the default value for the quot;content.descriptorsquot; id with your new value. This new value should contain all the ucls from all templates you would like to see within the STS wizard. In this case, we don't put the ucls in here directly, but we externalize them into a separate properties file. THIS IS NEEDED for multiple ucls, as you will see.
The next step is to create a properties file called quot;bundle.propertiesquot; inside your plugin, and you should put that into a directory called quot;OSGI-INF/I10n/quot;. This properties file should look like this:Code:
content.descriptor.ucls = release...riptors.xml\n\
file--/Users/mlippert/template-contribution/descriptor.xml
As you can see, I put the original STS value in there to keep the STS default templates available and added my own ucl to my (in this case local) descriptor.xml file. ´so if you would like to use just a local file, you can go ahead with this approach. In the case where you would like to distribute your new template across your company, having the descriptor.xml file being served as a from resources would make sense. Please note that the ucls are separated by a quot;\nquot;.
Now you are pretty much done with the new plugin for STS. You can export that and put the resulting JAR file into the quot;dropinsquot; directory of your STS installation. Take care that the build.properties have META-INF, OSGI-INF, and plugin.xml checked to be included for the binary build.
(I attached an example plugin project for this, take a look at the attachments.)
2. Build the descriptor.xml file
The ucl that you added to the STS extension in the previous step points to a descriptor.xml file. This file should look like this:Code:
lt;?xml version=quot;1.0quot; encoding=quot;UTF-8quot; standalone=quot;noquot;?gt;
lt;descriptorsgt; lt;descriptor id=quot;org.test.mynewtemplatequot; kind=quot;templatequot; local=quot;falsequot; name=quot;Mein neues Templatequot; size=quot;998877quot; ucl=quot;file--/Users/mlippert/template-contribution/test-template-1.0.0.zipquot; version=quot;1.0.0quot;gt; lt;descriptiongt;My brand new template contribution for this hacky stuff etc.lt;/descriptiongt; lt;/descriptorgt;
lt;/descriptorsgt;
As you can see, I put some random stupid examples in there, you should replace that with your own ids. Please choose:
your own ID for the template (quot;org.test.mynewtemplatequot; in the example)
your own name for the template (quot;Mein neues Templatequot;), this will show up in the list of the wizard
your own ucl to point to the ZIP file for the template
don't forget to set the correct size of the ZIP file in the quot;sidequot; attribute
description: some more text to tell the user what your template is all about
3. Build the real template
Now you need to create that ZIP file that we referenced from the descriptor.xml file. This ZIP file should contain three files in its root directory:
template.xml
template.zip
wizard.json
Lets take a look at the first one, template.xml:Code:
lt;templategt;
lt;descriptor id=quot;org.test.mynewtemplatequot;
name=quot;Mein neues Templatequot;
kind=quot;templatequot;
version=quot;1.0.0quot;gt;
lt;descriptiongt;
My brand new template contribution for this hacky stuff
lt;/descriptiongt;
lt;/descriptorgt;
lt;project path=quot;template.zipquot;/gt;
lt;json path=quot;wizard.jsonquot;/gt;
lt;/templategt;
It contains the same id, name, and description for the template than the descriptor.xml, also the same version and the kind quot;templatequot;. In addition to that it also contains the reference to the template.zip file and the wizard.json file. So this is the place where you define those two other files.
The wizard.json file contains the description that is used to create and handle the UI of the wizard for this template specifically. In my example the wizard.json file looks like this:Code:
{
quot;infoquot; : {
quot;elementsquot; : {
quot;elementquot; :[
{
quot;namequot; : quot;topLevelPackagequot;,
quot;descriptionquot; : quot lease specify the top-level package e.g. com.mycompany.myappquot;,
quot;typequot; : quot;java.lang.Stringquot;,
quot;pagequot; : 0,
quot;requiredquot; : true,
quot;patternquot; : quot;\\w+\\.\\w+(\\.\\w+)+quot;,
quot;replaceKindquot; : quot;topLevelPackagequot;
}
]
},
quot;topLevelPackagequot; : quot;mytld.mycompany.myapp.mysubsystemquot;,
quot;projectNamequot; : quot;projectNamequot;,
quot;pagesquot; : {
quot;pagequot; : [
{
quot;orderquot; : 0,
quot;descriptionquot; : quot roject Settingsquot;
}
]
}
}
}
I don't go into the details here, but this is basically the information for the wizard UI. It contains the elements that the user can type in within the wizard and that are used to create the actual project (for example the top level packages).
The last part is the actual project that you would like to instantiate if the user chooses your template. All you need to do here is to create the project yourself and name the top-level packages the way you defined it in the wizard.json (in this case mytld.mycompany.myapp.mysubsystem). In the example that I attached to this the template project resides in the root directory called quot;templatequot; and contains:
.classpath
.project
.settings
.springBeans
pom.xml
src directory containing the project layout and some predefined source code
That's it!
Please note: Once you have downloaded the zip file of your new template into STS (this is done automatically after you have chosen it from the list and pressed return), STS will re-use the downloaded file the next time. So if you chance your template, take care to set a newer version number to it. Otherwise STS will not pick-up your changed one. |
|