How to export customs task types?


(Sebastian Peters) #1

Hello there! First and foremost, I’m bloodily new to Gradle, so I do feel like I’m pretty stumped with basic knowledge. Sorry for that.

In any case, I’m currently trying to learn how to write custom task types. For that, I’ve tried to follow this guide. No problem here when I directly included that as a normal task in my main build.gradle:

task mytask(type: MyCustomTask) {
    somestring = 'hello'
}

class MyCustomTask extends DefaultTask {
    String somestring = 'goodbye'
    
    @TaskAction
    def run() {
        println somestring
    }
}

As expected, executing gradle mytask prints “Hello” just fine.

However, the general idea I’m pursuing is obviously a reusable plugin that could be applied to a number of existing projects. The guide I’ve linked gets pretty confusing at 38.3. I’ve never specified a group, name, or version for that matter, so I got no idea how to properly export MyCustomTask.


(James Justinic) #2

It sounds like you are specifically confused by Example 38.6. Using a custom task in another project that specifies:

The following example shows how you might do this when the JAR containing the task class has been published to a local repository:

I would recommend that you either take a detour and read about publishing before you continue or reference the full example mentioned in the note.

Note: The code for this example can be found at samples/customPlugin/plugin in the ‘-all’ distribution of Gradle.

If you’d like to look at it on GitHub rather than in the distribution, here’s a direct link to the folder containing both the plugin and consumer projects: https://github.com/gradle/gradle/tree/master/subprojects/docs/src/samples/customPlugin

The publishing step will specify the information that is then used for consuming the plugin.