Wednesday May 30, 2007
Managing NetBeans Project Templates
Although it's not hard to create NetBeans project templates, it's quite painful to have to update them, unless you know about what I'm going to tell you about in this blog entry. A project template is created by a wizard and each consists of a number of things. Firstly, most importantly, the template itself, in the form of a ZIP file. The template is normally an entire application, with its own sources and build files, and so on. But, in addition, a project template includes an HTML file with a description that will be shown in the New Project wizard. It also contains an icon. It also contains an XML layer file that registers the project template. It also contains a Bundle file. Hence, what happens when you update the application that you're distributing as a template? Do you need to work through the Project Template wizard again and provide all those various items all over again?
And the answer is "no". There's no need to go through the Project Template wizard again, each time you update the application. Why? Because of Ant. You can provide an Ant target, in the application that you want to distribute as a template, to create a new ZIP file whenever you change it. And you can use the Ant target to specify where the ZIP file should be created. Thus, you can pop that ZIP file automatically into the project template's source structure, overwriting the existing ZIP file. As a result, updating your project template is very easy. I learned this from Jesse, who provides this very useful Ant script as an illustration.
The nicest thing would be, in my opinion, if the application was rezipped whenever I build it. There don't seem to be handy hooks like -post-jar in the module development world, so I simply copied the build target from build-impl.xml, into build.xml. And then I added my own few lines after that, so that I had an overridden build target, extended with my ZIP functionality. Below, only the highlighted lines are my own, the rest is the standard build target in module projects:
<target name="build" depends="-init,branding" description="Build all modules in the suite.">
<subant target="netbeans" buildpath="${modules.sorted}" inheritrefs="false" inheritall="false"/>
<mkdir dir="${cluster}/config/Modules"/>
<createmodulexml xmldir="${cluster}/config/Modules">
<hidden dir="${netbeans.dest.dir}">
<custom classpath="${harness.dir}/tasks.jar" classname="org.netbeans.nbbuild.ModuleSelector">
<param name="excludeModules" value="${disabled.modules}"/>
<param name="excluded" value="true"/>
</custom>
</hidden>
</createmodulexml>
<property name="build.classes.dir" location="/home/geertjan/Desktop/TextFileDictionaryProjectTemplate"/>
<property name="examples" location="${build.classes.dir}/src/org/netbeans/modules/textfiledictionaryprojecttemplate/"/>
<zip basedir="../TextFileDictionaryProjectTemplate" destfile="${examples}/TextFileDictionaryProjectTemplateProject.zip">
<exclude name="**/build/"/>
<exclude name="**/dist/"/>
<exclude name="**/nbproject/private/"/>
</zip>
</target>
As a result of the above, my project template's ZIP file, which contains the application that I want to distribute as a template (or as a sample, which comes down to the same thing) is updated whenever I build my application. Hurray.
May 30 2007, 11:42:25 AM PDT Permalink


