Sunday Jul 27, 2008

Groovy Generation of Modular Desktop Applications

I came across a rather cool blog entry today (via the always excellent http://groovyblogs.org/), ICEFaces Project Generation Using Groovy, by Rob Mayhew. Very cool that he included all his code. A few tweaks later I was able to generate a new NetBeans Platform application from Groovy:
package generators

def folder = "/home/geertjan/Desktop/wonderful";
def name = "MyWonderfulApplication";

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

def build = """\\
<?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 the module suite ${name}.</description>
    <import file="nbproject/build-impl.xml"/>
</project>

"""
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=".." xmlns:sproject="http://www.netbeans.org/ns/nb-module-suite-project/1">
    <property file="nbproject/private/platform-private.properties"/>
    <property file="nbproject/platform.properties"/>
    <macrodef name="property" uri="http://www.netbeans.org/ns/nb-module-suite-project/1">
        <attribute name="name"/>
        <attribute name="value"/>
        <sequential>
            <property name="@{name}" value="\\${@{value}}"/>
        </sequential>
    </macrodef>
    <property file="\\${user.properties.file}"/>
    <sproject:property name="harness.dir" value="nbplatform.\\${nbplatform.active}.harness.dir"/>
    <sproject:property name="netbeans.dest.dir" value="nbplatform.\\${nbplatform.active}.netbeans.dest.dir"/>
    <fail message="You must define 'nbplatform.\\${nbplatform.active}.harness.dir'">
        <condition>
            <not>
                <available file="\\${harness.dir}" type="dir"/>
            </not>
        </condition>
    </fail>
    <import file="\\${harness.dir}/suite.xml"/>
</project>

"""
def platform_properties = """\\
disabled.clusters=\\
    apisupport1,\\
    enterprise5,\\
    groovy1,\\
    gsf1,\\
    harness,\\
    ide10,\\
    java2,\\
    nb6.5,\\
    profiler3,\\
    visualweb2,\\
    webcommon1,\\
    websvccommon1,\\
    xml2
disabled.modules=\\
    org.netbeans.libs.jsr223,\\
    org.openide.compat,\\
    org.netbeans.modules.autoupdate.services,\\
    org.netbeans.api.visual,\\
    org.netbeans.core.execution,\\
    org.netbeans.core.multiview,\\
    org.openide.execution,\\
    org.openide.options,\\
    org.netbeans.modules.favorites,\\
    org.netbeans.modules.templates,\\
    org.netbeans.modules.autoupdate.ui,\\
    org.openide.util.enumerations,\\
    org.netbeans.modules.core.kit
enabled.clusters=\\
    platform9
nbplatform.active=default

"""
def project_properties = """\\

app.name=token
app.title=${name}
branding.token=\\${app.name}
modules=

"""
def project_xml = """\\
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
    <type>org.netbeans.modules.apisupport.project.suite</type>
    <configuration>
        <data xmlns="http://www.netbeans.org/ns/nb-module-suite-project/1">
            <name>${name}</name>
        </data>
    </configuration>
</project>

"""
def base = new File(folder);
base.mkdirs();
def nbproject = new File(folder + File.separator + "nbproject");
nbproject.mkdirs();
new File(folder + File.separator  +"build.xml").write(build);
new File(folder + File.separator + "nbproject" + File.separator + "build-impl.xml").write(build_impl_xml);
new File(folder + File.separator + "nbproject" + File.separator + "platform.properties").write(platform_properties);
new File(folder + File.separator + "nbproject" + File.separator + "project.properties").write(project_properties);
new File(folder + File.separator + "nbproject" + File.separator + "project.xml").write(project_xml);

println("Done.")

Rather wonderfully, running the above script will give you a NetBeans project which, when you open it, will give you this:

That's a NetBeans Platform application which, when deployed, gives you this:

Now... imagine that you were to create different Groovy scripts, each providing different starting points for developing an application on the NetBeans Platform. One Groovy script would provide the application above, i.e., the same as you get via the NetBeans Platform Application wizard in the New Project dialog. Another one, however, could include a few basic modules that provide a more fleshed out layout. Or maybe you'd like to have the Plugin Manager available by default? Simply tweak the script to include the applicable modules, which you could simulate via the ui in the IDE and then copy the generated tags over to your Groovy script. In the end, you'd never need to go through a template in the IDE again, because you could, in the blink of an eye, generate everything you need via Groovy. And the script could be run, of course, from within the IDE.

NetBeans IDE 6.1 Java Editor Refcard

Here it is:

Click the above or click this link: http://refcardz.dzone.com/announcements/netbeans

It specifically covers the Java editor (no way could it cover everything the IDE offers, so choices had to be made), future NetBeans cards could cover other languages (Ruby, PHP, Groovy, etc) or specific technologies (Mobility, Java EE, etc), for which proposals are welcome. Also, it is specifically focused on NetBeans IDE 6.1, an update will be published when 6.5 is released. Suggestions for improvements (and corrections) that will then be implemented in that release are welcome. Thanks very much to the reviewers (listed at the end of the card) and Jill Tomich from DZone for being patient and great to work with.

About

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.

Search

Archives
« July 2008 »
SunMonTueWedThuFriSat
  
4
5
6
11
12
13
14
17
26
30
  
       
Today