gradle/gradle · error · InvalidUserCodeException
Build was configured to prefer settings component metadata…
Error message
Build was configured to prefer settings component metadata rules over project rules but rule '${ruleName}' was added by ${displayName} What it means
Gradle's dependencyResolutionManagement can be configured with rulesMode FAIL_ON_PROJECT_RULES (the default since Gradle 7), which requires all component metadata rules to be declared in settings.gradle rather than in projects. When a project-scoped script or plugin registers such a rule (e.g. via dependencies.components), ruleMutationDisallowedOnProject fires an InvalidUserCodeException naming the rule and the project that added it. The goal is consistent dependency resolution across all projects in the build.
Solutions
- Move the offending rule from the project build script into settings.gradle under dependencyManagement: dependencyResolutionManagement { components { withModule('group:name') { ... } } }
- Use the rule name and displayName from the message to locate the declaring script/plugin (grep build files and plugin sources for components.withModule / components.all)
- Temporarily switch rulesMode to preferSettings() in settings.gradle to downgrade the failure to a warning while you migrate
- Pin or patch the plugin that registers project-scoped rules until it supports settings-scoped registration
Example fix
// before (build.gradle of a project):
dependencies {
components {
withModule('com.google.guava:guava') { versionSelection { rejectVersion { it.version == 'bad' } } }
}
}
// after (settings.gradle):
dependencyResolutionManagement {
components {
withModule('com.google.guava:guava') { versionSelection { rejectVersion { it.version == 'bad' } } }
}
} Defensive patterns
Strategy: validation
Validate before calling
// settings.gradle — fail fast with a clearer message during migration
import org.gradle.api.GradleException
gradle.beforeProject { p ->
def script = p.file('build.gradle')
if (script?.exists() && script.text =~ /(?m)^\s*components\s*\{/) {
throw new GradleException("Project ${p.path} declares project-scoped component metadata rules; move them to settings.gradle")
}
} Prevention
- Declare all component metadata rules only in settings.gradle dependencyResolutionManagement { components { } }
- During migration, set rulesMode { preferSettings() } and eliminate the warnings project by project
- Audit third-party plugins for dependencies.components usage before applying them to projects
When it happens
Trigger: settings.gradle uses the default/declared failOnProjectRules() rulesMode, and build code calls dependencies.components.all(...) or dependencies.components.withModule(...) inside a project's build script, or a plugin applied to a project registers components { } rules itself. The displayName in the message is the project (build script) that executed the rule registration.
Common situations: Upgrading to Gradle 7+ where project-scoped component metadata rules stopped being silently allowed; in-house or third-party plugins that call project.getDependencies().getComponents() from project scope; builds migrating resolution logic toward centralized settings.gradle dependencyResolutionManagement blocks.
Related errors
- Build was configured to prefer settings component metadata…
- Cannot add capability
- Configuration ' ' not defined in module
- Could not add a component metadata rule for module
- Could not register artifact transform
AI-assisted analysis of gradle/gradle@534f27719b (2026-08-22).
Data as JSON: /api/errors/56fc4da32ed8d3f4.
Report an issue: GitHub.
Appendix: source
Thrown at platforms/software/dependency-management/src/main/java/org/gradle/internal/management/DefaultDependencyResolutionManagement.java:213
throw new InvalidUserCodeException(message);
case PREFER_SETTINGS:
LOGGER.warn(message);
break;
default:
break;
}
}
private void ruleMutationDisallowedOnProject(DisplayName ruleName) {
UserCodeApplicationContext.Application current = context.current();
Describable displayName = current == null ? null : current.getSource().getDisplayName();
if (displayName == null) {
displayName = UNKNOWN_CODE;
}
String message = "Build was configured to prefer settings component metadata rules over project rules but rule '" + ruleName + "' was added by " + displayName;
switch (getConfiguredRulesMode()) {
case FAIL_ON_PROJECT_RULES:
throw new InvalidUserCodeException(message);
case PREFER_SETTINGS:
LOGGER.warn(message);
break;
default:
break;
}
}
}
View on GitHub (pinned to 534f27719b)