What does the `--type` flag control in `gradle init`, and what happens if you omit it?
answer
- selects the project template
- java-application / kotlin-library / basic / pom
- decides plugins + source skeleton
- omit → prompt or detect pom.xml
- narrows other flag choices
basics
~10 s--type selects the project template to scaffold—e.g. java-application, kotlin-library, java-library, basic. It determines the plugins, source layout, and sample code generated. Omitting it makes init prompt interactively (or infer from an existing pom.xml).
solid answer
~40 s`--type` chooses which **project template** the build-init plugin generates. Common values: `basic` (just build + settings, no source), `java-application`, `java-library`, `kotlin-application`, `kotlin-library`, `groovy-library`, `scala-library`, `cpp-application`, and `pom` (migrate a Maven project). The type decides which plugins go into the generated `build.gradle(.kts)` (e.g. `application` vs `java-library`), the `src/main`/`src/test` skeleton and sample classes, and sensible defaults. If you omit `--type` in an empty directory, `init` runs interactively and asks you to pick. If the directory already contains a Maven `pom.xml`, `init` detects it and defaults to converting the Maven build (the `pom` type), porting dependencies and modules into Gradle scripts. Some newer/experimental types are only listed when you also pass `--incubating`.
code
bash · 8 lines# Library, Kotlin DSL, JUnit 5
gradle init --type kotlin-library --dsl kotlin --test-framework junit-jupiter
# Minimal scaffold with no source
gradle init --type basic --dsl kotlin
# Convert an existing Maven build
gradle init --type pomgo deeper
Recognize a few common types (java-application, java-library) and that --type picks the template.
Enumerate the main types, explain what each generates, and how omitting --type triggers prompts or Maven detection.
Discuss how type constrains other flags and when to use basic vs pom migration in real onboarding/tooling flows.
Consider authoring custom/standardized scaffolds (org templates) since built-in types may not capture company conventions.
## The role of `--type` `--type` is the single most consequential `init` flag: it picks the **build-init template** that drives everything else. Each template bundles a coherent set of choices—plugins, source layout, sample code, and default test framework. ## Common types - `basic` — only `settings.gradle(.kts)` + an empty `build.gradle(.kts)`. No source, no language plugin. Good as a starting point you'll customize. - `java-application` / `kotlin-application` / `groovy-application` — applies the `application` plugin, generates a runnable `App` class with a `main`, and wires `run`. - `java-library` / `kotlin-library` / etc. — applies the `java-library` plugin, generates a `Library` class exposing an `api`/`implementation` split. - `pom` — migrates an existing Maven `pom.xml` into Gradle, mapping dependencies and multi-module structure. - `cpp-application`, `cpp-library`, `scala-library`, `swift-*` — language-specific variants. ## What omitting it does Without `--type`: 1. **Empty directory** → interactive prompt asks you to choose a type (and then implementation language for some). 2. **Directory with a `pom.xml`** → `init` auto-detects Maven and offers/uses the `pom` conversion. 3. **`--type` plus an unsupported value** → the task fails listing the valid types. ## Interaction with other flags The chosen type narrows the legal values for the *other* prompts. For example, `--type java-library` still lets you pick `--dsl kotlin|groovy` and `--test-framework junit|junit-jupiter|testng|spock` (Spock only for Groovy/JVM), while `--type basic` ignores test-framework entirely because it generates no source. ```bash gradle init --type kotlin-library --dsl kotlin --test-framework junit-jupiter ``` ## Incubating types A few cutting-edge templates surface only with `--incubating`, which also opts the generated build into the latest, possibly-unstable conventions.
- What does `--type basic` generate compared to `java-application`?`basic` writes only settings + an empty build script with no source skeleton or language plugin; `java-application` adds the `application` plugin, a runnable `App` class, and test sources.
- What happens if you run `init` with no `--type` in a folder containing a `pom.xml`?`init` detects the Maven build and converts it to Gradle (the `pom` type), porting dependencies and module structure.
saying these in an interview costs you the question
- Thinking `--type` only sets a language and not the whole plugin/layout template.
- Assuming `basic` generates source files—it does not.