BookNBPlatformCookbookCH1007

Contents

NetBeans Platform Cookbook Chapter 10 07

Create Project Template

We will learn in this section create project template. It is used in these cases:

Your module defines own project type and it must create new project. It can define several kinds of given type, e.g. empty, rich, simple. You want provide an example project to show how some API, technology etc. is used.

You use some project structure with standard context more times when you begin new project. So you store such a project into template. When you create any similar project you have prepared the infrastructure and many classes without copying them.

Preparation

We show template creating on our project type created in the previous tip.

image:Nbpcook_10_11_projectcrt.png

Figure 10.11 Create MyProject project

How to

We will add creating of the new MyProject feature into application created in previous section. Open this project suite.

Create new module in this suite, e.g. crtproj.

Create directory templates within module base package.

In the templates directory create directory MyProject and files by wished project structure within it or copy project from elsewhere. The structure of our example is next:

<crtproj-project-directory>
|- <base-package>
     |- templates
           |- MyProject
               | 
               |- documents
               |   |- ... any prepared files
               |
               |- data
               |   |- ... any prepared files
               |
               |- myproj
                    |- project.properties

If the new blank project would contain any other files create them.

Run the project (or run other IDE and install your modules). You run the new copy of your IDE. It recognizes your project type now.

Open the suite project and crtproj project in the copy of IDE.

In the copy of IDE open the sample project in the <base-package> / tempplates directory. File / Open Project , find <suiteproj>/<crtproj-project-dir>/src/<base-package>/templates/MyProject and open it. You must pass through project structure to the templates directory expanding folders.

In the java crtproj module click on the Source Package node and choose File / New File.

Choose Module Development in the Categories and Project Template in the File type. Press Next.

In the combo box choose your template MyProject project. Press Next. Fill the Template Name (e.g. MyProject), Display Name (e.g. New My Project) and left the category (or choose any simple, e.g. Java). We will change it later. In the package choose (or fill) the module base package and add e. g. empty or newproj to it. The subpackage will be created. Press Finish.

Look at file structure of the crtproj project. There is the MyProjectProject.zip file in the module base package and the project template was registered in the layer.xml file. In new created subpackage (empty or myproject) were created some files.

Now you can close the copy of the IDE.

Into the build.xml file of the crtproj project add this target to actualize zip file by building process to keep zip file up to date:

<target name="netbeans-extra" depends="zip-proj-template"/>

<target name="zip-proj-template" 
        description="Zips MyProject project template">

    <delete file="${basedir}/src/<base-package>/MyProjectProject.zip"/>

    <zip compress="9" basedir="src/<base-package>/templates/MyProject/"
     zipfile="${basedir}/src/<base-package>/MyProjectProject.zip"/>

</target>   

where <base-package> means the project slashed base package, e. g. com/packtpub/nbpcook/best/myproj/crtproj for our example.

This zip file would be excluded from version control because it is recreated by every build process.

In the Important Files open the layer.xml file and rewrite the Standard category under Templates/Project folder to MyProject.

Copy the attr XML node from the MyProjectProject.zip file to Templates/Project/MyProject file.

Replace the Standard name to your MyProject. Do the same in the Bundle.properties file. It now looks like:

 <folder name="Templates">
     <folder name="Project">
         <folder name="MyProject">
             <attr name="displayName" 
                   bundlevalue="<base.package>.Bundle#Templates/Project/MyProject/MyProjectProject.zip"/>
             <file name="MyProjectProject.zip" url="MyProjectProject.zip">
                 <attr name="displayName" bundlevalue="<base.package>.Bundle#Templates/Project/MyProject/MyProjectProject.zip"/>
                 ....

where <base.package> string reprezents base package of the project, e. g. the com.packtpub.nbpcook.best.myproj.crtproj in our example.

Edit description of the project in the MyProjectDescription.html file.

Now you can deselect unused clusters and modules of the used platform from project suite to get the application smaller.

Try your application to create new MyProject project.

Notes/Tips - documentation

How to create project sample is described in NetBeans Project Sample Module Tutorial on http://platform.netbeans.org/tutorials/nbm-projectsamples.html and in "10-week Free NetBeans Platform Programming (with Passion!) Online Course" on http://javapassion.com/nbplatform/.

In the “NetBeans Project Type Extension Module Tutorial” is described how to add. You can find it on http://platform.netbeans.org/tutorials/nbm-projectextension.html.

In the “Project Properties GUI for custom project templates” tutorial on http://blogs.sun.com/gridbag/entry/project_properties_gui_for_custom is described how to edit project properties.

Notes/Tips - examples

The example sources are available in 10_best / MyprojectsTmp directory. You can add this template into project in 10_best / Myprojects directory.

Text and sources were created under NB 6.8, 6.9, 7.0 and 7.1.

Navigation

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo