Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Setting Preferences in Groovy; Retrieving Preferences in Java

DZone's Guide to

Setting Preferences in Groovy; Retrieving Preferences in Java

· Java Zone
Free Resource

Try Okta to add social login, MFA, and OpenID Connect support to your Java app in minutes. Create a free developer account today and never build auth again.

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 {

    @Override
    public boolean interestedInSession(AntSession session) {
        return true;
    }

    @Override
    public boolean interestedInAllScripts(AntSession session) {
        return true;
    }

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

    private static final String PROJ_LOCATION = "";

    @Override
    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(
                    "com/groovy/generated/netbeans/projects");
            //If the node exists:
            if (exists) {
                //Get the node:
                Preferences prefs = Preferences.userRoot().node(
                        "com/groovy/generated/netbeans/projects");
                //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:
                    prefs.removeNode();
                    //Message to stdout:
                    IOProvider.getDefault().getStdOut().println(
                            "Successfully opened " +
                            proj.getProjectDirectory().getName());
                }
            }
        } catch (BackingStoreException ex) {
            Exceptions.printStackTrace(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!

 

Build and launch faster with Okta’s user management API. Register today for the free forever developer edition!

Topics:

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}