Lazy property/provider API maven publish plugin

I created a plugin “MyPlugin” and would like consumers of the plugin allow the to override the prefix of the artifactId. Everything works when using “afterEvaluate” and the overridden value from the consumer is applied. However the afterEvaluate closure is causing other issues.

Unfortunately the artifactId is not using the lazy provider API and I have to provide a String and I can’t see how I can avoid setting the artifactId within afterEvaluate.

My code is basically this:

// My Plugin
abstract class MyPublishingExtension {
    abstract val prefix: Property<String>
    init {
        prefix.convention("default")
    }
}
val myPlugin = project.extensions.create<MyPublishingExtension>("myPlugin")
publishing {
    publications {
        create<MavenPublication>("maven") {
            logger.lifecycle(myPlugin.prefix.get()) // default
            afterEvaluate {
                logger.lifecycle(myPlugin.prefix.get()) // overridden
                artifactId = myPlugin.prefix.map { "$it${project.name}" }.get()
            }
        }
    }
}

// Plugin consumer
plugins {
    id("myPlugin")
}
myPlugin {
    prefix.set("overridden")
}

One way for such cases is to instead have a function in your extension that the consumer calls.
In the function you can then do the configuration according to consumerwishes.

I am sorry for my late reply and I just picked this up again.

Any hints or documentation references on how to define such a function?

Adding just a logging statement:

// My Plugin
abstract class MyPublishingExtension {
    abstract val prefix: Property<String>
    init {
        prefix.convention("default")
    }

    fun overridePrefix(prefix: String) = {
        logger.lifecycle("setting prefix")
   }
}

fails with

Caused by: java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
        at org.gradle.internal.instantiation.generator.DependencyInjectingInstantiator.addServicesToParameters(DependencyInjectingInstantiator.java:167)
        at org.gradle.internal.instantiation.generator.DependencyInjectingInstantiator.convertParameters(DependencyInjectingInstantiator.java:121)

Remove the equals sign. :slight_smile: