{"record":{"id":"56fc4da32ed8d3f4","repo":"gradle/gradle","slug":"build-was-configured-to-prefer-settings-component","errorCode":null,"errorMessage":"Build was configured to prefer settings component metadata rules over project rules but rule '${ruleName}' was added by ${displayName}","messagePattern":"Build was configured to prefer settings component metadata rules over project rules but rule '(.+?)' was added by (.+?)","errorType":"exception","errorClass":"InvalidUserCodeException","httpStatus":null,"severity":"error","filePath":"platforms/software/dependency-management/src/main/java/org/gradle/internal/management/DefaultDependencyResolutionManagement.java","lineNumber":213,"sourceCode":"                throw new InvalidUserCodeException(message);\n            case PREFER_SETTINGS:\n                LOGGER.warn(message);\n                break;\n            default:\n                break;\n        }\n    }\n\n    private void ruleMutationDisallowedOnProject(DisplayName ruleName) {\n        UserCodeApplicationContext.Application current = context.current();\n        Describable displayName = current == null ? null : current.getSource().getDisplayName();\n        if (displayName == null) {\n            displayName = UNKNOWN_CODE;\n        }\n        String message = \"Build was configured to prefer settings component metadata rules over project rules but rule '\" + ruleName + \"' was added by \" + displayName;\n        switch (getConfiguredRulesMode()) {\n            case FAIL_ON_PROJECT_RULES:\n                throw new InvalidUserCodeException(message);\n            case PREFER_SETTINGS:\n                LOGGER.warn(message);\n                break;\n            default:\n                break;\n        }\n    }\n\n}\n","sourceCodeStart":195,"sourceCodeEnd":223,"githubUrl":"https://github.com/gradle/gradle/blob/534f27719b66953f95cc907aae7f2c1b12f5482d/platforms/software/dependency-management/src/main/java/org/gradle/internal/management/DefaultDependencyResolutionManagement.java#L195-L223","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before (build.gradle of a project):\ndependencies {\n    components {\n        withModule('com.google.guava:guava') { versionSelection { rejectVersion { it.version == 'bad' } } }\n    }\n}\n// after (settings.gradle):\ndependencyResolutionManagement {\n    components {\n        withModule('com.google.guava:guava') { versionSelection { rejectVersion { it.version == 'bad' } } }\n    }\n}","handlingStrategy":"validation","validationCode":"// settings.gradle — fail fast with a clearer message during migration\nimport org.gradle.api.GradleException\ngradle.beforeProject { p ->\n    def script = p.file('build.gradle')\n    if (script?.exists() && script.text =~ /(?m)^\\s*components\\s*\\{/) {\n        throw new GradleException(\"Project ${p.path} declares project-scoped component metadata rules; move them to settings.gradle\")\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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"],"tags":["gradle","dependency-management","component-metadata-rules","settings-gradle","rules-mode"],"backgroundTag":"configuration-precedence-conflict","analyzedSha":"534f27719b66953f95cc907aae7f2c1b12f5482d","analyzedAt":"2026-08-22T08:09:12.375Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}