Configure plugin added in init script

Hello,

Could anybody advise on the problem below?

We use a custom gradle distribution with our own init script. We register and configure a plugin there:

initscript {
    classpath "tech.harmonysoft:gradle-release-paperwork:1.8.0"
}

allprojects {
    apply plugin: tech.harmonysoft.oss.gradle.release.paperwork.GradleReleasePaperworkPlugin
}

projectsEvaluated {
    rootProject.allprojects { project ->
        releasePaperwork {
            releaseNotesFile = "some-file" // plugin configuration works here
        }
    }
}

The problem is that when we try to additionally configure the plugin in the target project which uses this init script, that fails:

releasePaperwork {
    tagPattern = "v%s"
}

output:

Build file '/xxx/build.gradle.kts' line: 9

* What went wrong:
Script compilation error:

  Line 9:     tagPattern = "v%s"
              ^ Unresolved reference: tagPattern

1 error

Is it expected to be possible to configure a plugin registered in init script?

Whether this is a bug or a technical limitation I do now know.
Maybe worth reporting an issue if there is none yet.
It seems the class loader from the init script is not in the upwards hierarchy of the class loader from the build script.

The releasePaperwork extension is seen, so an accessor is generated for it and it does not complain about that one.
But its type is not found and so is of type Any which does not have the tagPattern property.

If you try to cast this, the extension class to cast to is not found.

If you simply add the plugin to the classpath of the build script too, even without an explicit cast, you get a cast exception.

As long as you do not add the plugin to the buildscript classpath you can work-around the problem using:

releasePaperwork {
    withGroovyBuilder {
        setProperty("tagPattern", "v%s")
    }
}

Another work-around would be to have

releasePaperwork {
   tagPattern = 'v%s'
}

in a Groovy DSL script plugin that you then apply using

apply(from = "gradle/configure-release-paperwork.gradle")

Another work-around that also mitigates the cast exception if the plugin is also on the build script classpath is to use

extensions["releasePaperwork"].withGroovyBuilder {
    setProperty("tagPattern", "v%s")
}

But all these are imho just work-arounds and you should report that use-case.

thanks, Björn!

Created Please expose plugins defined in init script to the target project · Issue #27979 · gradle/gradle · GitHub for that

1 Like