Publications configure after it has been accessed

I’m quite flumoxed by the dependency between publications and a zip task which generates its artifact. If I have this:

task installer_unix(dependsOn: jar, type: Zip) {

publishing.publications {
	unix(MavenPublication) {
        artifact installer_unix {
              classifier "unix"
              extension "zip"

Then I get the error:

Cannot configure the ‘publishing’ extension after it has been accessed.

But if I reverse the order and put the task second, then I get the error:

Could not find method installer_unix() for arguments…

Argh! Is there a way out of this?

The initial error is not about order of the installer_unix task vs. the publishing.publications configuration in your build script. You’ve written them as expected. Reversing them is worse because you can’t reference the installer_unix as a project property until you’ve defined the task.

The error about configuring the ‘publishing’ extension after it has been accessed suggests that something else in your build is tampering with the publishing extension too late or forcing a deferred action to execute too early. Depending on which case, a stack trace may provide a clue to what is causing this.

Additionally, adding apply plugin: 'maven-publish' and a couple lines to actually create the zip file yields a project that can successfully publish the artifact, so the example provided doesn’t seem to actually reproduce the issue you’re seeing.

Thanks for these clues. I think that solved it. Like so much of gradle, lots of trial and error is needed to figure out the best way to structure things. The docs are useful for the details, but rarely for the big picture.

In my case, I had publishing.repositories defined in the top level gradle but inside an allprojects{} block. I’m never really sure what belongs inside allprojects{} and what doesn’t.