I want to move some logic from build script to a plugin but I mess up in the process and publishing stops working. As a starting point, my working build script looks like this:
plugins {
id 'maven-publish'
}
group = 'org.foo'
version = '1.0.0'
def appBuildDir = "${->project.buildDir}/app"
def distDir = "${project.projectDir}/dist"
configurations {
production
}
task buildApp(type: Copy) {
doFirst {
file(appBuildDir).mkdirs()
}
from file('src/app')
into appBuildDir
}
task assemble(type: Zip) {
baseName = project.name
destinationDir = file(distDir)
doFirst {
destinationDir.mkdirs()
}
from appBuildDir
dependsOn(buildApp)
}
artifacts {
production assemble
}
publishing {
publications {
app(MavenPublication) {
artifactId project.name
artifact assemble
}
}
}
This script is capable of pushing the built zip file to mavenLocal.
Now, I can more or less cut-and-paste the build logic into the apply method of my plugin:
public void apply(Project project) {
project.plugins.apply('maven-publish')
project.configurations.maybeCreate('production')
def appBuildDir = "${->project.buildDir}/app"
def distDir = "${project.projectDir}/dist"
def buildAppTask = project.tasks.create('buildApp', Copy) {
doFirst {
project.file(appBuildDir).mkdirs()
}
from project.file('src/app')
into appBuildDir
}
def assembleTask = project.tasks.create('assemble', Zip) {
baseName = project.name
destinationDir = project.file(distDir)
doFirst {
destinationDir.mkdirs()
}
from appBuildDir
dependsOn(buildAppTask)
}
project.artifacts {
production assembleTask
}
project.publishing {
publications {
app(MavenPublication) {
artifactId project.name
artifact assembleTask
}
}
}
}
And the build script reduces to:
plugins {
id 'myPlug' version '1.0.0'
}
group = 'org.foo'
version = '1.0.0'
Now, for some reason, the publishing configuration has broken down and I get a configuration-time exception telling me this:
C:\projects\foo>gradle publishToMavenLocal
FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring project ':modules:fooModule'.
> Exception thrown while executing model rule: PublishingPlugin.Rules#publishing(ExtensionContainer)
> Cannot create a Publication named 'MavenPublication' because this container does not support creating elements by name alone. Please specify which subtype of Publication to create. Known subtypes are: MavenPublication
I can not decipher that message into anything meaningful. Does anyone understand what could be happening here? If I disable the publishing block, the other aspects of the plugin work fine.