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

Automatically Install the NetBeans Gradle Plugin

DZone's Guide to

Automatically Install the NetBeans Gradle Plugin

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

Your requirement is that users of your NetBeans module will need to make use of Gradle, hence it would be handy if the NetBeans Gradle plugin were to be installed when the module you've created is installed.

To implement this requirement, first read everything here:

http://bits.netbeans.org/dev/javadoc/org-netbeans-modules-autoupdate-services/overview-summary.html

And then here's all the code you need:

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.netbeans.api.autoupdate.InstallSupport;
import org.netbeans.api.autoupdate.InstallSupport.Installer;
import org.netbeans.api.autoupdate.InstallSupport.Validator;
import org.netbeans.api.autoupdate.OperationContainer;
import org.netbeans.api.autoupdate.OperationException;
import org.netbeans.api.autoupdate.OperationSupport.Restarter;
import org.netbeans.api.autoupdate.UpdateElement;
import org.netbeans.api.autoupdate.UpdateManager;
import org.netbeans.api.autoupdate.UpdateUnit;
import org.netbeans.api.progress.ProgressHandle;
import org.netbeans.api.progress.ProgressHandleFactory;
import org.openide.modules.OnStart;
import org.openide.util.Cancellable;

@OnStart
public class Startable implements Runnable {

    @Override
    public void run() {
        System.out.println("Getting started installing Gradle plugin...");
        List<UpdateUnit> allModules = UpdateManager.getDefault().getUpdateUnits();
        for (UpdateUnit oneModule : allModules) {
            List<UpdateElement> aus = oneModule.getAvailableUpdates();
            for (UpdateElement el : aus) {
                //System.out.println("Available module " + el);
                if (el.getCodeName().equals("org.netbeans.gradle.project")) {
                    try {
                        OperationContainer<InstallSupport> container = OperationContainer.createForInstall();
                        container.add(el);
                        InstallSupport support = container.getSupport();
                        Validator v = doDownload(support);
                        Installer i = doVerify(support, v);
                        doInstall(support, i);
                    } catch (OperationException ex) {
                        Logger.getLogger(Startable.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }
    }

    static Restarter doInstall(InstallSupport support, Installer installer) throws OperationException {
        final String displayName = "Installing Gradle plugin...";
        System.out.println(displayName);
        ProgressHandle installHandle = ProgressHandleFactory.createHandle(
                displayName,
                new Cancellable() {
                    @Override
                    public boolean cancel() {
                        return true;
                    }
                }
        );
        return support.doInstall(installer, installHandle);
    }

    static Installer doVerify(InstallSupport support, Validator validator) throws OperationException {
        final String displayName = "Validating Gradle plugin...";
        System.out.println(displayName);
        ProgressHandle validateHandle = ProgressHandleFactory.createHandle(
                displayName,
                new Cancellable() {
                    @Override
                    public boolean cancel() {
                        return true;
                    }
                }
        );
        Installer installer = support.doValidate(validator, validateHandle);
        return installer;
    }

    static Validator doDownload(InstallSupport support) throws OperationException {
        final String displayName = "Downloading Gradle plugin...";
        System.out.println(displayName);
        ProgressHandle downloadHandle = ProgressHandleFactory.createHandle(
                displayName,
                new Cancellable() {
                    @Override
                    public boolean cancel() {
                        return true;
                    }
                }
        );
        return support.doDownload(downloadHandle, false, true);
    }

}

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}