1
votes

I've created a custom plugin to simply manage a version.properties file. All works fine, can execute gradle tasks and what not. The issue I'm running into is how to access the value returned from my task (to set the version variable at runtime) in my build.gradle.

Error:

Build file '/Users/yomateod/workspace/work/spring.black/tags/build.gradle' line: 35

A problem occurred evaluating root project 'tags'.
> Could not find method getCurrentVersion() for arguments [] on root project 'tags' of type org.gradle.api.Project.

Line 34 and 35 in build.gradle:

apply plugin: 'versionest'
version = getCurrentVersion()

The task class:

package matthewdavis.io.plugins.versionest.tasks;

import matthewdavis.io.plugins.versionest.Version;
import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.TaskAction;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GetVersion extends DefaultTask {

    public static Version getCurrentVersionFromFile() throws IOException {

        String file = Files.readString(Path.of("version.properties"));

        Pattern pattern = Pattern.compile("(\\d+)\\.(\\d+)\\.(\\d+)");
        Matcher matcher = pattern.matcher(file);

        matcher.find();

        Version version = new Version();

        if (matcher.groupCount() == 3) {

            version.setMajor(Integer.parseInt(matcher.group(1)));
            version.setMinor(Integer.parseInt(matcher.group(2)));
            version.setPatch(Integer.parseInt(matcher.group(3)));

        } else {

            System.out.println("ERROR: invalid format for version.properties");
        }

        return version;

    }

    @TaskAction
    public String getCurrentVersion() throws IOException {

        Version version = getCurrentVersionFromFile();

        System.out.println(version);

        return version.getMajor() + "." + version.getMinor() + "." + version.getPatch();

    }

}

As you can see above I have the method getCurrentVersion() and running it via gradle works as expected:

$ gradle getCurrentVersion

> Configure project :
unspecified
> Task :getCurrentVersion
Version(major=0, minor=1, patch=0)

The full build.gradle is at https://github.com/spring-black/tags/blob/master/build.gradle#L35.

I declare my task at https://github.com/mateothegreat/gradle-plugin-versionest/blob/master/src/main/java/matthewdavis/io/plugins/versionest/VersionestPlugin.java#L12 and the task class is at https://github.com/mateothegreat/gradle-plugin-versionest/blob/master/src/main/java/matthewdavis/io/plugins/versionest/tasks/GetVersion.java.

I suspect I'm missing the proper way to call a tasks from the global scope of the build.gradle. Any help would be greatly appreciated.

Thanks ya'll! -Matthew

1

1 Answers

1
votes

Figured it out.. I needed to use an "extension" to expose my value to build.gradle:

project.getExtensions().create("versionest", VersionestExtension.class);

The extension class looks like:

public class VersionestExtension {

    public Version getVersion() throws IOException {

        return GetVersion.getCurrentVersionFromFile();

    }

}

build.gradle:

apply plugin: 'versionest'
version = versionest.version.toString()

Running a test task to println version:

10:45:29 PM: Executing task 'compileJava'...

> Configure project :
0.1.0

For those that come across the same problem check out https://github.com/mateothegreat/gradle-plugin-versionest#setup