MavenPublication fails when moving publishing config from build script to plugin


(Jonas Gustavsson) #1

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.


(Chris Doré) #2

I recall possibly seeing this as well and I think it was because I missed adding an import for the org.gradle.api.publish.maven.MavenPublication interface. Do you have an import for it in the plugin source file?


(Jonas Gustavsson) #3

OMG, of course! This was the case, and the errror message even makes perfect sense now.

In the light of this, I am convinced I should move my plugins suite to Java, where I can employ an IDE to watch my back for embarrassing things like missing imports.

Thanks a bunch for taking the time to reply Chris!
Please enjoy this complementary wild boar: :boar:


(Chris Doré) #4

I use Groovy for plugins and IntelliJ seems to work fine. If you’re an Eclipse user, have you tried the Buildship plugin?