(deferred) configuration behavior for maven-publish

Hallo All,
since we have upgraded from gradle4 to the current version we have a changed behavior for the evaluation of publishing.publications, because there are changes in gradle >5.0
https://docs.gradle.org/current/userguide/publishing_maven.html#publishing_maven:deferred_configuration
But how do you get the code to run like it did before

The publishing array during the init-phase:

rootProject {
    allprojects {
        afterEvaluate { project ->
            if (project.plugins.hasPlugin("maven-publish")) {
                def publicationNames = project.publishing.publications.collect { it.name }.toArray()
                println("During: Init-Phase: ${project}:${publicationNames}")
            }
        }
    }
}

produces:

[pluginMaven]

the same code, later in the root projects build.gradle produces:

[myPluginPluginMarkerMaven, pluginMaven]

which I had also expected in the previous phase and which was the behavior with gradle 4.10.3

How to fix this?

Kind regards,

Tom

The correct advice heavily depends on your actual use-case.
Using afterEvaluate is almost always wrong and usually just introduces race conditions as you have observed.
In your concrete example you could for example do:

rootProject {
    allprojects {
        pluginManager.withPlugin("publishing") {
            publishing.publications.configureEach {
                println("$project:$it.name")
            }
        }
    }
}
1 Like

Hi Björn,
this is a great solution and it works perfectly to configure the artifactoryPublish-plugin during the initialisation phase (with was actually my use-case).
Thanks a lot,
Tom

Just to be clear, you are not configuring it during initialization phase.
You schedule the configuration action in the initialization phase.
But the configuration is done in the configuration phase as soon as the publishing plugin is applied. :slight_smile: