/commands/maven/mvn-archetype-generate
mvn archetype:generate: create a new Maven project from the command line
- tool
- Maven
- versions
- Maven 3.x, maven-archetype-plugin 3.x
- os
- any
mvn archetype:generate creates a new project from a template (an archetype). For a plain Java project, run it with the quickstart archetype and your coordinates: -DgroupId=com.acme -DartifactId=demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false. Maven writes a folder demo with a pom.xml, src/main/java/com/acme/App.java and a matching JUnit test.
Create a project non-interactively
mvn archetype:generate \
-DgroupId=com.acme \
-DartifactId=demo \
-DarchetypeGroupId=org.apache.maven.archetypes \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DarchetypeVersion=1.5 \
-DinteractiveMode=false
cd demo
mvn package-DarchetypeVersion pins the template version, which decides the Java and JUnit versions in the generated POM; leave it out to use the latest one known to your Maven. In Windows PowerShell, put each -D... argument in quotes and use the backtick or one long line instead of \.
Parameters
| Parameter | Meaning |
|---|---|
| -DgroupId=com.acme | Group id; also the base Java package unless -Dpackage is set |
| -DartifactId=demo | Artifact id and the name of the new folder |
| -Dversion=1.0.0-SNAPSHOT | Project version (default 1.0-SNAPSHOT) |
| -Dpackage=com.acme.demo | Java package for the generated classes |
| -DarchetypeArtifactId=maven-archetype-quickstart | Which template to use |
| -DarchetypeVersion=1.5 | Version of the template |
| -DinteractiveMode=false | Do not ask questions; fail if something required is missing |
| -B | Batch mode for the whole Maven run (same effect for prompts) |
Common archetypes
| archetypeArtifactId | Creates |
|---|---|
| maven-archetype-quickstart | A JAR project with App.java and a unit test |
| maven-archetype-webapp | A WAR project with src/main/webapp and web.xml |
| maven-archetype-simple | A minimal project with a site skeleton |
| maven-archetype-archetype | A new archetype of your own |
Pitfalls
- Running inside another project. Generating inside a folder that has a
pom.xmlwithpompackaging adds the new project as a module of that parent. Run it in an empty folder unless that is what you want. - Folder exists. The command fails if a folder with the artifactId already exists.
- Old compiler settings. Older archetype versions write Java 1.7 or older
source/targetvalues, which current JDKs reject with Source option 5 is no longer supported-style errors. Use a recentarchetypeVersionor edit the POM. - Wrong folder afterwards. Maven commands run outside the new folder fail with there is no POM in this directory;
cd demofirst.
How to confirm it worked
mvn package inside the new folder compiles the code, runs the sample test and writes target/demo-1.0-SNAPSHOT.jar. Then run a single test with mvn test -Dtest.
FAQ
Why does archetype:generate list thousands of archetypes?
Without -DarchetypeArtifactId and -DinteractiveMode=false, the plugin runs interactively and shows the full archetype catalog. Name the archetype on the command line to skip the list.
Which Java version does the quickstart project use?
The one written in the generated pom.xml (maven.compiler.release or source/target properties), which depends on the archetype version. Check it right after generation and set it to the JDK you use, otherwise you can get errors such as Source option 5 is no longer supported.
Is there a faster way to create a Spring Boot project?
Spring Boot projects are usually generated with Spring Initializr (start.spring.io or the IDE wizard) rather than a Maven archetype. Use archetype:generate for plain Java projects and organisation-specific templates.