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

The CMS developers love. Open Source, API-first and Enterprise-grade. Try BloomReach CMS for free.

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);
    }

}

BloomReach CMS: the API-first CMS of the future. Open-source & enterprise-grade. - As a Java developer, you will feel at home using Maven builds and your favorite IDE (e.g. Eclipse or IntelliJ) and continuous integration server (e.g. Jenkins). Manage your Java objects using Spring Framework, write your templates in JSP or Freemarker. Try for free.

Topics:

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}