Monday Jul 28, 2008

Groovy Generation of NetBeans Modules

I'm continuing to be impressed with the usefulness of Groovy in getting started with source structures for applications. Yesterday I showed how to generate a NetBeans Platform application from a Groovy script. Today I present... a Groovy script that will generate a NetBeans module. This is pretty much the absolutely barest NetBeans module imaginable. Nonetheless, a NetBeans module is what it is, and no different from the one you get via the "New Module" wizard in the "New Project" dialog. And once you've run this script, you'll be able to open the module in the IDE and begin adding features to it right away.
package generators

def id = new Date().time
def folder = System.getProperty("user.home") + "/NewModule" + id;
def name = "NewModule" + id;
def pkgs = "org/netbeans/modules/demo"

println "Generating project ${name} in folder ${folder}";

def manifest_mf = """\\
Manifest-Version: 1.0
OpenIDE-Module: ${name}
OpenIDE-Module-Layer: ${pkgs}/layer.xml
OpenIDE-Module-Localizing-Bundle: ${pkgs}/
OpenIDE-Module-Specification-Version: 1.0


def build_xml = """\\
<?xml version="1.0" encoding="UTF-8"?>
<!-- You may freely edit this file. See harness/README in the NetBeans platform -->
<!-- for some information on what you could do (e.g. targets to override). -->
<!-- If you delete this file and reopen the project it will be recreated. -->
<project name="${name}" basedir=".">
    <description>Builds, tests, and runs the project ${name}.</description>
    <import file="nbproject/build-impl.xml"/>


def layer_xml = """\\
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE filesystem PUBLIC "-//NetBeans//DTD Filesystem 1.1//EN" "">


def bundle_properties = """\\


def build_impl_xml = """\\
<?xml version="1.0" encoding="UTF-8"?>
\*\*\* GENERATED FROM project.xml - DO NOT EDIT  \*\*\*
\*\*\*         EDIT ../build.xml INSTEAD         \*\*\*
<project name="${name}-impl" basedir="..">
    <property file="nbproject/private/"/>
    <property file="nbproject/"/>
    <macrodef name="property" uri="">
        <attribute name="name"/>
        <attribute name="value"/>
            <property name="@{name}" value="\\${@{value}}"/>
    <property file="\\${}"/>
    <nbmproject2:property name="harness.dir" value="nbplatform.\\${}.harness.dir" xmlns:nbmproject2=""/>
    <nbmproject2:property name="netbeans.dest.dir" value="nbplatform.\\${}.netbeans.dest.dir" xmlns:nbmproject2=""/>
    <fail message="You must define 'nbplatform.\\${}.harness.dir'">
                <available file="\\${harness.dir}" type="dir"/>
    <import file="\\${harness.dir}/build.xml"/>

def platform_properties = """\\

def project_properties = """\\
javac.compilerargs=-Xlint -Xlint:-serial

def project_xml = """\\
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="">
        <data xmlns="">

def base = new File(folder);
def src = new File(folder + File.separator + "src" + File.separator + "${pkgs}");
def nbproject = new File(folder + File.separator + "nbproject");
new File(folder + File.separator  +"build.xml").write(build_xml);
new File(folder + File.separator  +"").write(manifest_mf);
new File("${src}" + File.separator + "layer.xml").write(layer_xml);
new File("${src}" + File.separator + "").write(bundle_properties);
new File("${nbproject}" + File.separator + "build-impl.xml").write(build_impl_xml);
new File("${nbproject}" + File.separator + "").write(platform_properties);
new File("${nbproject}" + File.separator + "").write(project_properties);
new File("${nbproject}" + File.separator + "project.xml").write(project_xml);


The screenshot below has two nodes highlighted. The first is the node of the Groovy script that I ran to generate the whole of the source structure represented by the second highlighted node:

I.e., running the above script gives you a compilable starting point for creating NetBeans modules, without needing to go through any wizards to do so. The script runs in the space of a split second, generating everything I need. Plus, it's very simple to tweak the script to make it generate something different. Imagine, for example, how easy it would be to have a few predefined actions or windows in your module? Just go through the motions using the IDE's tools and then copy the generated code into the script above and then you'll never need to create them again via wizards, but always via your script instead.


Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.


« July 2008 »