gradle/gradle · error · InvalidUserDataException
Publication with name '%s' added multiple times
Error message
Publication with name '%s' added multiple times
What it means
PublicationContainer is Gradle's polymorphic container for publishing publications; domain object containers enforce name uniqueness, and this container overrides handleAttemptToAddItemWithNonUniqueName to throw InvalidUserDataException as soon as a second publication with an existing name is added.
Source
Thrown at platforms/software/publish/src/main/java/org/gradle/api/publish/internal/DefaultPublicationContainer.java:33
*/
package org.gradle.api.publish.internal;
import org.gradle.api.InvalidUserDataException;
import org.gradle.api.internal.CollectionCallbackActionDecorator;
import org.gradle.api.internal.DefaultPolymorphicDomainObjectContainer;
import org.gradle.api.publish.Publication;
import org.gradle.api.publish.PublicationContainer;
import org.gradle.internal.reflect.Instantiator;
public class DefaultPublicationContainer extends DefaultPolymorphicDomainObjectContainer<Publication> implements PublicationContainer {
public DefaultPublicationContainer(Instantiator instantiator, CollectionCallbackActionDecorator collectionCallbackActionDecorator) {
super(Publication.class, instantiator, instantiator, collectionCallbackActionDecorator);
}
@Override
protected void handleAttemptToAddItemWithNonUniqueName(Publication o) {
throw new InvalidUserDataException(String.format("Publication with name '%s' added multiple times", o.getName()));
}
}
View on GitHub (pinned to 534f27719b)
Solutions
- Use unique publication names ('maven', 'mavenJava', 'mavenKotlin')
- Configure the existing publication instead of re-creating it: publishing.publications.named('maven') { ... }
- Check existence first: if (publishing.publications.findByName('maven') == null) { ... create ... }
- Print publishing.publications.names during configuration to find which script/plugin registers the duplicate
Example fix
// before
def names = ['maven', 'maven']
names.each { n ->
publishing.publications.create(n, MavenPublication) { from components.java } // second add throws
}
// after
def names = ['maven', 'mavenExtra']
names.each { n ->
publishing.publications.create(n, MavenPublication) { from components.java }
} Defensive patterns
Strategy: validation
Validate before calling
def addPublication = { String name, Class type, Closure cfg ->
if (publishing.publications.findByName(name) != null) {
throw new GradleException("Publication '$name' already exists - choose another name")
}
publishing.publications.create(name, type, cfg)
} Prevention
- Generate publication names from a de-duplicated list or map key
- Prefer named('x') { ... } to reconfigure an existing publication instead of create()
- When writing a plugin, check publications.findByName(name) before creating
When it happens
Trigger: publishing { publications { maven(MavenPublication) { ... }; maven(MavenPublication) { ... } } }, or register('maven') followed by create('maven'), or two applied plugins each creating a publication named 'maven' in the same project.
Common situations: A custom plugin plus maven-publish both creating publications with the same name; loops generating publications from lists that contain duplicate names; refactors leaving a create() call next to an existing register() call.
Related errors
- Cannot add feature variant '{}' as a variant with the same n
- Maven publication '%s' cannot include multiple components
- multiple artifacts with the identical extension and classifi
- A dependency with alias '{}' already exists for module '{}:{
- Cannot enable dependency mapping without configuring a resol
AI-assisted analysis of gradle/gradle@534f27719b (2026-08-22).
Data as JSON: /api/errors/f03082582d86067a.
Report an issue: GitHub.