clankerguru/opsx/gradle-dependency-injection
Claude Code and agent skill by clankerguru/opsx
- community
Checking that the SKILL.md file is still in the repository.
Gradle service injection — @Inject, ObjectFactory, ExecOperations, WorkerExecutor, BuildService, configuration-cache-safe patterns.
Install
npx vskill@latest install clankerguru/opsx/gradle-dependency-injectionSKILL.md
The same SKILL.md was published earlier as clankerguru/opsx/gradle-dependency-injection.
Gradle Service Injection
Gradle has a built-in DI container. Tasks, plugins, extensions, build services, and value sources all receive services via @Inject. This replaces the old pattern of pulling things off Project at execution time — critical for configuration-cache compatibility.
Source:
- https://docs.gradle.org/current/userguide/service_injection.html
- https://docs.gradle.org/current/userguide/build_services.html
- https://docs.gradle.org/current/userguide/configuration_cache.html
1. The @Inject rule
On an abstract class (task, extension, service), Gradle constructs an instance and injects:
Or via an abstract getter (no constructor needed):
The getter form is nicer for Kotlin — no constructor boilerplate, and the abstract keyword tells Gradle to synthesise the implementation.
2. Injectable service catalog
| Service | Role | Inject into | |
|---|---|---|---|
ObjectFactory | Create Property, ListProperty, FileCollection etc. | Plugin, Task, Extension, Service | |
ProjectLayout | projectDirectory, buildDirectory, settingsDirectory | Plugin, Task | |
ProviderFactory | gradleProperty, environmentVariable, fileContents, exec | Plugin, Task, Extension, Service | |
ExecOperations | exec { ... }, javaexec { ... } (CC-safe) | Task, Service | |
FileSystemOperations | copy, sync, delete (CC-safe) | Task, Service | |
ArchiveOperations | zipTree, tarTree readers | Task, Service | |
WorkerExecutor | Parallel work submission | Task | |
BuildServiceRegistry | Register/access shared services | Plugin | |
BuildEventsListenerRegistry | Subscribe to build-finished events | Plugin | |
ToolingModelBuilderRegistry | Register IDE tooling models | Plugin | |
ProblemReporter (incubating) | Report structured problems | Plugin, Task | |
FileResolver | Resolve arbitrary Any to files (rare) | Task, Service |
Scope table:
| Inject target | ObjectFactory | ProjectLayout | ProviderFactory | ExecOperations | FileSystemOperations | WorkerExecutor | BuildServiceRegistry | |
|---|---|---|---|---|---|---|---|---|
Plugin<Project> | ✓ | ✓ | ✓ | ✓ | ||||
Plugin<Settings> | ✓ | ✓ | ✓ | |||||
Task | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ||
Extension | ✓ | ✓ | ||||||
BuildService | ✓ | ✓ | ✓ | ✓ | ||||
ValueSource | ✓ | ✓ |
3. Why this matters (configuration cache)
The old way:
The new way:
Same effect, but no Project reference survives to execution time. The configuration cache can serialise the task and reload it without re-running configuration.
4. ExecOperations / FileSystemOperations
Replacements for project.exec, project.javaexec, project.copy, project.sync, project.delete:
ExecOperations.exec returns an ExecResult — capture its exitValue to act on failure.
5. ArchiveOperations
Read-only view of archives — e.g. walk a jar's entries during a task:
6. WorkerExecutor
Parallelise inside a task — see /gradle-tasks §13.
7. BuildService — shared across tasks
A BuildService is a singleton kept alive for the build. Use cases:
- Connection pool to a test database.
- An external tool process (browser, emulator).
- Throttling / semaphore shared across parallel tasks.
Register once (usually in a plugin):
Use from a task:
Rules:
registerIfAbsentreturns aProvider<T>— never the service directly.maxParallelUsagescaps concurrency; default is unbounded.usesService(...)on the task is required for CC; without it Gradle can't tell which tasks depend on which services.AutoCloseable.close()is called when the build finishes.- Parameters are CC-serialised — use lazy
Property/ListProperty.
8. BuildEventsListenerRegistry
Subscribe to task completion events — the CC-safe replacement for gradle.buildFinished { }:
project.serviceOf<T>() is the Kotlin-DSL accessor for pulling a Gradle-internal service — here we need the listener registry.
9. ValueSource — external values with tracked sources
A ValueSource<T, P> computes a value outside the build and declares it as a tracked source for CC.
Why this instead of Runtime.exec? Gradle tracks the value across builds; on CC hit, it re-executes obtain() and compares — if the value is the same, the cache is still valid.
10. Plugin-side: access services from Plugin<Project>
Or explicit injection (useful inside a nested class):
11. Anti-patterns
- Accessing
project.exec,project.copy,project.filefrom inside@TaskAction— breaks CC. Inject the operations service. @Injecton a field (@field:Inject) — wrong target. Use getter (@get:Inject) or constructor.- Storing a
Projectreference in a task field — breaks CC. - Using
System.getenv/System.getPropertyin task code — invisible to CC. Useproviders.environmentVariable/systemProperty. - Calling
gradle.buildFinished { }— legacy and CC-incompatible. UseBuildEventsListenerRegistry+ aBuildService. - Forgetting
usesService(...)on tasks that consume a build service — works until CC is enabled, then fails. - Long-lived mutable state on a
BuildServicewithoutmaxParallelUsages— race conditions under parallel execution. BuildServiceparameters with non-serialisable types — CC fails with a clear error at write time.
12. Quick reference
13. Sub-skill cross-reference
- Lazy values (
Property,ProviderFactory) →/gradle-providers-properties - Task API,
@TaskAction,WorkerExecutorusage →/gradle-tasks - Writing a plugin that registers a
BuildService→/gradle-custom-plugins
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewNot runA model review is scheduled for the next rescan.
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingcli/src/main/resources/content/skills/gradle-dependency-injection/SKILL.md
Popularity
- 7-day trend score
- 0
Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/clankerguru/opsx/gradle-dependency-injection)