Maven Archetype Plugin

In the previous tutorial, we covered Maven Resources Plugin. In this tutorial, we introduce Maven Archetype Plugin.

An archetype is a template for a Maven project which is used by the Maven Archetype plugin to create new projects. Simply put, it generates skeleton for different types of projects.

To better understand, lets create a new project using Maven Archetype Plugin.

$ mvn archetype:generate 
-DgroupId=com.xyz 
-DartifactId=my-app 
-Dversion=1.0 
-DpackageName=com.xyz 
-DarchetypeGroupId=org.apache.maven.archetypes 
-DarchetypeArtifactId=maven-archetype-quickstart 
-DarchetypeVersion=1.0 
-DinteractiveMode=false
Maven Archetype Plugin - quickstart dir structure

The archetype plugin creates a new sample Java project named my-app with source and test directories with sample files and pom.xml. The plugin uses archetype org.apache.maven.archetypes:maven-archetype-quickstart:1.0 as the template.

The above command invokes archetype:generate goal of maven-archetype-plugin. We pass various parameters to the goal (through -D) and meaning of these parameters are:

  • groupId, artifactId, version - defines the coordinates of the new project and com.xyz:my-app:1.0 is the Maven coordinates of my-app project. The goal uses artifactId to name the project.

  • packageName - defines the package in the project. It creates a package named com.xyz for my-app project.

  • archetypeGroupId, archetypeArtifactId, archetypeVersion

    • defines the coordinates of archetype to be used as template. It uses quickstart archetype which is template for Java application with sample files.
 
 

Note

So far in the guide, we avoided the use of Maven Archetype Plugin so that beginners can have a better grasp of things by hand coding the project. The directory structure and sources of the Simple App Project, we hand coded in the earlier chapters is same as the one created by quickstart archetype.

Maven Archetype Plugin - webapp dir structure

Some of the commonly used archetypes are

  • maven-archetype-quickstart - creates a Java project with sample files.

  • maven-archetype-simple - creates a Java project without sample files.

  • maven-archetype-webapp - creates a Web App project as shown in the screenshot.

There are hundreds of archetypes in Maven repository contributed by different software projects. We can use following command to enter interactive mode and select the required archetype for our project.

$ mvn archetype:generate -DgroupId=com.xyz -DartifactId=my-app -Dversion=1.0 -DinteractiveMode=true

Above, we have not provided the archetype to use instead we use interactive mode and select the archetype. In interactive mode, plugin shows a very long list of archetype (more than five hundred archetypes) and prompt us enter the archetype number. In the prompt, we can enter some text such as quickstart, java etc., to filter the long list of archetypes and to show only the matching archetypes. From the filtered list, select from the archetype number and enter it to generate the project from that template.

 
 

Maven Archetype Plugin is also useful to generate multi-module project and we cover this in a later chapter.

In the next tutorial, we cover project distribution.