Introduction to Maven Archetype
Maven is a project management tool and archetype is the toolkit that helps to define the project templates. The archetype can also be considered as the model or pattern that is defined and that can be used further to generate other things in the same manner. These new creations follow the same template structure. These projects help the developers by providing a consistent pattern that comprises of the best practices for creating the project and to generate their projects with parameterized versions of the templates of the project with the help of using the archetypes.
We can even create our artifacts and define our template structure to be followed in the project or organizations. Besides this, there are many archetypes provided by maven providing templates for different project purposes. Also, if such templates are used, it will be easier to work with the projects for new developers as the structure is consistent and understandable due to segregation.
Additive Nature of Archetypes
The archetype facility provided by the maven is additive. That means we can add the new portions and aspects or pieces of the project to the existing project. To give an example of the above statement consider that you have created a quickstart archetype template’s project and then in the future, you felt the necessity to have the site for the project. In this case, it is very easy to inculcate the site model in the existing project with the help of the site archetype. We can perform such operations if we use the archetypes in our project.
Sharing the created archetype across the organization
Suppose, that your organization or company wants to create a project using J2EE standards and a provision for the web services containing EJBs or WARs is to be provided in the archetype or template of the project. You can create such an archetype and then deploy it to the remote repository of your organization so that this archetype is available for users to all other developers and members working in your organization.
Maven tool provides us with many in-built archetype artifacts that can be used by us for our projects. Depending upon the type of the project, technologies, languages, and domain of our project, we can choose the artifact that we want to use for generating our project. If none of the available fits or you want to use your self-defined archetype, you can create an archetype.
To list out all the artifact Ids archetypes present in the maven, you can fire the following command on your terminal –
mvn archetype: generate
This command is used to create a new project with the specified archetype and is the goal available in maven. But as we are not mentioning any artifactid or number equivalent to a particular id, it will display the list of the artifacts of archetype available in your installed version on the terminal and ask for entering the number of the artifact id of the archetype whose template is to be used for creating your project. You can exit your command prompt to skip project creation by using the Ctrl+Z command on UNIX/Linux and Ctrl+c on the windows machine. The output of the above command is too lengthy but I will share with you the start and end portion of it which is somewhat like following –
The above output shows that 2690 artifact ids are corresponding to the archetypes available in maven. Alternatively, you can export the resultset of the command into a file to view the archetype list available by using the following command –
mvn archetype:generate > listOfArchetypes.txt
Firing will result in the creation of a new file named listOfArchetypes that will contain the list of archetypes available.
Some of the available and most often used artifacts are listed below –
|Artifact Id of archetype||Usage|
|Maven-archetype-archetype artifactid||This artifact id is used to create a sample archetype project using maven.|
|Maven-archetype-site-simple artifactid||This artifact id is used to create a sample maven site using maven.|
|Maven-archetype-webapp artifactid||This artifact id is used to create a sample webappmaven project.|
|Maven-archetype-plugin artifactid||This artifact id is used to create a sample maven plugin.|
|Maven-archetype-quickstart artifactid||This artifact id is used to create a sample project using maven.|
|Maven-archetype-portlet artifactid||This artifact id is used to create a sample JSR-268 Portlet using maven.|
|Maven-archetype-site artifactid||This artifact id is used to create a sample maven site consisting of some of the supported types of documents such as Xdoc, FML, and APT and help in guiding how to i18n your maven site.|
|Maven-archetype-plugin-site artifactid||This artifact id is used to create a sample plugin site using maven.|
|Maven-archetype-j2ee-simple artifactid||This artifact id is used to create a sample J2EE application project using maven.|
|Maven-archetype-mojo artifactid||This artifact id is used to create a sample maven plugin.|
|Maven-archetype-simple artifactid||This artifact id is used to create a simple maven project.|
Maven Archetype Creation
If you want to create your archetype, you can do so by following certain simple steps. An archetype is itself a normal project along with some extra content that is as follows –
- src/main/resources/archetype-resources – This is the directory from where the archetype i.e template resources are copied while creating a new project of the current archetype.
- src/main/resources/META-INF/maven/archetype-metadata.xml – This file stores the description of the metadata of the archetype.
We can create a new archetype by using the archetype-maven-plugin and then modifyu=ing the contents of the above two mentioned things of archetype archetype-resources directory and archetype-metadata.xml file.
Fire the following command to create a new archetype –
mvn archetype:generate -B -DarchetypeArtifactId=maven-archetype-archetype \
Alternatively, we can create a normal project and add these two things in it to create an archetype. To create archetype from the existing project you can make the use of the following goal and fire the command –
The general structure of the archetype is as shown below –
Conclusion – Maven Archetype
We can create the maven project of a particular archetype. Archetypes are just the template or structure defining how the project should be created. There are many available archetypes for different usages. If you want to create a new archetype, you can do so and even share it with your organization by deploying it on a remote server of your organization.
This is a guide to Maven Archetype. here we discuss how the maven project should be created along with the available archetypes for different usages. You may also look at the following articles to learn more –