Custom Project Templates in SpringSource Tool Suite |
|

The SpringSource Tool Suite (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. While this gives you an easy way to create new projects, you might wanna define your own custom project templates for your team or your organization. And we will show you how to do this in the following.
1. Step: Contribute 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:
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension point="com.springsource.sts.core.resources">
<url
id="content.descriptors"
value="%content.descriptor.urls">
</url>
</extension>
</plugin>
This defines a contribution to the STS extension point com.springsource.sts.core.resources and overrides the default value for the content.descriptors id with your new value. This new value should contain all the URLs from all templates you would like to see within the STS wizard. In this case, we don't put the URLs in here directly, but we externalize them into a separate properties file. *THIS IS NEEDED* for multiple URLs, as you will see.
The next step is to create a properties file called bundle.properties inside your plugin, and you should put that into a directory called OSGI-INF/I10n/. This properties file should look like this:
content.descriptor.urls = http://dist.springsource.com/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 URL 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 http resources would make sense. Please note that the URLs are separated by a \n.
Now you are pretty much done with the new plugin for STS. You can export that and put the resulting JAR file into the dropins 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: template-contribution-plugin.zip)
2. Step: Build the descriptor.xml file
The URL that you added to the STS extension in the previous step points to a descriptor.xml file. This file should look like this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<descriptors>
<descriptor id="org.test.mynewtemplate" kind="template" local="false" name="Mein neues Template" size="998877" url="file:///Users/mlippert/template-contribution/test-template-1.0.0.zip" version="1.0.0">
<description>My brand new template contribution for this hacky stuff etc.</description>
</descriptor>
</descriptors>
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 (
org.test.mynewtemplatein the example) - your own name for the template ("Mein neues Template"), this will show up in the list of the wizard
- your own URL to point to the ZIP file for the template
- don't forget to set the correct size of the ZIP file in the
sideattribute - description: some more text to tell the user what your template is all about
3. Step: 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:
<template>
<descriptor id="org.test.mynewtemplate"
name="Mein neues Template"
kind="template"
version="1.0.0">
<description>My brand new template contribution for this hacky stuff</description>
</descriptor>
<project path="template.zip"/>
<json path="wizard.json"/>
</template>
It contains the same id, name, and description for the template than the descriptor.xml, also the same version and the kind template. 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:
{
"info" : {
"elements" : {
"element" : [
{
"name" : "topLevelPackage",
"description" : "Please specify the top-level package e.g. com.mycompany.myapp",
"type" : "java.lang.String",
"page" : 0,
"required" : true,
"pattern" : "\\w+\\.\\w+(\\.\\w+)+",
"replaceKind" : "topLevelPackage"
}
]
},
"topLevelPackage" : "mytld.mycompany.myapp.mysubsystem",
"projectName" : "projectName",
"pages" : {
"page" : [
{
"order" : 0,
"description" : "Project Settings"
}
]
}
}
}
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 template and contains:
- .classpath
- .project
- .settings
- .springBeans
- pom.xml
- src directory containing the project layout and some predefined source code
Here is an example for this: template-contribution.zip.
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.
Similar Posts
- Maven PAR Plugin 1.0.0.M1
- Community-Driven Spring Integration Extensions
- Simple templates for the Scripted Editor
- Create Spring Integration Projects using STS
- Using an OSGi Profile with Bundlor





