Hello guys, I am facing an issue when creating a c...
# gradle
d
Hello guys, I am facing an issue when 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
Copy code
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:
Copy code
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:
Copy code
* 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
s
Classes defined in build.gradle.kts automatically become inner classes if they reference any properties from the build itself, e.g. the
project
.
I'm not exactly sure which line will be the culprit in this case 🤔 because the task's own
project
property should shadow the one from the outer build script
Probably one of the DSL function you're using, like
repositories
, is resolving to a function from the build script instead of something defined inside the class
To be honest, I would just put the whole thing in a separate module like buildSrc instead of declaring it inside a build script
e
also you should not modify the project model (such as adding publications) from within a task action. it will not work and that should all be set up at configuration time
d
let me try doing it in a separate module, didn't know it could be specific to build.gradle, I just wanted to test it in there for the beginning
👍 1
h
Your actual task action will still not work.
v
The culprit should be
uri(...)
which references the outer script and thus makes the task non-static. But as others said, you must not change the configuration at task execution time anyway. What you want is not a task, but a plugin. A so-called convention plugin that you develop in
buildSrc
or an included build like
gradle/build-logic
and that you then apply to all your modules builds.
d
Thank you guys, I will let you know when I figure out how to do this properly. Will create a convention plugin. @hfhbd why you think it won't work?