Skip to content

/commands/maven/mvn-archetype-generate

mvn archetype:generate: create a new Maven project from the command line

The commands on this page have not been executed by our verification runner yet.updated
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

bash
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

ParameterMeaning
-DgroupId=com.acmeGroup id; also the base Java package unless -Dpackage is set
-DartifactId=demoArtifact id and the name of the new folder
-Dversion=1.0.0-SNAPSHOTProject version (default 1.0-SNAPSHOT)
-Dpackage=com.acme.demoJava package for the generated classes
-DarchetypeArtifactId=maven-archetype-quickstartWhich template to use
-DarchetypeVersion=1.5Version of the template
-DinteractiveMode=falseDo not ask questions; fail if something required is missing
-BBatch mode for the whole Maven run (same effect for prompts)

Common archetypes

archetypeArtifactIdCreates
maven-archetype-quickstartA JAR project with App.java and a unit test
maven-archetype-webappA WAR project with src/main/webapp and web.xml
maven-archetype-simpleA minimal project with a site skeleton
maven-archetype-archetypeA new archetype of your own

Pitfalls

  • Running inside another project. Generating inside a folder that has a pom.xml with pom packaging 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/target values, which current JDKs reject with Source option 5 is no longer supported-style errors. Use a recent archetypeVersion or edit the POM.
  • Wrong folder afterwards. Maven commands run outside the new folder fail with there is no POM in this directory; cd demo first.

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.

Sources