Has someone seen this crash after upgrading to Gra...
# community-support
j
Has someone seen this crash after upgrading to Gradle v9? The full stacktrace does not point to any code I own in my convention plugins 🤔
Copy code
* What went wrong:
A problem occurred configuring project ':semver-gradle-plugin'.
> Failed to notify project evaluation listener.
   > Cannot mutate the hierarchy of configuration ':semver-gradle-plugin:apiElements' after the configuration was published as a variant. After a configuration has been observed, it should not be modified.
   > Gradle Module Metadata can't be modified after an eagerly populated publication.
I have a workaround to remove duplicated artifacts and it is causing the issue, but I don't have clear how to fix it
Copy code
private fun removeDuplicatedPublications() {
    configure<PublishingExtension> {
        publications.withType<MavenPublication>().configureEach { publication ->
            if (publication.name == "maven") {
                val distinctArtifacts: List<MavenArtifact> =
                    publication.artifacts.distinctBy { artifact -> artifact.classifier }
                publication.artifacts.clear()
                publication.artifacts.addAll(distinctArtifacts)
            }
        }
    }
}
Fixed with:
Copy code
private fun removeDuplicatedPublications() {
    configure<PublishingExtension> {
        publications.withType<MavenPublication>().whenObjectAdded { publication ->
            if (publication.name == "maven") {
                val distinctArtifacts: List<MavenArtifact> =
                    publication.artifacts.distinctBy { artifact -> artifact.classifier }
                publication.artifacts.clear()
                publication.artifacts.addAll(distinctArtifacts)
            }
        }
    }
}
Well, a different error is happening, it is becoming really annoying to support publishing a Gradle plugin into both, Gradle plugin portal and MavenCentral 😢
v
That "fixed" it, because you either register that publication before you do that call and this your call does nothing at all, it because you switched from lazy configuration-avoiding API to eager API. If it is the later, you should probably use
all
instead to remove the ordering constraint.
j
I tried
all
too, but it does not work
Same error,
whenObjectAdded
was fixing the error generated by Gradle 9.0.0, but the artifacts that were duplicated were not being correctly deleted, so it was generating task dependencies issues
v
And besides that, you should probably use
publications.withType<MavenPublication>().named { it == "maven" }...
instead of matching all and filtering inside.
j
Yeah, I did that too, but same issue
v
With
configureEach
, sure, there should be no change in behavior
j
Both, that and
all
v
Then your fix is probably a noop and that's why it fixes it
j
Yeah, currently there is no fix
Is it possible to "clone" the publication, remove the initial one. Modify the cloned one without the duplicated artifacts, and add it?
In any case, calling
named("maven")
generates the issue (if not
all
would work)...
v
Is it possible to "clone" the publication, remove the initial one. Modify the cloned one without the duplicated artifacts, and add it?
Most probably not. And even if it were, you write says you try to modify it after it was used for something already, do that would probably be a very bad idea.
In any case, calling
named("maven")
generates the issue (if not all would work)...
named("maven")
(eager) !=
named { it == "maven" }
(lazy). But anyway, if it fails with
named("maven")
, then it means that at the time you call this the publication is already registered and also already used, so no matter what code you use will not work. This would for example be the case when you call that in an
afterEvaluate
while the code that used that publication also run in an earlier
afterEvaluate
or similar. But hard to say without an MCVE what the actual problem might be.
j
I (hope) fixed it without the need to remove the publication. Anyway thank you!
👌 1