Geertjan's Blog

  • July 29, 2008

Groovy Generation of NetBeans Modules

Geertjan Wielenga
Product Manager
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}/Bundle.properties
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" "http://www.netbeans.org/dtds/filesystem-1_1.dtd">
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/platform-private.properties"/>
<property file="nbproject/platform.properties"/>
<macrodef name="property" uri="http://www.netbeans.org/ns/nb-module-project/2">
<attribute name="name"/>
<attribute name="value"/>
<property name="@{name}" value="\\${@{value}}"/>
<property file="\\${user.properties.file}"/>
<nbmproject2:property name="harness.dir" value="nbplatform.\\${nbplatform.active}.harness.dir" xmlns:nbmproject2="http://www.netbeans.org/ns/nb-module-project/2"/>
<nbmproject2:property name="netbeans.dest.dir" value="nbplatform.\\${nbplatform.active}.netbeans.dest.dir" xmlns:nbmproject2="http://www.netbeans.org/ns/nb-module-project/2"/>
<fail message="You must define 'nbplatform.\\${nbplatform.active}.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="http://www.netbeans.org/ns/project/1">
<data xmlns="http://www.netbeans.org/ns/nb-module-project/3">
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 +"manifest.mf").write(manifest_mf);
new File("${src}" + File.separator + "layer.xml").write(layer_xml);
new File("${src}" + File.separator + "Bundle.properties").write(bundle_properties);
new File("${nbproject}" + File.separator + "build-impl.xml").write(build_impl_xml);
new File("${nbproject}" + File.separator + "platform.properties").write(platform_properties);
new File("${nbproject}" + File.separator + "project.properties").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.

Join the discussion

Comments ( 2 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.