Over a million developers have joined DZone.

Setting Preferences in Groovy; Retrieving Preferences in Java

DZone's Guide to

Setting Preferences in Groovy; Retrieving Preferences in Java

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

In the past two days I've been figuring out how to use Groovy scripts to generate NetBeans projects:

The aim of the exercise was to mirror as closely as possible the end result of working through the "New Module" wizard and the "New NetBeans Platform Application" wizard in NetBeans IDE. And the above two Groovy scripts do just that.

However... when the two wizards mentioned above are completed, the created projects are automatically opened in the IDE. I can't do that from within the Groovy script that defines the source structure that I want to create. Instead, I need to create a NetBeans module that listens to the Ant output that is generated when the Groovy script is run. At the point where the relevant Ant target has completed its work, I need to have my module open the created project.

But there is a problem. How can my module know which project has been created? It needs to know that in order to know what to open. So here's my solution—at the end of the two Groovy scripts described in the links above, I now have these lines:

private static final String PROJ_LOCATION = ""

Preferences prefs = Preferences.userRoot().node("com/groovy/generated/netbeans/projects")
prefs.put(PROJ_LOCATION, folder)


Above, "folder" is the full path to the root directory of the NetBeans project created by the Groovy script. So the lines above write that folder to a java.util.Preference.

Then, in my module I have a class that implements org.apache.tools.ant.module.spi.AntLogger, exposed via META-INF/services, with exactly this content, which does everything I need it to do. The comments in the code should explain everything:

public class GenProjListener extends org.apache.tools.ant.module.spi.AntLogger {

    public boolean interestedInSession(AntSession session) {
        return true;

    public boolean interestedInAllScripts(AntSession session) {
        return true;

    public String[] interestedInTargets(AntSession session) {
        return new String[]{"run-single"};

    private static final String PROJ_LOCATION = "";

    public void targetFinished(AntEvent event) {
        try {
            //Check if the node exists, which it should,
            //because the Groovy script created it:
            boolean exists = Preferences.userRoot().nodeExists(
            //If the node exists:
            if (exists) {
                //Get the node:
                Preferences prefs = Preferences.userRoot().node(
                //Get the property from the node:
                String loc = prefs.get(PROJ_LOCATION, "");
                //Turn the String into a File and then into a FileObject:
                FileObject fo = FileUtil.toFileObject(new File(loc));
                //Then find that FileObject's owner, which is a NetBeans Project:
                Project proj = FileOwnerQuery.getOwner(fo);
                //Next, check to make sure the owner
                //is really a NetBeans Project:
                if (proj.getProjectDirectory().isValid()) {
                    //Now simply open the NetBeans Project:
                    OpenProjects.getDefault().open(new Project[]{proj}, true);
                    //Remove the node again, which will be recreated
                    //by the Groovy script:
                    //Message to stdout:
                            "Successfully opened " +
        } catch (BackingStoreException ex) {


Notice, among other things, how easy it is to get from a String to a NetBeans project! In addition, notice that I'm interacting, via java.util.Preferences, with my Groovy script. That's kind of fun too.

And now, whenever I run my Groovy scripts, the generated NetBeans project automatically opens in the IDE. I need never use a project-level wizard again. I can now always run a simple Groovy script that does all the same work for much less effort on my part!


Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}