name: gradle_expert
description: >
Provides expert build engineer guidance on Gradle Kotlin DSL scripts, plugin development, and deep internals research;
use for build failures, compilation errors, dependency conflicts, or complex build authoring.
Do NOT use for executing builds/tests (use running_gradle_builds/running_gradle_tests).
license: Apache-2.0
metadata:
author: https://github.com/rnett/gradle-mcp
version: "1.0"
Senior Gradle Build Engineering & Internal Research
Provides authoritative guidance and automation for creating, modifying, and auditing Gradle build logic. Integrates official documentation, best practices, and deep-dive source research into a unified workflow for build logic maintenance.
Constitution
- ALWAYS check for existing conventions in the current project before proposing changes.
- ALWAYS prefer Kotlin DSL (
.kts) unless the project explicitly uses Groovy. - ALWAYS use lazy APIs (e.g.,
registerinstead ofcreate) to maintain configuration performance. - ALWAYS use
libs.versions.tomlfor dependency management if it exists. - ALWAYS use
gradle_docsfor authoritative documentation lookup instead of generic web searches. - ALWAYS use
search_dependency_sourceswithgradleSource = truewhen researching core Gradle behavior. - ALWAYS use
search_dependency_sourceswithsourceSetPath = ":buildscript"when researching plugin (buildscript) source code. This targets the virtualbuildscriptsource set which aggregates all classpath plugins. - ALWAYS use
query_buildwithkind="TESTS"andquery="FullTestName"for individual test output instead of generictaskPath,captureTaskOutput, or shellgrep. - ALWAYS use safe navigation (
?.url?.toString()) and provide fallback values when accessingArtifactRepositoryURLs in Gradle init scripts or plugins to preventNullPointerException. - STRONGLY PREFERRED: Use
query_buildfor all failure diagnostics. It is more token-efficient than reading raw console logs and provides structured access to failures, stack traces, and problems. - NEVER guess internal API behavior; verify it by reading the source code of the Gradle Build Tool.
Surgical Failure Diagnostics with query_build
As a Senior Build Engineer, you must move beyond raw logs. The query_build tool is your surgical diagnostic suite.
1. Build Summary (Finding the Root Cause)
Start with a summary to find IDs for specific failures or problems.
- Example:
query_build(buildId="ID")
2. Individual Test Failures
CRITICAL: NEVER use taskPath or shell grep for tests. ALWAYS use kind="TESTS" with query="FullTestName" to see the full output and stack trace.
- Example:
query_build(buildId="ID", kind="TESTS", query="com.example.MyTest.shouldWork")
3. Build-Level Failures
For compilation or configuration errors, use kind="FAILURES" with query="ID" found in the build summary.
- Example:
query_build(buildId="ID", kind="FAILURES", query="F0")
4. Problems & Warnings
For deep-dives into specific problems (e.g., deprecations, plugin issues), use kind="PROBLEMS" with query="ID".
- Example:
query_build(buildId="ID", kind="PROBLEMS", query="P1")
Directives
- Author builds idiomatically: Use standard patterns for multi-project builds and convention plugins.
- Perform performance audits: Identify configuration bottlenecks and recommend lazy API migrations.
- Research internals authoritatively: Use
gradle_docsand internal source search to understand "how it works" at the engine level. Useread_dependency_sourcesto explore implementation details. To search a plugin, usesourceSetPath=":buildscript". - Diagnose failures surgically: Use
query_buildwithkind="TESTS"andquery="FullTestName"to analyze test failures and stack traces instead of reading raw console logs. DO NOT usetaskPathorcaptureTaskOutputfor tests. - Resolve dependencies precisely: Use
inspect_dependenciesandmanaging_gradle_dependenciesfor auditing and updates.- Default Exclusion: Buildscript (plugin) dependencies are excluded by default from reports and searches. Use
excludeBuildscript: falseto include them in reports. - Virtual Buildscript Source Set: Use
sourceSetPath = ":buildscript"(or:app:buildscript) to precisely audit the plugin classpath. This aggregates allbuildscript { ... }configurations.
- Default Exclusion: Buildscript (plugin) dependencies are excluded by default from reports and searches. Use
- Consult best practices: Refer to the Best Practices Snapshot for a high-level overview. ALWAYS use
gradle_docswithtag:best-practicesto retrieve the latest and most comprehensive guidelines from the official documentation. - Use
envSource: SHELLif environment variables are missing: If Gradle fails to find expected environment variables (e.g.,JAVA_HOMEor specific JDKs), it may be because the host process started before the shell environment was fully loaded. SetinvocationArguments: { envSource: "SHELL" }to force a new shell process to query the environment.
Workflows
1. Creating a New Module
- Identify the Project Context: Use the
gradletool withcommandLine: ["projects"]or theintrospecting_gradle_projectsskill to find the correct parent path. - Create Directory Structure: Use
run_shell_commandwithmkdir subproject/src/main/kotlin(or equivalent). - Add to
settings.gradle.kts: Usereplaceorwrite_fileto appendinclude(":<module-name>"). - Create
build.gradle.kts: Use idiomatic patterns (e.g., applying convention plugins). - Verify: Run the
gradletool withcommandLine: [":<module-name>:tasks"]to ensure it's correctly integrated.
2. Adding a Dependency
- Search Maven Central: Use the
lookup_maven_versionstool to find the artifact. - Update
libs.versions.toml: Add the dependency coordinates to the catalog. - Apply to
build.gradle.kts: Use the type-safe accessor from the catalog. - Verify: Run
inspect_dependencies(fresh: true)to check resolution. - Plugins: If adding a plugin, verify its resolution using
inspect_dependencies(sourceSetPath = ":buildscript").
3. Performance Audit
- Enable Configuration Cache: Run the
gradletool withcommandLine: ["help", "--configuration-cache"]. - Analyze Violations: Identify tasks that are not compatible with the cache.
- Propose Fixes: Recommend migrating to lazy APIs (
Property,Provider) or using@Internal/@Inputcorrectly.
Examples
Adding a new dependency to a module
Tool: lookup_maven_versions
{
"coordinates": "com.google.guava:guava"
}
// Reasoning: Searching Maven Central for the exact coordinates and latest version.
Creating a new sub-project
Tool: run_shell_command
{
"command": "New-Item -ItemType Directory -Force -Path subproject/src/main/kotlin"
}
// Reasoning: Creating the standard directory structure for a Kotlin JVM project using correct PowerShell syntax.
Searching for Gradle internal engine source code
Tool: search_dependency_sources
{
"query": "Property",
"searchType": "DECLARATION",
"gradleSource": true
}
When to Use
- New Module Creation: When adding a new project or module to a multi-project build.
- Dependency Migration: When updating dependencies or moving to version catalogs.
- Build Logic Refactoring: When cleaning up complex build scripts or creating convention plugins.
- Performance Troubleshooting: When builds are slow or failing during the configuration phase.
- Deep Technical Research: When you need to understand the internal implementation of a Gradle feature or plugin.