Executing a custom gradle task causes `Class Build_gradle.MyTask is a non-static inner class`

426 views Asked by At

I am creating a custom task that will be registered in each module's build.gradle.kts file. I need maven-publish plugin for that. Here is how the task looks

abstract class MyTask : DefaultTask() {
    @get:Input
    abstract val moduleName: Property<String>

    @get:Input
    abstract val moduleArtifactId: Property<String>

    @get:Input
    abstract val repositoryUrl: Property<String>

    @TaskAction
    fun execute() {

        print("hello from my task, my name is ${moduleName.get()}, ${moduleArtifactId.get()}, ${repositoryUrl.get()}") //${name.get()}")
        project.publishing {
            publications {
                create<MavenPublication>("release") {
                    groupId = project.properties["groupId"] as String
                    version = project.properties["version"] as String
                    artifactId = moduleArtifactId.get()
                    project.afterEvaluate {
                        from(components["release"])
                    }
                    artifact("$buildDir/outputs/aar/$artifactId-release.aar") {
                        classifier = "release"
                    }
                }
            }
            repositories {
                maven {
                    this.name = moduleName.get()
                    this.url = uri(repositoryUrl.get())

                    credentials {
                        username = "my-user-name"
                        password = "my-password"
                    }
                }
            }
        }
    }
}

and I register it in build.gradle.kts in the following manner:


tasks.register<MyTask>("mytask") {
    moduleName.set("module's name")
    moduleArtifactId.set("artifact's id")
    repositoryUrl.set("remote repo's url")
}

Error I am getting is:

* What went wrong:
A problem occurred configuring project ':sdk:modulename'.
> Could not create task ':sdk:modulename:mytask'.
   > Could not create task of type 'MyTask'.
      > Class Build_gradle.MyTask is a non-static inner class.

I need a task for this since I want the exact same logic for all the modules. I just change some of things dynamically, like url and artifactId

1

There are 1 answers

1
Jonathan Schneider On

This is the error message whenever the task uses a variable or state from outside the class definition (that seems to make it implicitly a non-static inner class).

See the comment on improving Gradle's error messaging here.

I don't immediately see which variable is coming from outside of the class scope, but you may do a quick binary search of commenting out halves of the code until you find it.