Explain the named-argument dependency notation like compile group: 'x', name: 'y', version: 'z', and how it differs from the string notation.
answer
- named args = a single Groovy map
- group/name/version/classifier/ext keys
- string 'g:n:v' parsed to same parts
- compile/runtime legacy -> implementation/api
- map form for computed/extra fields
basics
~10 sNamed-argument notation passes a Groovy map (group:, name:, version:) to the configuration method. The string notation 'group:name:version' is shorthand for the same thing — Gradle parses the colon-separated string into those parts.
solid answer
~30 sGroovy lets you call a method with `key: value` pairs, which it collects into a single `Map` argument. So `implementation group: 'com.google.guava', name: 'guava', version: '33.0.0-jre'` actually calls `implementation([group:..., name:..., version:...])`. Gradle's `DependencyHandler` accepts that map and builds a `ModuleDependency` from it. The compact string form `implementation 'com.google.guava:guava:33.0.0-jre'` is equivalent — Gradle splits the colon-separated coordinate into group/name/version. The map form is more verbose but readable when you need extra keys (e.g. `classifier:`, `ext:`) or want to compute one field; the string form is idiomatic for simple cases. Note `compile`/`runtime` are legacy configurations; modern builds use `implementation`/`api`.
code
groovy · 6 linesdependencies {
// map (named-argument) notation
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.14.0'
// equivalent string notation
implementation 'org.apache.commons:commons-lang3:3.14.0'
}go deeper
Recognize both forms and state they declare the same dependency; know group/name/version.
Explain that named args become a single map and string is parsed to the same parts; know compile->implementation.
Discuss classifier/ext, attaching configuration closures, and when map vs string aids readability/computation.
Recommend a team convention (usually version-catalog aliases over both raw forms) for consistency across many modules.
## Named arguments are just a map Groovy has a syntax convenience: when you call `foo(a: 1, b: 2)`, Groovy gathers the colon pairs into a `LinkedHashMap` and passes it as the **first** argument: `foo([a:1, b:2])`. There is no special 'named parameter' mechanism in the JVM — it's a map. Gradle exploits this so a dependency can be declared field-by-field: ```groovy implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.14.0' ``` This calls the `implementation` method (a dynamically-added configuration method on `DependencyHandler`) with a single map. Gradle reads the well-known keys — `group`, `name`, `version`, and optionally `classifier` and `ext` — to construct an `ExternalModuleDependency`. ## The string (coordinate) notation The equivalent compact form is: ```groovy implementation 'org.apache.commons:commons-lang3:3.14.0' ``` Gradle parses the `group:name:version` (optionally `:classifier@ext`) string into the same parts. Functionally identical to the map; just terser. ## When each is preferable - **String notation** — default for normal, fully-specified coordinates. Concise, copy-pasteable from Maven Central. - **Map notation** — when you need a key that doesn't fit cleanly in the colon string, when you want to build a field from a variable (`version: libVersion`), or for readability in a team that prefers explicit fields. You can also attach a configuration closure: `implementation('g:n:v') { exclude group: 'x' }`. ## Legacy `compile`/`runtime` Interviewers use `compile group:...` because it's the classic example, but `compile` and `runtime` were **removed** for dependency declaration in Gradle 7+. Modern Java builds use `implementation`, `api`, `compileOnly`, `runtimeOnly`, `testImplementation`. The notation mechanics are identical; only the configuration name changed. ## Why it matters Understanding that both forms are sugar over the same handler call explains why you can mix them, why a typo in a key (`nme:`) silently does nothing useful, and why the string form is just a parse of the same coordinate.
- Why are compile and runtime no longer valid for declaring dependencies?They were deprecated then removed in Gradle 7; they didn't model the api/implementation distinction. Use implementation, api, compileOnly, runtimeOnly, and testImplementation instead.
- When would you choose the map notation over the string notation?When you need a key awkward in the colon string (classifier, ext), when computing a field from a variable, or to attach a configuration closure to exclude transitives.
saying these in an interview costs you the question
- Claiming named arguments are a JVM-level named-parameter feature rather than a Groovy map.
- Saying compile is still the recommended configuration in modern Gradle.