Cannot create a publication named x

I’m trying to build a plugin that consolidates the configuration of artifact publication. I had the logic working in a generic build, but now that I’ve reworked it into a plugin, I’m having trouble. Here is the basics of my apply function:

void apply(Project project) {
        PluginExtension extension = project.extensions.create('myExt', PluginExtension)
        project.apply plugin: 'maven-publish'
          project.task('downloadRelease') {
            ext.downloadedFile = project.file("${extension.moduleName}-release.zip")
            ext.moduleVersion = extension.moduleVersion
            outputs.file downloadedFile
              doLast {
                println ("Downloading ${extension.moduleName}-release.zip")
            }
        }
          project.task('downloadDebug') {
            ext.downloadedFile = project.file("${extension.moduleName}-debug.zip")
            ext.moduleVersion = extension.moduleVersion
            outputs.file downloadedFile
              doLast {
                println ("Downloading ${extension.moduleName}-debug.zip")
            }
        }
          project.publishing {
            publications {
                moduleRelease(MavenPublication) {
                    artifact (downloadRelease.downloadedFile) {
                        classifier "release"
                    }
                    artifact (downloadDebug.downloadedFile) {
                        classifier "debug"
                    }
                    artifactId extension.moduleName
                    groupId extension.groupId
                    version downloadRelease.moduleVersion
                }
              }
            repositories {
                maven {
                    credentials {
                        username extension.artifactoryUser
                        password
extension.artifactoryPass
                    }
                    url "${extension.artifactoryUrl}/${extension.repositoryName}"
                }
            }
        }
        project.publish.dependsOn project.downloadRelease, project.downloadDebug
    }

When I try to use the plugin, it seems there is difficulty in recognizing the MavenPublication class, because I get this error:

Cannot create a Publication named 'downloadRelease' because this container does not support creating elements by name alone. Please specify which subtype of Publication to create. Known subtypes are: MavenPublication

The error message isn’t great, but basically Gradle is not able to resolve ‘downloadRelease’ inside your MavenPublication, and so is trying to create a new publication with that name.

It should work if you use

version extension.moduleVersion

or

version project.tasks.downloadRelease.moduleVersion