Chuck S says:
Added on February 24th, 2011 at 5:19 pmok that's nice. would you guys support helios already?!
Martin Lippert (blog author) says:
Added on February 25th, 2011 at 4:53 amHey Chuck!
We are supporting Eclipse Helios already for quite some time. The latest release STS 2.5.2 ships based on Eclipse 3.6.1 and the update sites are there for 3.6 as well. Or do you mean 3.7 aka Indigo? That update site is going to be available for EclipseCon (mid March, that is the current plan).
HTH,
Martin
Hernan says:
Added on February 25th, 2011 at 9:43 amThis is a feature I really wanted. Are there any plans of publishing this plugin with an open source license?
bethsands says:
Added on April 12th, 2011 at 3:34 amThank you for sharing this one. Very nice tutorial.
Declan says:
Added on June 2nd, 2011 at 5:09 amThis is exactly what I have been looking for! Great post and the examples worked perfectly.
All I had to do was update the actual template
Mani says:
Added on October 18th, 2011 at 8:46 amHi,
Everything i did. as you said but i was unable get a custom template.
May i know the correct procedure. Actually i have a doubt of generating jar file. There is no option without java packages. To export as a jar. How to generate jar before going to copy that jar into dropins folder of STS_HOME. Please let me come out of this.
Mani says:
Added on October 18th, 2011 at 10:11 amHi,
Everything i did well as you said but i was unable to get a custom template. Still i am unable to get the way. I am Using STS 2.3.2,
Please help to get this done.
Thanks,
Mani
Martin Lippert (blog author) says:
Added on October 21st, 2011 at 6:47 amHey Mani!
If you created a new plugin project within Eclipse/STS, you can export it by right-click on the project, select "Export…" and choose "as deployable plugin". This shoud produce the correct JAR file for you.
Regarding your second post: I haven't tried this procedure with STS 2.3.2, so I can't guarantee that it is working with that version. Why not using a newer one?
HTH,
Martin
visualjeff says:
Added on March 5th, 2012 at 5:43 pmDoesn't seem to work. Even used the provided code and created the appropriate User directory and it didn't make difference.
I'm running on Linux Mint 12 and STS 2.8.1.RELEASE
Jeff
Martin Lippert (blog author) says:
Added on March 8th, 2012 at 8:11 amHey Jeff!
What exactly is not working as expected? Can you provide more details? That would be great!
-Martin
visualjeff says:
Added on March 8th, 2012 at 11:10 amDon't you hate that when someone says something doesn't work and they don't leave much in the way of detail. Sorry, my mother just passed away so I'm a bit scattered.
We'll after implementing the sample code that was provided I was thinking STS might display a new template. So I did a File –> New –> Spring Template Project –> refresh. But nothing new showed up in the list of available templates. I was expecting something with the name of "Mein neues Template". Oh, well.
I'll give it another attempt today. The instructions looked pretty straight forward. The only change I accommodated for was the difference in directory paths.
Jeff
Preeth says:
Added on June 1st, 2012 at 7:18 amHi,
The download of the custom template is failing with following error.
Download of template 'MyTemplate' failed.
Caused by: org.eclipse.core.runtime.CoreException: Download of template 'MyTemplate' failed at com.springsource.sts.wizard.template.TemplateUtils.importDirectory(TemplateUtils.java:58)
Any idea?
Martin Lippert (blog author) says:
Added on June 5th, 2012 at 9:42 amHey Preeth,
hard to say what exactly is causing the problem. It looks a bit like your template.zip file (the one inside the complete template archive) is not there. But its a bit hard to say from here. Feel free to post this question to the forum, attaching your template project, and I will take a look at it in more detail.
-Martin
Rocco says:
Added on December 4th, 2012 at 9:36 pmHi ya. I followed these directions precisely both trying to put the template zip locally using file: url and on a real web server using both http & https. I first tried creating the plug-in as you suggested, then the template itself and all files. No go.
I also simply tried clicking New Spring Template project, then clicked configure templates blue link, and adding the descriptor.xml manually and choosing the descriptor.xml which resides locally on my machine. The wizard "picked it up" so when going back to the 1st page of the wizard, the new template project IS there. However, when trying to then actually create the project all I keep getting are errors. Now it seems the wizard simply pops up a dialog "Invalid Thread Access" … and no other messages.
I am running latest STS Version 3.1 on Mac OS 10.6.8. The biggest reason I am trying to do this is have a template for core spring 3.1, instead of 3.0 … seems weird that there is no template for 3.1 with the newest release of the sts.
Any ideas? Thanks a bunch.
Rocco says:
Added on December 4th, 2012 at 9:49 pmThe other point I will make is the following. I took an existing descriptor from github which is the exact one that already exists in the STS. The only thing different then, is the fact that I copied the descriptor.xml locally, instead of retrieving that from a web server. *failed* … so it seems file: is not working at all. I read a bug report saying in version 3.0.x … it did not work, but supposedly it was fix? Do you know anything about this? …. thanks!
Martin Lippert (blog author) says:
Added on December 6th, 2012 at 1:43 amHey Rocco!
This blog post refers to the template mechanism as it was in place for STS versions prior to 3.0. Since we improved and changed the way templates are managed within STS, the exact steps described here doesn't work anymore.
However, working with template projects should be a lot easier than before. The case that you describe where you have a local descriptors.xml file should work. If you have trouble getting this to work, I would suggest to raise an issue in our issue tracker (https://issuetracker.springsource.com/browse/STS) or ask those questions at the forum (http://forum.springsource.org/forumdisplay.php?32-SpringSource-Tool-Suite). Please attach your descriptors.xml file, the template project (the zip file) you are trying to use and some steps to reproduce the problem. We are very happy to help you to get this working!!!
HTH,
Martin