skip to content

What is a Maven archetype, and how do you use archetype:generate to scaffold a new project?

level: juniorimportance: must knowfreq 55%

answer

  1. project template = artifact (g/a/v)
  2. mvn archetype:generate
  3. asks groupId/artifactId/version/package
  4. quickstart vs webapp
  5. -DinteractiveMode=false for CI

basics

~10 s

An archetype is a project template. You run mvn archetype:generate, pick a template, and answer prompts for groupId, artifactId and version; Maven creates a ready-to-build project skeleton from that template.

solid answer

~40 s

A Maven archetype is a reusable project template packaged as its own artifact (groupId/artifactId/version). The `maven-archetype-plugin` consumes it: `mvn archetype:generate` runs an interactive wizard where you choose an archetype from a catalog and supply the new project's coordinates (groupId, artifactId, version, package). The plugin then materializes a fresh directory tree — pom.xml, source folders, sample classes/resources — substituting your values into the template's placeholders. You can skip the wizard by passing `-DarchetypeGroupId`, `-DarchetypeArtifactId`, `-DarchetypeVersion` plus `-DgroupId`, `-DartifactId` and `-DinteractiveMode=false` for CI or scripted use. Common archetypes include `maven-archetype-quickstart` (a basic JAR app with JUnit) and `maven-archetype-webapp`. It standardizes layout and conventions across teams, so every new module starts the same way.

code

bash · 8 lines
bash
mvn archetype:generate \
  -DarchetypeGroupId=org.apache.maven.archetypes \
  -DarchetypeArtifactId=maven-archetype-quickstart \
  -DarchetypeVersion=1.4 \
  -DgroupId=com.example \
  -DartifactId=my-app \
  -Dversion=1.0.0-SNAPSHOT \
  -DinteractiveMode=false

go deeper

for a junior

Knows archetype = template and can run archetype:generate interactively to scaffold a project.

for a middle

Can drive it non-interactively with full coordinates and knows quickstart/webapp.

for a senior

Uses archetypes to standardize team layout; weighs them against alternatives like a shared parent POM or copier/cookiecutter.

for a principal

Defines org-wide scaffolding strategy, governs archetype versioning and catalog hosting, and decides when archetypes vs other tooling are the right standardization mechanism.

## What an archetype is A **Maven archetype** is a *project template*. Instead of hand-creating `pom.xml`, the `src/main/java` and `src/test/java` folders, and boilerplate files for every new project, you generate them from a template. An archetype is itself a normal Maven artifact identified by coordinates: a **groupId** (organization namespace), an **artifactId** (the template's name), and a **version**. ## The plugin and the generate goal The `maven-archetype-plugin` provides the machinery. The headline goal is `archetype:generate`: - It shows (or you preselect) an archetype from a **catalog** — a list of available templates. - It asks for the *new* project's coordinates: `groupId`, `artifactId`, `version`, and `package` (the base Java package). - It copies the template's files into a new folder named after your `artifactId`, replacing placeholders with your values. ## Interactive vs batch mode By default the wizard is interactive. For automation you pass everything on the command line and disable prompts with `-DinteractiveMode=false` (sometimes written `-B`/`--batch-mode`). You must then specify both the archetype's coordinates and the new project's coordinates. ## Common built-in archetypes - `maven-archetype-quickstart` — minimal JAR project with an `App` class and a JUnit test. - `maven-archetype-webapp` — WAR project with `web.xml`. - Many frameworks (Spring Boot historically, Scala, etc.) ship their own. ## Why teams use them Archetypes enforce **consistency**: identical directory layout, parent POM, plugin versions, and conventions for every new module. That reduces onboarding friction and copy-paste drift. ```bash # Interactive wizard mvn archetype:generate # Fully scripted (no prompts) mvn archetype:generate \ -DarchetypeGroupId=org.apache.maven.archetypes \ -DarchetypeArtifactId=maven-archetype-quickstart \ -DarchetypeVersion=1.4 \ -DgroupId=com.example \ -DartifactId=my-app \ -Dversion=1.0.0-SNAPSHOT \ -Dpackage=com.example.myapp \ -DinteractiveMode=false ```

  • How do you run archetype:generate non-interactively in a CI pipeline?
    Pass all archetype coordinates (-DarchetypeGroupId/ArtifactId/Version) and the new project coordinates (-DgroupId/-DartifactId/-Dversion), and add -DinteractiveMode=false (or -B) so no prompts appear.
  • What is the difference between maven-archetype-quickstart and maven-archetype-webapp?
    quickstart produces a plain JAR project with a sample App class and JUnit test; webapp produces a WAR-packaged project with a web.xml and webapp directory layout for servlet/JSP apps.

Like a 'New Project from template' file menu, but the templates are versioned artifacts you can publish and share.

saying these in an interview costs you the question

  • Thinking an archetype is a Maven plugin you write code in — it is a template artifact consumed by the archetype plugin.
  • Believing archetypes are required to create a Maven project (you can write pom.xml by hand).

context