Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying Gradle plugin from local file

I have the following gradle plugin that does the job of starting up a java process. The code for this lives under a file named startAppServerPlugin.gradle under the project's buildSrc directory.

The code of the plugin looks like this:

    repositories.jcenter()
    dependencies {
        localGroovy()
        gradleApi()
    }
}

public class StartAppServer implements Plugin<Project> {
    @Override
    void apply(Project project) {
        project.task('startServer', type: StartServerTask)
    }
}

public class StartServerTask extends DefaultTask {

    String command
    String ready
    String directory = '.'

    StartServerTask(){
        description = "Spawn a new server process in the background."
    }

    @TaskAction
    void spawn(){
        if(!(command && ready)) {
            throw new GradleException("Ensure that mandatory fields command and ready are set.")
        }

        Process process = buildProcess(directory, command)
        waitFor(process)
    }

    private waitFor(Process process) {
        def line
        def reader = new BufferedReader(new InputStreamReader(process.getInputStream()))
        while ((line = reader.readLine()) != null) {
            logger.quiet line
            if (line.contains(ready)) {
                logger.quiet "$command is ready."
                break
            }
        }
    }

    private static Process buildProcess(String directory, String command) {
        def builder = new ProcessBuilder(command.split(' '))
        builder.redirectErrorStream(true)
        builder.directory(new File(directory))
        def process = builder.start()
        process
    }

}

I'm trying to figure out a way of having this imported into my main build.gradle file due everything I tried so far has been unsuccessful.

So far I have tried this:

apply from: 'startAppServerPlugin.gradle'
apply plugin: 'fts.gradle.plugins'

But it has been failing. I've tried searching online for examples of doing what I need to do but so far I've been unsuccessful. Can anyone please provide a hint as to how I'm supposed to do so?

like image 989
akortex Avatar asked Sep 02 '25 16:09

akortex


1 Answers

You are on the right path. The first order of business is to import the external gradle build using:

apply from: 'startAppServerPlugin.gradle'

Then you can apply the plugin with:

apply plugin: StartAppServer

See Script Plugins and Applying Binary Plugins

like image 160
smac89 Avatar answered Sep 04 '25 15:09

smac89