What is Archetype in Maven?

Archetype is a Maven project templating toolkit. Archetype works to provide a system that provides a consistent means of generating Maven projects.

An archetype is defined as an original pattern or model from which all other things of the same kind are made.

In our last article, we saw how to generate first maven project. While creating new project, we use archetype in command to apply a template to our project folder structure.

Within the Maven project, it uses archetypes to show and get users up and running as quickly as possible by providing a sample project that demonstrates many of the features of Maven, while introducing new users to the best practices employed by Maven. Though there are many predefined archetypes, in real life you may want to standardize J2EE development within your organization, so you may want to provide archetypes for EJBs, or WARs, or for your web services. Once these archetypes are created and deployed in your organization’s repository, they are available for use by all developers within your organization.

Using an Archetype

To create a new project based on an Archetype, you need to call mvn archetype:generate goal, like the following:

mvn archetype:generate

Creating Project from Archetype

Creating a project from an archetype involves three steps:

  • prepare repository reference
  • the selection of the archetype,
  • the configuration of that archetype,
  • the effective creation of the project from the collected information.

Usage

Calling archetype:generate the plugin will first ask to choose the archetype from the internal catalog. Just enter the number of the archetype.

It then asks you to enter the values for the groupId, the artifactId and the version of the project to create and the base package for the sources.

It then asks for confirmation of the configuration and performs the creation of the project.

Steps to Create Project using Archetype Interactive mode

Open Command Prompt and navigate to directory where you would like to create project as below,

mvn archetype:generate

When above command is provided, it will list all the archetypes available in Maven repository as below. In my case, it has 1338 archetypes – I’m just going to take the first and last page of the result after above command.

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.0.1:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.0.1:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.0.1:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
1: remote -> am.ik.archetype:maven-reactjs-blank-archetype (Blank Project for React.js)
2: remote -> am.ik.archetype:msgpack-rpc-jersey-blank-archetype (Blank Project for Spring Boot + Jersey)
3: remote -> am.ik.archetype:mvc-1.0-blank-archetype (MVC 1.0 Blank Project)
4: remote -> am.ik.archetype:spring-boot-blank-archetype (Blank Project for Spring Boot)
5: remote -> am.ik.archetype:spring-boot-docker-blank-archetype (Docker Blank Project for Spring Boot)
6: remote -> am.ik.archetype:spring-boot-gae-blank-archetype (GAE Blank Project for Spring Boot)
7: remote -> am.ik.archetype:spring-boot-jersey-blank-archetype (Blank Project for Spring Boot + Jersey)
8: remote -> am.ik.archetype:spring-fu-jafu-blank-archetype (Blank project for Vanilla Spring WebFlux.fn)
9: remote -> am.ik.archetype:vanilla-spring-webflux-fn-blank-archetype (Blank project for Vanilla Spring WebFlux.fn)
10: remote -> at.chrl.archetypes:chrl-spring-sample (Archetype for Spring Vaadin Webapps)
11: remote -> biz.turnonline.ecosystem:turnonline-ecosystem-microservice-archetype (TurnOnline.biz Ecosystem: Cloud Toolkit Microservice Archetype)
12: remote -> br.com.address.archetypes:struts2-archetype (an archetype web 3.0 + struts2 (bootstrap + jquery) + JPA 2.1 with struts2 login system)

Likewise, it scans all archetypes and ends as below,

2355: remote -> us.fatehi:schemacrawler-archetype-maven-project (-)
2356: remote -> us.fatehi:schemacrawler-archetype-plugin-command (-)
2357: remote -> us.fatehi:schemacrawler-archetype-plugin-dbconnector (-)
2358: remote -> us.fatehi:schemacrawler-archetype-plugin-lint (-)
2359: remote -> ws.osiris:osiris-archetype (Maven Archetype for Osiris)
2360: remote -> xyz.luan.generator:xyz-gae-generator (-)
2361: remote -> xyz.luan.generator:xyz-generator (-)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 1338:

From the list of archetypes, select a number which suits your project template.

In this case, refer point(1)  1339 is choosed which is a simple maven project and its asks for version of maven project.

Point(2) refers to questions such as ‘groupId’, ‘artifactId’, ‘version’, ‘package’.

Points(3) refers to confirmation from maven regarding project details.

When the confirmation is given, project is created using archetype selected.

maven command steps

Filtering to reduce archetype list

As of version 2.1, you can reduce the list of displayed archetypes. The filter use the following format: [groupId:]artifactId. If you use single word without :, only artifactId will be checked. The filtering applied is a case sensitive contains on the artifactId (and groupId if set).

Two options

  1. With a mojo parameter:
$ mvn archetype:generate -Dfilter=org.apache:struts

The displayed list will contain only archetypes with a groupId containing org.apache AND an artifactId containing struts

2. Through the prompt:

$ mvn archetype:generate

The full list is displayed and in the prompt response, you will be able to answer with a filter.

Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): org.apache:struts

Notice that if your filter doesn’t match any archetype, the previous list will be displayed again.

Conclusion

Hence we saw the importance and role of archetype in creation of project.

Please follow and like us:
error